ExpandableListView的简单使用

1,xml中引入布局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:layout_width="match_parent"
    android:layout_height="match_parent">
    <ExpandableListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/elv_main"></ExpandableListView>
</LinearLayout>

2,Adapter的编写,继承BaseExpandableListAdapter并实现抽象方法,并对应各个方法填充对应参数。注意一般前面一个id为parentId,后面一个为childId

public class MyExpandableListViewAdapter extends BaseExpandableListAdapter{
    private Map<String,List<String>>data=new HashMap<>();
    private String[] parentList=new String[]{};
    private Context context;
    public MyExpandableListViewAdapter(Context context, Map<String, List<String>> data, String[] parentList) {
        this.context=context;
        this.data=data;
        this.parentList=parentList;
    }
    //  获得父项的数量
    @Override
    public int getGroupCount() {
        return data.size();
    }
    //  获得某个父项的子项数目
    @Override
    public int getChildrenCount(int parentPos) {
        return data.get(parentList[parentPos]).size();
    }
    //  获得某个父项
    @Override
    public Object getGroup(int parentPos) {
        return data.get(parentList[parentPos]);
    }
    //  获得某个父项的某个子项
    @Override
    public Object getChild(int parentPos, int childPos) {
        return data.get(parentList[parentPos]).get(childPos);
    }
    //  获得某个父项的id
    @Override
    public long getGroupId(int parentPos) {
        return parentPos;
    }
    //  获得某个父项的某个子项的id
    @Override
    public long getChildId(int parentPos, int childPos) {
        return childPos;
    }
    //  按函数的名字来理解应该是是否具有稳定的id,这个函数目前一直都是返回false,没有去改动过
    @Override
    public boolean hasStableIds() {
        return false;
    }
    //  获得父项显示的view
    @Override
    public View getGroupView(int parentPos, boolean b, View view, ViewGroup viewGroup) {
        if (view==null){
            LayoutInflater inflater= (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view=inflater.inflate(R.layout.parent_item,null);
        }
        view.setTag(R.layout.parent_item,parentPos);
        view.setTag(R.layout.child_item,-1);
        TextView text = (TextView) view.findViewById(R.id.parent_title);
        text.setText(parentList[parentPos]);
        return view;
    }

    @Override
    public View getChildView(int parentPos, int childPos, boolean b, View view, ViewGroup viewGroup) {
        if (view == null) {
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = inflater.inflate(R.layout.child_item, null);
        }
        view.setTag(R.layout.parent_item, parentPos);
        view.setTag(R.layout.child_item, childPos);
        TextView text = (TextView) view.findViewById(R.id.child_title);
        text.setText(data.get(parentList[parentPos]).get(childPos));
        text.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(context, "点到了内置的textview", Toast.LENGTH_SHORT).show();
            }
        });
        return view;
    }
    //  子项是否可选中,如果需要设置子项的点击事件,需要返回true
    @Override
    public boolean isChildSelectable(int i, int i1) {
        return true;
    }
}
3,Activity中引用

public class MainActivity extends AppCompatActivity {
    private ExpandableListView elv_main;
    private MyExpandableListViewAdapter adapter;
    private Map<String,List<String>> data=new HashMap<>();
    private String[] parentList=new String[]{"first","second","third"};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        elv_main=findViewById(R.id.elv_main);
        initData();
        adapter=new MyExpandableListViewAdapter(this,data,parentList);
        elv_main.setAdapter(adapter);
    }

    private void initData() {
        for (int i=0;i<parentList.length;i++){
            List<String> list=new ArrayList<>();
            for (int j=0;j<8;j++){
                list.add("item"+j);
            }
            data.put(parentList[i],list);
        }
    }
}
到这里就没了,很简单


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
好的,您想了解安卓开发 ExpandableListView使用示例,以下是一个简单的示例: 1. 定义适配器类 ``` public class ExpandableListAdapter extends BaseExpandableListAdapter { private Context mContext; private List<String> mGroupTitles; // 父分组数据 private HashMap<String, List<String>> mChildData; // 子分组数据 public ExpandableListAdapter(Context context, List<String> groupTitles, HashMap<String, List<String>> childData) { mContext = context; mGroupTitles = groupTitles; mChildData = childData; } @Override public int getGroupCount() { return mGroupTitles.size(); } @Override public int getChildrenCount(int groupPosition) { return mChildData.get(mGroupTitles.get(groupPosition)).size(); } @Override public Object getGroup(int groupPosition) { return mGroupTitles.get(groupPosition); } @Override public Object getChild(int groupPosition, int childPosition) { return mChildData.get(mGroupTitles.get(groupPosition)).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) { LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); View view = inflater.inflate(R.layout.list_item_group, null); TextView title = (TextView) view.findViewById(R.id.group_title); title.setText(mGroupTitles.get(groupPosition)); return view; } @Override public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); View view = inflater.inflate(R.layout.list_item_child, null); TextView title = (TextView) view.findViewById(R.id.child_title); title.setText(mChildData.get(mGroupTitles.get(groupPosition)).get(childPosition)); return view; } @Override public boolean isChildSelectable(int groupPosition, int childPosition) { return true; } } ``` 2. 定义布局文件 list_item_group.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="wrap_content" android:orientation="vertical" android:paddingLeft="40dp" android:paddingRight="40dp" android:paddingTop="20dp" android:paddingBottom="20dp"> <TextView android:id="@+id/group_title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="18sp" /> </LinearLayout> ``` list_item_child.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="wrap_content" android:orientation="vertical" android:paddingLeft="40dp" android:paddingRight="40dp" android:paddingTop="15dp" android:paddingBottom="15dp"> <TextView android:id="@+id/child_title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="16sp" /> </LinearLayout> ``` 3. 在 Activity 中使用 ExpandableListView ``` public class MainActivity extends AppCompatActivity { private ExpandableListView mExpandableListView; private ExpandableListAdapter mAdapter; private List<String> mGroupTitles; private HashMap<String, List<String>> mChildData; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 初始化数据 initData(); // 初始化 ExpandableListView mExpandableListView = (ExpandableListView) findViewById(R.id.expandable_list_view); mAdapter = new ExpandableListAdapter(this, mGroupTitles, mChildData); mExpandableListView.setAdapter(mAdapter); // 设置子项点击监听器 mExpandableListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() { @Override public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) { Toast.makeText(MainActivity.this, "您点击了" + mChildData.get(mGroupTitles.get(groupPosition)).get(childPosition), Toast.LENGTH_SHORT).show(); return true; } }); } private void initData() { mGroupTitles = new ArrayList<>(); mGroupTitles.add("计算机"); mGroupTitles.add("自然科学"); mGroupTitles.add("哲学"); List<String> computers = new ArrayList<>(); computers.add("Java"); computers.add("Python"); computers.add("C++"); computers.add("PHP"); computers.add("JavaScript"); computers.add("HTML/CSS"); computers.add("SQL"); List<String> sciences = new ArrayList<>(); sciences.add("物理"); sciences.add("化学"); sciences.add("生物"); sciences.add("地理"); sciences.add("天文"); List<String> philosophies = new ArrayList<>(); philosophies.add("伦理学"); philosophies.add("形而上学"); philosophies.add("认识论"); philosophies.add("逻辑学"); philosophies.add("美学"); mChildData = new HashMap<>(); mChildData.put(mGroupTitles.get(0), computers); mChildData.put(mGroupTitles.get(1), sciences); mChildData.put(mGroupTitles.get(2), philosophies); } } ``` 这样,您就可以展示一个带父分组和子分组的 ExpandableListView ,同时设置子项点击监听器,实现相应的功能了。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

流星雨在线

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值