Android控件二:ExpandableListView可展开的列表组件

效果图:
在这里插入图片描述
代码实现:

  • 1,布局中引入
<ExpandableListView
       android:id="@+id/expandableListView"
       android:layout_width="match_parent"
       android:layout_height="match_parent">
   </ExpandableListView>
  • 2,设置所需bean文件

    public class GroupBean {
         private int img;
         private String txt;
         private List<ChildBean> list_child = new ArrayList<ChildBean>();
         public List<ChildBean> getList_child() {
             return list_child;
         }
    
     public void setList_child(List<ChildBean> list_child) {
         this.list_child = list_child;
     }
    
     public int getImg() {
         return img;
     }
    
     public void setImg(int img) {
         this.img = img;
     }
    
     public String getTxt() {
         return txt;
     }
    
     public void setTxt(String txt) {
         this.txt = txt;
     }    }
    
     public class ChildBean {
           private int img;
           private String txt;
       public int getImg() {
           return img;
       }
       public void setImg(int img) {
           this.img = img;
       }
       public String getTxt() {
           return txt;
       }
       public void setTxt(String txt) {
           this.txt = txt;
       }
  • 3,绑定view和数据
// 准备数据
   private List<GroupBean> list_data;
   private int[] images = { R.drawable.aa, R.drawable.bb, R.drawable.cc };
   
   @Override
   protected void onCreate(@Nullable Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_main2);
       initData();
       initView();
   }

   private void initView() {
       ExpandableListView listView = findViewById(R.id.expandableListView);
       MyExpandableListViewAdapter adapter = new MyExpandableListViewAdapter(this,list_data);
       listView.setAdapter(adapter);
   }

   int[] arr = {R.drawable.a1,R.drawable.a2,R.drawable.a3,R.drawable.a4,R.drawable.a5};
   private void initData() {
       list_data = new ArrayList<GroupBean>();
       for (int i = 0; i < 5; i++) {
           GroupBean group = new GroupBean();
           group.setImg(arr[i]);
           group.setTxt("组" + (i + 1));
           List<ChildBean> child = new ArrayList<ChildBean>();
           for (int j = 0; j < 3; j++) {
               ChildBean chid = new ChildBean();
               chid.setImg(images[j]);
               chid.setTxt("子" + (j + 1));
               child.add(chid);
           }
           group.getList_child().addAll(child);
           list_data.add(group);
       }
   }
4,设置适配器

class MyExpandableListViewAdapter extends BaseExpandableListAdapter{
   private List<GroupBean> list_data;
   private Context context;

   public MyExpandableListViewAdapter(Context context, List<GroupBean> list_data) {
       this.context = context;
       this.list_data = list_data;
   }

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

   @Override
   public int getChildrenCount(int groupPosition) {

       return list_data.get(groupPosition).getList_child().size();
   }

   @Override
   public Object getGroup(int groupPosition) {
       return list_data.get(groupPosition);
   }

   @Override
   public Object getChild(int groupPosition, int childPosition) {
       return list_data.get(groupPosition).getList_child().get(childPosition);
   }

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

   @Override
   public long getChildId(int groupPosition, int childPosition) {
       return childPosition;
   }

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

   @Override
   public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
       HolderGroup group = null;
       if (convertView == null) {
           convertView = View.inflate(context,R.layout.layout_group,null);
           group = new HolderGroup(convertView);
           convertView.setTag(group);
       } else {
           group = (HolderGroup) convertView.getTag();
       }
       group.img.setImageResource(list_data.get(groupPosition).getImg());
       group.tv.setText(list_data.get(groupPosition).getTxt());

       return convertView;
   }

   @Override
   public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView,
                            ViewGroup parent) {
       HolderChild child = null;
       if (convertView == null) {
           convertView = View.inflate(context,R.layout.layout_item,null);
           child = new HolderChild(convertView);
           convertView.setTag(child);
       } else {
           child = (HolderChild) convertView.getTag();
       }
       child.img.setImageResource(list_data.get(groupPosition).getList_child().get(childPosition).getImg());

       child.tv.setText(list_data.get(groupPosition).getList_child().get(childPosition).getTxt());

       return convertView;
   }

   @Override
   public boolean isChildSelectable(int groupPosition, int childPosition) {
       return true;
   }

   class HolderGroup {
       ImageView img;
       TextView tv;

       public HolderGroup(View convertView) {
           img = (ImageView) convertView.findViewById(R.id.iv_group);
           tv = (TextView) convertView.findViewById(R.id.tv_group);
       }
   }

   class HolderChild {
       ImageView img;
       TextView tv;

       public HolderChild(View convertView) {
           img = (ImageView) convertView.findViewById(R.id.iv_item);
           tv = (TextView) convertView.findViewById(R.id.tv_item);
       }
   }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值