zoukankan      html  css  js  c++  java
  • Android笔记之ExpandableListView

    1、布局文件:

    (1)主布局:放置一个ExpandableListView组件

    (2)每一行的Item布局,child_listitem.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal" >
    
        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    
        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TextView" />
    
    </LinearLayout>

    2、代码清单:

    主代码:

    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    
    import android.os.Bundle;
    import android.support.v4.app.Fragment;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.ExpandableListView;
    
    public class fragment_2 extends Fragment {
    
        MyExAdapter myadapter;// 自定义的BaseExpandableListAdapter
        ExpandableListView exList;// 可扩展的ListView
        List<Map<String, Object>> groupData = null;// 这里用Object泛型,可能会有图片数据
        List<List<Map<String, Object>>> childData = null;
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            View v = inflater.inflate(R.layout.fragment_2, container, false);
            exList = (ExpandableListView) v.findViewById(R.id.list);
            myadapter = new MyExAdapter(getActivity());// 实例化适配器,传context参数
            childData = new ArrayList<List<Map<String, Object>>>();// 初始化子列表的数据集
            groupData = new ArrayList<Map<String, Object>>();// 初始化父列表的数据集

    //这个for循环的3为大组的数量
    for (int i = 0; i < 3; i++) { // 每个list是一个组的多个行的内容,这里有3个组 List<Map<String, Object>> list = new ArrayList<Map<String, Object>>(); // 每一个map是一个子列表的一行的内容(包括文本,图标) for (int j = 0; j < 5; j++) { Map<String, Object> map1 = new HashMap<String, Object>(); map1.put("title", "路人" + j); map1.put("icon", this.getResources().getDrawable(R.drawable.ic_launcher)); list.add(map1); } childData.add(list); } String[] strings = { "常用联系人", "我的同学", "我的老师" }; for (String string : strings) { // 开始填充父列表数据 // 每一个map是一个父列表的一行的内容 Map<String, Object> map = new HashMap<String, Object>(); map.put("title", string); groupData.add(map); } // childData.size()=groupData.size() myadapter.setChildData(childData); myadapter.setGroupData(groupData); exList.setAdapter(myadapter); exList.setGroupIndicator(null);// 不设置大组指示器图标,因为我们自定义设置了 exList.setDivider(null);// 设置图片可拉伸的 return v; } }

    适配器MyExAdapter

    用到ExpandableListView时有个箭头图标系统自带的在你自定义布局也不能去掉只要设置一个属性即可,如下:

      settingLists.setGroupIndicator(null);  ~~~~~~~~~~~~~~~~~此处就是设置自定义的箭头图标的。置空则没有了。

    也可以自定义(但是位置还是在那个地方不推荐)如下:

    首先,自定义一个expandablelistviewselector.xml文件,具体内容如下:
    Java代码

    <?xml version="1.0" encoding="utf-8"?>   

    <selector xmlns:android="http://schemas.android.com/apk/res/android">   

        <item android:state_expanded="true" android:drawable="@drawable/expandablelistviewindicatordown" />   

        <item android:drawable="@drawable/expandablelistviewindicator" />   

    </selector>  

    加一句代码如下:

    settingLists.setGroupIndicator(this.getResources().getDrawable(R.layout.expandablelistviewselector)); 

    大功告成

    To be continued!

  • 相关阅读:
    精讲响应式WebClient第6篇-请求失败自动重试机制,强烈建议你看一看
    dubbo实战之三:使用Zookeeper注册中心
    dubbo实战之二:与SpringBoot集成
    dubbo实战之一:准备和初体验
    树莓派4B安装官方Ubuntu20 Server版(64位)
    JUnit5学习之八:综合进阶(终篇)
    纯函数
    函数式编程基本概念
    vscod使用指南
    实际使用Elasticdump工具对Elasticsearch集群进行数据备份和数据还原
  • 原文地址:https://www.cnblogs.com/xingyyy/p/3389611.html
Copyright © 2011-2022 走看看