多级可扩展列表视图

Posted

技术标签:

【中文标题】多级可扩展列表视图【英文标题】:Multilevel Expandable Listview 【发布时间】:2019-05-21 17:04:02 【问题描述】:

我正在尝试将值添加到父 groupPosition[0] 中。但它反映到父列表项的所有 groupPositions。请帮帮我。

   public class MainActivity extends Activity
    
    ExpandableListView explvlist;
    ArrayList<String> StringArray = new ArrayList<String>();
    ArrayList<String> StringArrayChild=new ArrayList<String>();
    @Override
    public void onCreate(Bundle savedInstanceState)
    
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ArrayValueAddFunction();
        ArrayValueAddFunction_Level2();
        explvlist = (ExpandableListView)findViewById(R.id.ParentLevel);
        explvlist.setAdapter(new ParentLevel(StringArray)); 
    
    private void ArrayValueAddFunction_Level2() 
        StringArrayChild.add("Level1 item1");
        StringArrayChild.add("Level1 item2");
        StringArrayChild.add("Level1 item3");
        StringArrayChild.add("Level1 item4");
        StringArrayChild.add("Level1 item5");
        StringArrayChild.add("Level1 item6");
    
    private void ArrayValueAddFunction() 
        StringArray.add("ONE");
        StringArray.add("TWO");
        StringArray.add("THREE");
        StringArray.add("FOUR");
        StringArray.add("FIVE");
        StringArray.add("SIX");
    
    public class ParentLevel extends BaseExpandableListAdapter 
        ArrayList<String> StringArray = new ArrayList<String>();
        private Context context;
        public ParentLevel(ArrayList<String> stringArray) 
            this.StringArray=stringArray;
        
        @Override
        public Object getChild(int arg0, int arg1) 
            return arg1;
        
        @Override
        public long getChildId(int groupPosition, int childPosition) 
            return childPosition;
        
        @Override
        public View getChildView(int groupPosition, int childPosition,
                                 boolean isLastChild, View convertView, ViewGroup parent) 
            CustExpListview SecondLevelexplv = new CustExpListview(MainActivity.this);

                SecondLevelexplv.setAdapter(new SecondLevelAdapter());
                SecondLevelexplv.setGroupIndicator(null);
                return SecondLevelexplv;
        
        @Override
        public int getChildrenCount(int groupPosition) 
            return 3;
        
        @Override
        public Object getGroup(int groupPosition) 
            return groupPosition;
        
        @Override
        public int getGroupCount() 
            return StringArray.size();
        
        @Override
        public long getGroupId(int groupPosition) 
            return groupPosition;
        
        @Override
        public View getGroupView(int groupPosition, boolean isExpanded,
                                        View convertView, ViewGroup parent) 
            TextView tv = new TextView(MainActivity.this);
            if (groupPosition==0) 
                tv.append(StringArray.get(0));
            else if (groupPosition==1)
                tv.append(StringArray.get(1));
            else if (groupPosition==2)
                tv.append(StringArray.get(2));
            else if (groupPosition==3)
                tv.append(StringArray.get(3));
            else if (groupPosition==4)
                tv.append(StringArray.get(4));
            else if (groupPosition==5)
                tv.append(StringArray.get(5));
            
            return tv;
        
        @Override
        public boolean hasStableIds() 
            return true;
        
        @Override
        public boolean isChildSelectable(int groupPosition, int childPosition) 
            return true;
        
        public class SecondLevelAdapter extends BaseExpandableListAdapter
        

            @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)
            
                TextView tv = new TextView(MainActivity.this);
                tv.setText("child");
                tv.setPadding(15, 5, 5, 5);
                tv.setBackgroundColor(Color.GRAY);
                tv.setLayoutParams(new ListView.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT));
                return tv;
            
            @Override
            public int getChildrenCount(int groupPosition) 
                return 6;
            
            @Override
            public Object getGroup(int groupPosition) 
                return groupPosition;
            
            @Override
            public int getGroupCount() 
                return 1;
            

            @Override
            public long getGroupId(int groupPosition)
            
                return groupPosition;
            

            @Override
            public View getGroupView(int groupPosition, boolean isExpanded,
                                     View convertView, ViewGroup parent)
            
                TextView tv = new TextView(MainActivity.this);
                if (groupPosition==0) 
                    tv.append(StringArrayChild.get(0));
                else if (groupPosition==1)
                    tv.append(StringArrayChild.get(1));
                else if (groupPosition==2)
                    tv.append(StringArrayChild.get(2));
                else if (groupPosition==3)
                    tv.append(StringArrayChild.get(3));
                else if (groupPosition==4)
                    tv.append(StringArrayChild.get(4));
                else if (groupPosition==5)
                    tv.append(StringArrayChild.get(5));
                
                tv.setPadding(12, 7, 7, 7);
                tv.setBackgroundColor(Color.YELLOW);
                return tv;
            

            @Override
            public boolean hasStableIds() 
                // TODO Auto-generated method stub
                return true;
            

            @Override
            public boolean isChildSelectable(int groupPosition, int childPosition) 
                // TODO Auto-generated method stub
                return true;
            

        
    
    

CustExpList 视图

     class CustExpListview extends ExpandableListView
     
    int intGroupPosition, intChildPosition, intGroupid;
    public CustExpListview(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);
    

expandableListView 的布局设计

     <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_
    android:layout_
    tools:context=".MainActivity">
    <ExpandableListView
        android:layout_
        android:id="@+id/ParentLevel"
        android:groupIndicator="@null"
        android:layout_>
    </ExpandableListView>
</LinearLayout>

下面给出的图像是输出,所有项目都进入所有位置,但我需要放置位置[0],但它反映到父列表的所有 groupPositins

【问题讨论】:

【参考方案1】:

我认为问题发生在getGroupViewgroupPosition 总是返回 0。检查您的代码是否存在该问题。 对了,你可以用这个NLeveLExpandableListView。

【讨论】:

感谢兄弟,您的宝贵回复..我只需要 Root1 和 root2 的扩展子项,而对于剩余的根,我只需要子项..我热切地等待您的回复 您的代码完全错误,并且会产生一些不必要的循环。制作NLevelxpandable ListView 的方法有很多,例如查看this blog post 关于如何创建可扩展NLevel 列表 @SadraIsapanahAmlashi 是否有任何关于折叠所有项目并将所有项目一起展开以获取您的 github 链接的提示?

以上是关于多级可扩展列表视图的主要内容,如果未能解决你的问题,请参考以下文章

在保持 N 级或多级可展开列表视图的折叠/展开状态后,某些子组不会显示

如何在相对布局中添加固定标题和滚动视图表行?

DRF中五大扩展类及视图集的介绍

如何从 postgresql 视图表中获取要显示的对象列表

Unity实用小工具或脚本——可折叠伸缩的多级列表二(带搜索功能)

Unity实用小工具或脚本——可折叠伸缩的多级列表二(带搜索功能)