How to implement an ExpandableList in a ViewPager in Android?

后端 未结 1 1340
南方客
南方客 2020-12-18 15:57

How to implement an ExpandableList in a ViewPager in Android ?

This is a simple 1 file ExpandableList provided by google

/*
 * Copyrig         


        
相关标签:
1条回答
  • 2020-12-18 16:30

    First of all add make a normal view pager and in the overridden method instantiateItem

                    if(position == 2) {// third page
                            View v = inflater.inflate(R.layout.expander, null, false);
                            ExpandableListView elv1 = (ExpandableListView) v.findViewById(R.id.elv1);
                            mAdapter = new BaseExpandableListAdapter() {
    
                        @Override
                        public Object getChild(int groupPosition,
                                int childPosition) {
                            return null;
                        }
    
                        @Override
                        public long getChildId(int groupPosition,
                                int childPosition) {
                            return 0;
                        }
    
                        @Override
                        public int getChildrenCount(int groupPosition) {
                            return 3; // Size of children usually taken from
                                        // .length or .size()
                        }
    
                        @Override
                        public View getChildView(int groupPosition,
                                int childPosition, boolean isLastChild,
                                View convertView, ViewGroup parent) {
                            View v = inflater.inflate(R.layout.twolinelistitem,
                                    null, false);
                            TextView tv = (TextView) v
                                    .findViewById(R.id.simple_expandable_list_item_2_text1);
                            tv.setText("Child " + (childPosition + 1)
                                    + " of group " + (groupPosition + 1));
                            return v;
                        }
    
                        @Override
                        public Object getGroup(int groupPosition) {
                            return null;
                        }
    
                        @Override
                        public int getGroupCount() {
                            return 12; // Groups count usually taken from
                                        // .length or .size()
                        }
    
                        @Override
                        public long getGroupId(int groupPosition) {
                            return 0;
                        }
    
                        @Override
                        public View getGroupView(int groupPosition,
                                boolean isExpanded, View convertView,
                                ViewGroup parent) {
                            View v = inflater.inflate(R.layout.twolinelistitem,
                                    parent, false);
                            TextView tv = (TextView) v
                                    .findViewById(R.id.simple_expandable_list_item_2_text1);
                            tv.setText("Group " + (groupPosition + 1));
                            return v;
                        }
    
                        @Override
                        public boolean hasStableIds() {
                            return false;
                        }
    
                        @Override
                        public boolean isChildSelectable(int groupPosition,
                                int childPosition) {
                            return true;
                        }
                    };
                    elv1.setAdapter(ViewPagerPlusExpandableListActivity.this.mAdapter);
    
                    ((ViewPager) collection).addView(v, 0);
                    return v;
                }
    

    I am sure you are lost, this is why i made this example and posted it on github

    0 讨论(0)
提交回复
热议问题