Android ExpandableListView控件中child嵌套ListView显示

话不多说,先上效果图

在这里插入图片描述这里是父控件布局
在这里插入图片描述这里是子控件布局,嵌套ListView一齐使用

首先,现在主布局文件中定义ExpandableListView控件

<ExpandableListView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:id="@+id/sm_3_expand"
                android:groupIndicator="@null"
                />

android:groupIndicator="@null"将默认的箭头去掉

自定义一个类继承BaseExpandableListAdapter

class MyAdatper extends BaseExpandableListAdapter{

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

        @Override
        public int getChildrenCount(int childPosition) {
            return 1;
        }

        @Override
        public Object getGroup(int groupPosition) {
            return myStations.getROWS_DETAIL().get(i);
        }

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

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

        @Override
        public long getChildId(int i, int i1) {
            return 1;
        }

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

        @Override
        public View getGroupView(int position, boolean b, View context, ViewGroup viewGroup) {
  
            return context;
        }

        @Override
        public View getChildView(int position, int position2, boolean b, View context, ViewGroup viewGroup) {
           

            return context;
        }

        @Override
        public boolean isChildSelectable(int i, int i1) {
            return true;
        }
    }

自定义两个XML文件,用来加载getGroupView()布局和getChildView()布局

用来加载父布局的XML (group_item)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="150dp">

    <TextView
        android:id="@+id/sm_3_id"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:gravity="center"
        android:background="@drawable/sm_7_ret"
        android:text="TextView" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:orientation="vertical">
        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center"
            android:id="@+id/sm_3_all_sation"
            android:layout_weight="1"/>
        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/sm_3_all_price"
            android:gravity="center"
            android:layout_weight="1"/>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:orientation="vertical">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="asdasd"
            android:gravity="center"
            android:id="@+id/sm_3_0_time"
            android:drawableLeft="@mipmap/liveing"
            android:layout_weight="1"/>
        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="asdasd"
            android:id="@+id/sm_3_1_time"
            android:drawableLeft="@mipmap/liveing"
            android:gravity="center"
            android:layout_weight="1"/>

    </LinearLayout>

    <ImageView
        android:id="@+id/sm_7_img"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        app:srcCompat="@mipmap/arrow_left" />
</LinearLayout>

用来加载子布局的XML (child_item)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:orientation="vertical"
    android:descendantFocusability="blocksDescendants"
    android:layout_height="match_parent">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:layout_weight="1"
        android:textSize="30dp"
        android:text="起点:" />
    <com.lenovo.studentClient.activity.MyListView 
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/sm_3_child_list"
        android:layout_weight="2"
        />
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:layout_weight="1"
        android:textSize="30dp"
        android:text="终点" />
</LinearLayout>

注意,在子布局中,使用的是自定义的ListView,稍后会提到自定义ListView

加载子布局

        @Override
        public View getChildView(int position, int position2, boolean b, View context, ViewGroup viewGroup) {
        if(context == null){
                context = getLayoutInflater().inflate(R.layout.child_item,null);
            }    

            return context;
        }


自定义一个类继承ListView

public class MyListView extends ListView {
    public Smart3_NextFull(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
    //动态长度
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2 , MeasureSpec.AT_MOST));
    }
 }

在子布局中(这里非常重要啊!!!小编被这个坑困了很久)

   @Override
        public View getChildView(int position, int position2, boolean b, View context, ViewGroup viewGroup) {
        if(context == null){
                context = getLayoutInflater().inflate(R.layout.child_item,null);
            }    
            //加载自定义ListView,实体类里面数据可以先忽略,这里着重讲ListView显示不完全的问题
 		ListView sm_3_child_list = context.findViewById(R.id.sm_3_child_list);
            sm_3_child_list.setAdapter(new ArrayAdapter(getApplicationContext() , android.R.layout.simple_list_item_1 , myStations.getROWS_DETAIL().get(position).getSites()));
            return context;
        }

在完成这一步之后,就可以在ExpandableListView控件中,完整的显示item中listView显示不完整的问题啦!!

关于JSON数据方面的获取,不是很重要,但是还是提一下吧

JSON数据如下

{“Line”:0,“UserName”:“user1”}
返回 JSON
{“RUSULT”:“S”,“ERRMSG”:“成功”,“ROWS_DETAIL”:[{“id”:1,“name”:"1 号线
",“time”:[{“site”:“迈皋桥
“,“starttime”:“05:42:00”,“endtime”:“23:19:00”},{“site”:“中国药科大学
“,“starttime”:“05:47:00”,“endtime”:“23:27:00”}],“map”:“img/001.png”,“sites”:[”
迈皋桥”,“红山动物园”,“南京站”,“新模范马路”,“玄武门”,“鼓楼”,“珠江路”,“新街口”,”
张府园”,“三山街”,“中华门”,“安德门”,“天隆寺”,“软件大道”,“花神庙”,“南京南站”,“双
龙大道”,“河定桥”,“胜太路”,“百家湖”,“小龙湾”,“竹山路”,“天印大道”,“龙眠大道”,“南
医大江苏经贸学院站”,“南京交院”,“中国药科大学”]},……]}
{“RESULT”:“F”,“ERRMSG”:“失败”}

根据JSON数据定义实体类

 class MyStations {

        List<Smart7.MyStations.Station2> ROWS_DETAIL;

        public List<Smart7.MyStations.Station2> getROWS_DETAIL() {
            return ROWS_DETAIL;
        }

        public void setROWS_DETAIL(List<Smart7.MyStations.Station2> ROWS_DETAIL) {
            this.ROWS_DETAIL = ROWS_DETAIL;
        }

        class Station2 {
            int id;
            String name;

            public int getId() {
                return id;
            }

            public void setId(int id) {
                this.id = id;
            }

            public String getName() {
                return name;
            }

            public void setName(String name) {
                this.name = name;
            }

            public List<Smart7.MyStations.Station2.Station3> getTime() {
                return time;
            }

            public void setTime(List<Smart7.MyStations.Station2.Station3> time) {
                this.time = time;
            }

            public String getMap() {
                return map;
            }

            public void setMap(String map) {
                this.map = map;
            }

            public List<String> getSites() {
                return sites;
            }

            public void setSites(List<String> sites) {
                this.sites = sites;
            }

            public List<String> getTransfersites() {
                return transfersites;
            }

            public void setTransfersites(List<String> transfersites) {
                this.transfersites = transfersites;
            }

            List<Smart7.MyStations.Station2.Station3> time;
            class Station3{
                public String getSite() {
                    return site;
                }

                public void setSite(String site) {
                    this.site = site;
                }

                public String getStarttime() {
                    return starttime;
                }

                public void setStarttime(String starttime) {
                    this.starttime = starttime;
                }

                public String getEndtime() {
                    return endtime;
                }

                public void setEndtime(String endtime) {
                    this.endtime = endtime;
                }

                String site;
                String starttime;
                String endtime;
            }
            String map;
            List<String> sites;
            List<String> transfersites;
        }
    }

数据使用Gson转换一下就行了 ,注意字段要对应,不然是null

 public void onResponse(JSONObject jsonObject) {
 //返回的是一个实体类
                myStations = new Gson().fromJson(jsonObject.toString(), MyStations.class);
               }

然后设置数据也就这些了,这里是全部适配器的代码啦

class MyAdatper extends BaseExpandableListAdapter{

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

        @Override
        public int getChildrenCount(int childPosition) {
            return 1;
        }

        @Override
        public Object getGroup(int groupPosition) {
            return myStations.getROWS_DETAIL().get(i);
        }

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

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

        @Override
        public long getChildId(int i, int i1) {
            return 1;
        }

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

        @Override
        public View getGroupView(int position, boolean b, View context, ViewGroup viewGroup) {
  
  if(context == null){
                context = getLayoutInflater().inflate(R.layout.group_item,null);
            }
            TextView sm_3_id = context.findViewById(R.id.sm_3_id);
            TextView sm_3_all_sation = context.findViewById(R.id.sm_3_all_sation);
            TextView sm_3_all_price = context.findViewById(R.id.sm_3_all_price);
            TextView sm_3_0_time = context.findViewById(R.id.sm_3_0_time);
            TextView sm_3_1_time = context.findViewById(R.id.sm_3_1_time);

            sm_3_id.setText(myStations.getROWS_DETAIL().get(position).getId()+"号线");
            sm_3_all_sation.setText(myStations.getROWS_DETAIL().get(position).getTime().get(0).getSite() +"---" +myStations.getROWS_DETAIL().get(position).getTime().get(1).getSite());

            sm_3_all_price.setText("票价:¥"+(int) (Math.random()*20)+"    里程:"+(int) (Math.random()*35)+"km");

            sm_3_0_time.setText(myStations.getROWS_DETAIL().get(position).getTime().get(0).getStarttime()+"---" +myStations.getROWS_DETAIL().get(position).getTime().get(0).getEndtime());
            sm_3_1_time.setText(myStations.getROWS_DETAIL().get(position).getTime().get(1).getStarttime()+"---" +myStations.getROWS_DETAIL().get(position).getTime().get(1).getEndtime());

            return context;
        }

        @Override
        public View getChildView(int position, int position2, boolean b, View context, ViewGroup viewGroup) {
         
           if(context == null){
                context = getLayoutInflater().inflate(R.layout.child_item, null);
            }

            ListView sm_3_child_list = context.findViewById(R.id.sm_3_child_list);
            sm_3_child_list.setAdapter(new ArrayAdapter(getApplicationContext() , android.R.layout.simple_list_item_1 , myStations.getROWS_DETAIL().get(position).getSites()));


            return context;
        }

        @Override
        public boolean isChildSelectable(int i, int i1) {
            return true;
        }
    }

猜你喜欢

转载自blog.csdn.net/weixin_43409994/article/details/103469434