Sunday 25 March 2018

Expandable ListView


Expandable ListView = ListView with Nested ListVieew (Two Listview)

Short :
1. Defile Expandable ListView XML
---------------------------------------------------------------------------------

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 
xmlns:android="http://schemas.android.com/apk/res/android"    
xmlns:app="http://schemas.android.com/apk/res-auto"    
android:id="@+id/root"    
android:layout_width="match_parent"    
android:layout_height="match_parent"    >

<ExpandableListView        
android:id="@+id/poo"        
android:layout_width="match_parent"        
android:layout_height="match_parent"        
android:divider="#f00"        
android:dividerHeight="1dp"        />

</android.support.constraint.ConstraintLayout>
---------------------------------------------------------------------------------
2. Defile Expandable ListView Group XML
---------------------------------------------------------------------------------

<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    
android:layout_width="match_parent"    
android:layout_height="wrap_content"    
android:background="@color/colorPrimary"    >
    <TextView        
android:id="@+id/idPooGroup"        
android:layout_width="match_parent"        
android:layout_height="wrap_content"        
android:text="Group"        
android:textColor="@color/colorWhite"        
android:textSize="20dp"        
/>

</LinearLayout>
---------------------------------------------------------------------------------

3. Defile Expandable ListView Child XML
---------------------------------------------------------------------------------

<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    
android:layout_width="match_parent"    
android:layout_height="wrap_content"    
android:background="@color/colorAccent"    
>
    <TextView        
android:id="@+id/idPooChild"        
android:layout_width="match_parent"        
android:layout_height="wrap_content"        
android:text="Child"        
android:textColor="@color/colorWhite"        
android:textSize="20dp"        
/>
</LinearLayout>
---------------------------------------------------------------------------------

4. MainActivity.java
---------------------------------------------------------------------------------

public class MainActivity extends AppCompatActivity {

    private static final String TAG = "MainActivity";

    private ExpandableListView poo;
    CustomAdapter customAdapter;

    @Override    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        poo = (ExpandableListView)findViewById(R.id.poo);
        getParentFood();

    }

    public void getParentFood(){

        List<String> foodies = new ArrayList<>();
        foodies.add("Fruit");
        foodies.add("Junk Food");

        List<String> fruits = new ArrayList<>();
        fruits.add("Apple");
        fruits.add("Mango");
        fruits.add("Banana - sorry cold");
        fruits.add("PineApple - ChowMow");

        List<String> junkFoods = new ArrayList<>();
        junkFoods.add("King Burger");
        junkFoods.add("Pizza with coke");
        junkFoods.add("Afgani Sandwitch");

        HashMap<String, List<String>> headerMap = new HashMap<>();
        headerMap.put(foodies.get(0), fruits);
        headerMap.put(foodies.get(1), junkFoods);

        customAdapter = new CustomAdapter(MainActivity.this, foodies, headerMap);
        poo.setAdapter(customAdapter);

    }

}
---------------------------------------------------------------------------------

5. CustomAdapter.java
---------------------------------------------------------------------------------

public class CustomAdapter extends BaseExpandableListAdapter {

    Context mContext;
    List<String> foodies;
    HashMap<String, List<String>> headerMap;

    CustomAdapter(Context mContext, List<String> foodies, HashMap<String, List<String>> headerMap){
        this.mContext = mContext;
        this.foodies = foodies;
        this.headerMap = headerMap;

    }
    
    @Override    public int getGroupCount() {
        return headerMap.size();
    }

    @Override    public int getChildrenCount(int groupPosition) {
        return headerMap.get(foodies.get(groupPosition)).size();
    }

    @Override    public Object getGroup(int groupPosition) {
        return null;
    }

    @Override    public Object getChild(int groupPosition, int childPosition) {
        return null;
    }

    @Override    public long getGroupId(int groupPosition) {
        return 0;
    }

    @Override    public long getChildId(int groupPosition, int childPosition) {
        return 0;
    }

    @Override    public boolean hasStableIds() {
        return false;
    }

    @Override    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        View groupView = LayoutInflater.from(mContext).inflate(R.layout.item_group, null);
        TextView groupTV = groupView.findViewById(R.id.idPooGroup);
        groupTV.setText(foodies.get(groupPosition));
        return groupView;
    }

    @Override    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        View childView = LayoutInflater.from(mContext).inflate(R.layout.item_child, null);
        TextView childTV = childView.findViewById(R.id.idPooChild);
        childTV.setText(headerMap.get(foodies.get(groupPosition)).get(childPosition));
        return childView;
    }

    @Override    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return false;
    }
}
---------------------------------------------------------------------------------

No comments:

Post a Comment