How to add Three Level ListView in ExpandableListView in android

前端 未结 4 1424
清酒与你
清酒与你 2020-12-03 00:14

I want to add one more level of in ExpandableListView.In current ExpandableListView is two level how can i add one more level. I am new in android developing please help me.

4条回答
  •  北海茫月
    2020-12-03 00:37

    You can try my following sample code. I have posted my full project to GitHub

    Of course, you should modify more to meet all your requirements. For basic case, I only use the data source in the arrays.xml file. Hope this helps!

    arrays.xml:

    
        
            Level 1.1
            Level 1.2
            Level 1.3
        
        
            Level 1.1.1
            Level 1.1.2
        
        
            Level 1.2.1
        
        
            Second Level 01
            Second Level 02
            Second Level 03
        
        
            Child Level 01
            Child Level 02
        
    
    

    CustomExpListView.java:

    public class CustomExpListView extends ExpandableListView
    {
        public CustomExpListView(Context context)
        {
            super(context);
        }
        protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
        {
            widthMeasureSpec = MeasureSpec.makeMeasureSpec(960, MeasureSpec.AT_MOST);
            heightMeasureSpec = MeasureSpec.makeMeasureSpec(600, MeasureSpec.AT_MOST);
            super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        }
    }
    

    ParentLevelAdapter.java:

    public class ParentLevelAdapter extends BaseExpandableListAdapter {
        private final Context mContext;
        private final List mListDataHeader;
        private final Map> mListData_SecondLevel_Map;
        private final Map> mListData_ThirdLevel_Map;
        public ParentLevelAdapter(Context mContext, List mListDataHeader) {
            this.mContext = mContext;
            this.mListDataHeader = new ArrayList<>();
            this.mListDataHeader.addAll(mListDataHeader);
            // Init second level data
            String[] mItemHeaders;
            mListData_SecondLevel_Map = new HashMap<>();
            int parentCount = mListDataHeader.size();
            for (int i = 0; i < parentCount; i++) {
                String content = mListDataHeader.get(i);
                switch (content) {
                    case "Level 1.1":
                        mItemHeaders = mContext.getResources().getStringArray(R.array.items_array_expandable_level_one_one_child);
                        break;
                    case "Level 1.2":
                        mItemHeaders = mContext.getResources().getStringArray(R.array.items_array_expandable_level_one_two_child);
                        break;
                    default:
                        mItemHeaders = mContext.getResources().getStringArray(R.array.items_array_expandable_other_child);
                }
                mListData_SecondLevel_Map.put(mListDataHeader.get(i), Arrays.asList(mItemHeaders));
            }
            // THIRD LEVEL
            String[] mItemChildOfChild;
            List listChild;
            mListData_ThirdLevel_Map = new HashMap<>();
            for (Object o : mListData_SecondLevel_Map.entrySet()) {
                Map.Entry entry = (Map.Entry) o;
                Object object = entry.getValue();
                if (object instanceof List) {
                    List stringList = new ArrayList<>();
                    Collections.addAll(stringList, (String[]) ((List) object).toArray());
                    for (int i = 0; i < stringList.size(); i++) {
                        mItemChildOfChild = mContext.getResources().getStringArray(R.array.items_array_expandable_level_three);
                        listChild = Arrays.asList(mItemChildOfChild);
                        mListData_ThirdLevel_Map.put(stringList.get(i), listChild);
                    }
                }
            }
        }
        @Override
        public Object getChild(int groupPosition, int childPosition) {
            return childPosition;
        }
        @Override
        public long getChildId(int groupPosition, int childPosition) {
            return childPosition;
        }
        @Override
        public View getChildView(int groupPosition, int childPosition,
                                 boolean isLastChild, View convertView, ViewGroup parent) {
            final CustomExpListView secondLevelExpListView = new CustomExpListView(this.mContext);
            String parentNode = (String) getGroup(groupPosition);
            secondLevelExpListView.setAdapter(new SecondLevelAdapter(this.mContext, mListData_SecondLevel_Map.get(parentNode), mListData_ThirdLevel_Map));
            secondLevelExpListView.setGroupIndicator(null);
            return secondLevelExpListView;
        }
        @Override
        public int getChildrenCount(int groupPosition) {
            return 1;
        }
        @Override
        public Object getGroup(int groupPosition) {
            return this.mListDataHeader.get(groupPosition);
        }
        @Override
        public int getGroupCount() {
            return this.mListDataHeader.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 layoutInflater = (LayoutInflater) this.mContext
                        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                convertView = layoutInflater.inflate(R.layout.drawer_list_group, parent, false);
            }
            TextView lblListHeader = (TextView) convertView
                    .findViewById(R.id.lblListHeader);
            lblListHeader.setTypeface(null, Typeface.BOLD);
            lblListHeader.setTextColor(Color.CYAN);
            lblListHeader.setText(headerTitle);
            return convertView;
        }
        @Override
        public boolean hasStableIds() {
            return true;
        }
        @Override
        public boolean isChildSelectable(int groupPosition, int childPosition) {
            return true;
        } }
    

    SecondLevelAdapter.java:

    public class SecondLevelAdapter extends BaseExpandableListAdapter
    {
        private final Context mContext;
        private final List mListDataHeader;
        private final Map> mListDataChild;
        public SecondLevelAdapter(Context mContext, List mListDataHeader, Map> mListDataChild) {
            this.mContext = mContext;
            this.mListDataHeader = mListDataHeader;
            this.mListDataChild = mListDataChild;
        }
        @Override
        public Object getChild(int groupPosition, int childPosition)
        {
            return this.mListDataChild.get(this.mListDataHeader.get(groupPosition))
                    .get(childPosition);
        }
        @Override
        public long getChildId(int groupPosition, int childPosition)
        {
            return childPosition;
        }
        @Override
        public View getChildView(int groupPosition, int childPosition,
                                 boolean isLastChild, View convertView, ViewGroup parent)
        {
            final String childText = (String) getChild(groupPosition, childPosition);
    
            if (convertView == null) {
                LayoutInflater layoutInflater = (LayoutInflater) this.mContext
                        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                convertView = layoutInflater.inflate(R.layout.drawer_list_item, parent, false);
            }
            TextView txtListChild = (TextView) convertView
                    .findViewById(R.id.lblListItem);
            txtListChild.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 15);
            txtListChild.setText(childText);
            return convertView;
        }
        @Override
        public int getChildrenCount(int groupPosition)
        {
            try {
                return this.mListDataChild.get(this.mListDataHeader.get(groupPosition)).size();
            } catch (Exception e) {
                return 0;
            }
        }
        @Override
        public Object getGroup(int groupPosition)
        {
            return this.mListDataHeader.get(groupPosition);
        }
        @Override
        public int getGroupCount()
        {
            return this.mListDataHeader.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 layoutInflater = (LayoutInflater) this.mContext
                        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                convertView = layoutInflater.inflate(R.layout.drawer_list_group_second, parent, false);
            }
            TextView lblListHeader = (TextView) convertView
                    .findViewById(R.id.lblListHeader);
            lblListHeader.setText(headerTitle);
            lblListHeader.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 18);
            lblListHeader.setTextColor(Color.YELLOW);
            return convertView;
        }
        @Override
        public boolean hasStableIds() {
            return true;
        }
        @Override
        public boolean isChildSelectable(int groupPosition, int childPosition) {
            return true;
        }
    }
    

    MainActivity.java:

    public class MainActivity extends AppCompatActivity {
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.content_main);
            // Init top level data
            List listDataHeader = new ArrayList<>();
            String[] mItemHeaders = getResources().getStringArray(R.array.items_array_expandable_level_one);
            Collections.addAll(listDataHeader, mItemHeaders);
            ExpandableListView mExpandableListView = (ExpandableListView) findViewById(R.id.expandableListView_Parent);
            if (mExpandableListView != null) {
                ParentLevelAdapter parentLevelAdapter = new ParentLevelAdapter(this, listDataHeader);
                mExpandableListView.setAdapter(parentLevelAdapter);
            }
        }
    }
    

    Screenshot result:

提交回复
热议问题