Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Android Expandable ListView

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 1.07k
    Comment on it

    Expandable listview is used to categorise data in some specific type. Expandable list can have a two level scrolling list. Using ChildDivider feature we divide list items with drawables or colors, and ChildIndicator & GroupIndicator is for start, end , left and right bounds.

    activity_main.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="match_parent"
        android:orientation="vertical"
        android:background="#f4f4f4" >
    
        <ExpandableListView
            android:id="@+id/lvExpandable"
            android:layout_height="match_parent"
            android:layout_width="match_parent"
            android:childDivider="#EA8949"
            android:childIndicatorEnd="14.5sp"
            android:childIndicatorLeft="14.5sp"
            android:childIndicatorRight="14.5sp"
            android:childIndicatorStart="14.5sp"
            android:indicatorEnd="14.5sp"
            android:indicatorLeft="14.5sp"
            android:indicatorRight="14.5sp"
            android:indicatorStart="14.5sp"/>
    
    </LinearLayout>
    

    MainActivity.class having Expandable ListView

    public class MainActivity extends Activity {
        ExpandableListView expandableListView;
        ExpandableListAdapter expandableListAdapter;
        List<String> listGroup;
        HashMap<String,List<String>> listItem;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            expandableListView = (ExpandableListView)findViewById(R.id.lvExpandable);
            prepareListData();
            expandableListAdapter = new ExpandableListAdapter(this, listGroup, listItem);
            expandableListView.setAdapter(expandableListAdapter);
    
            // ListView onChild Click Listener
            expandableListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
                @Override
                public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
                    // your text
                    return false;
                }
            });
    
            // Used for being notified when a group is expanded
            expandableListView.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {
                @Override
                public void onGroupExpand(int groupPosition) {
                    // your text
                }
            });
    
            // Used for being notified when a group is collapsed
            expandableListView.setOnGroupCollapseListener(new ExpandableListView.OnGroupCollapseListener() {
                @Override
                public void onGroupCollapse(int groupPosition) {
                    // your text
                }
            });
    
            // ListView onGroup Click Listener
            expandableListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
                @Override
                public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
                    // your text
                    return false;
                }
            });
        }
    
        public void prepareListData(){
            listGroup = new ArrayList<>();
            listItem = new HashMap<String, List<String>>();
    
            listGroup.add("first list header");
            listGroup.add("second list header");
            listGroup.add("third list header");
    
            List<String> first = new ArrayList<>();
            first.add("first sub heading");
            first.add("second sub heading");
            first.add("third sub heading");
    
            List<String> second = new ArrayList<>();
            second.add("first sub heading");
            second.add("second sub heading");
            second.add("third sub heading");
    
            List<String> third = new ArrayList<>();
            third.add("first sub heading");
            third.add("second sub heading");
            third.add("third sub heading");
    
            listItem.put(listGroup.get(0), first);
            listItem.put(listGroup.get(1), second);
            listItem.put(listGroup.get(2), third);
        }
    }
    

    list_group.xml layout for group

    <?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:orientation="vertical"
        android:padding="8dp"
        android:background="#5698BD">
    
        <TextView
            android:id="@+id/tvListHeader"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingLeft="?android:attr/expandableListPreferredItemPaddingLeft"
            android:textSize="17dp"
            android:textColor="#f9f93d" />
    
    </LinearLayout>
    

    list_itm.xml layout for child items

    <?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="55dp"
        android:orientation="vertical" >
    
        <TextView
            android:id="@+id/tvListItem"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="17dp"
            android:paddingTop="5dp"
            android:paddingBottom="5dp"
            android:paddingLeft="?android:attr/expandableListPreferredChildPaddingLeft" />
    
    </LinearLayout>
    

    ExpandableListAdapter.class custom adapter class to create listview which extends BaseExpandableListAdapter.class

    public class ExpandableListAdapter extends BaseExpandableListAdapter {
    
        private Context context;
        private List<String> listGroup;
        private HashMap<String, List<String>> listItem;
    
        public ExpandableListAdapter(Context context, List<String> listDataHeader, HashMap<String, List<String>> listChildData) {
            this.context = context;
            this.listGroup = listDataHeader;
            this.listItem = listChildData;
        }
    
        @Override
        public Object getChild(int groupPosition, int childPosititon) {
            return this.listItem.get(this.listGroup.get(groupPosition)).get(childPosititon);
        }
    
        @Override
        public long getChildId(int groupPosition, int childPosition) {
            return childPosition;
        }
    
        @Override
        public View getChildView(int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
    
            final String childText = (String) getChild(groupPosition, childPosition);
    
            if (convertView == null) {
                LayoutInflater infalInflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                convertView = infalInflater.inflate(R.layout.list_item, null);
            }
    
            TextView txtListChild = (TextView) convertView.findViewById(R.id.tvListItem);
    
            txtListChild.setText(childText);
            return convertView;
        }
    
        @Override
        public int getChildrenCount(int groupPosition) {
            return this.listItem.get(this.listGroup.get(groupPosition)).size();
        }
    
        @Override
        public Object getGroup(int groupPosition) {
            return this.listGroup.get(groupPosition);
        }
    
        @Override
        public int getGroupCount() {
            return this.listGroup.size();
        }
    
        @Override
        public long getGroupId(int groupPosition) {
            return groupPosition;
        }
    
        @Override
        public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
            String headerTitle = (String) getGroup(groupPosition);
    
            if (convertView == null) {
                LayoutInflater infalInflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                convertView = infalInflater.inflate(R.layout.list_group, null);
            }
    
            TextView lblListHeader = (TextView) convertView.findViewById(R.id.tvListHeader);
            lblListHeader.setTypeface(null, Typeface.BOLD);
            lblListHeader.setText(headerTitle);
    
            return convertView;
        }
    
        @Override
        public boolean hasStableIds() {
            return false;
        }
    
        @Override
        public boolean isChildSelectable(int groupPosition, int childPosition) {
            return true;
        }
    }
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: