实现ExpandableListView 二级列表,点击二级条目状态的改变
2016-04-09 17:17
841 查看
概述
顾名思义,ExpandableListView就是可扩展的ListView,主要用于多级列表,适用于购物车、信息查询、部门信息等场景。重要API:
expandGroup (int groupPos) ;//在分组列表视图中 展开一组 setSelectedGroup (int groupPosition) ;//设置选择指定的组 setSelectedChild (int groupPosition, int childPosition, boolean shouldExpandGroup);//设置选择指定的子项 getPackedPositionGroup (long packedPosition);//返回所选择的组 getPackedPositionForChild (int groupPosition, int childPosition) ; //返回所选择的子项 getPackedPositionType (long packedPosition);//返回所选择项的类型(Child,Group) isGroupExpanded (int groupPosition);//判断此组是否展开 expandableListView.setDivider();这个是设定每个Group之间的分割线。 expandableListView.setGroupIndicator();这个是设定每个Group之前的那个图标 expandableListView.collapseGroup(int group); 将第group组收起
适配器–ExpandableListAdapter
用于设置数据的接口,提供访问Child的数据(由组分类),并实例化Child和Group。expandableListView.setAdapter(new BaseExpandableListAdapter(){}//设置对应的适配器
4个重要函数:
getGroupCount();//设置Group总条目 getChildrenCount(int groupPosition);//设置Children总条目 getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent);//设置Group数据与视图绑定 getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent); //设置Child数据与视图绑定
具体使用
布局:<?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" android:orientation="vertical" tools:context="com.blueocean2016.skycn.expandablelistview.MainActivity"> <TextView android:layout_width="match_parent" android:background="#3192a6" android:textColor="@android:color/white" android:textSize="20sp" android:gravity="center" android:layout_height="35dp" android:text="信息查询"/> <ExpandableListView android:id="@+id/expand" android:layout_width="match_parent" android:layout_height="match_parent"> </ExpandableListView> </LinearLayout>
逻辑代码:
package com.blueocean2016.skycn.expandablelistview; import android.app.Activity; import android.content.Intent; import android.graphics.Color; import android.net.Uri; import android.os.Bundle; import android.view.Gravity; import android.view.View; import android.view.ViewGroup; import android.widget.BaseExpandableListAdapter; import android.widget.ExpandableListView; import android.widget.TextView; import java.util.ArrayList; import java.util.List; public class MainActivity extends Activity implements ExpandableListView.OnChildClickListener, ExpandableListView.OnGroupClickListener { private List<String> mGroupData; private List<String> mChildData; private ExpandableListView mExlistview; private int mCurrentGroupPosition = -1; //默认全关闭 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mExlistview = (ExpandableListView) findViewById(R.id.expand); mExlistview.setOnChildClickListener(this); mExlistview.setOnGroupClickListener(this); initData(); } private void initData() { mGroupData = new ArrayList<>(); mChildData = new ArrayList<>(); //模拟一级条目数据 for (int i = 0; i < 50; i++) { mGroupData.add("一级条目" + i); } //模拟二级条目数据 for (int i = 0; i <5 ; i++) { mChildData.add("2254655" + i); } //设置适配器 mExlistview.setAdapter(new BaseExpandableListAdapter() { @Override public int getGroupCount() { return mGroupData.size(); } @Override public int getChildrenCount(int groupPosition) { return mChildData.size(); } @Override public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { //TextView与数据绑定 String text = mGroupData.get(groupPosition); TextView tv1 = new TextView(MainActivity.this); tv1.setText(text); tv1.setPadding(40, 0, 0, 0); tv1.setTextColor(Color.RED); tv1.setTextSize(16); return tv1; } @Override public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { //TextView与数据绑定 String text = mChildData.get(childPosition); TextView tv2 = new TextView(MainActivity.this); tv2.setPadding(40, 0, 0, 0); tv2.setText(text); tv2.setTextSize(16); return tv2; } /*---------------------其他暂时不关注---------------------*/ @Override public Object getGroup(int groupPosition) { return null; } @Override public Object getChild(int groupPosition, int childPosition) { return null; } @Override public long getGroupId(int groupPosition) { return 0; } @Override public long getChildId(int groupPosition, int childPosition) { return 0; } @Override public boolean hasStableIds() { return false; } /** * 如果需要child条目获取点击事件, 需要设置为true * @param groupPosition * @param childPosition * @return */ @Override public boolean isChildSelectable(int groupPosition, int childPosition) { return true; } }); } /** * 设置child条目点击拨打电话 * @param parent * @param v * @param groupPosition * @param childPosition * @param id * @return */ @Override public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) { String number = mChildData.get(childPosition); Intent intent = new Intent(); //指定拨打电话的动作 intent.setActio 9c48 n(Intent.ACTION_CALL); //获取号码 intent.setData(Uri.parse("tel://" + number)); startActivity(intent); //消费事件 return true; } /** * 设置Group点击状态的改变 * @param parent * @param v * @param groupPosition * @param id * @return */ @Override public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) { if(mCurrentGroupPosition == -1){ //等于-1 --> 没有任何组打开 //展开组 mExlistview.expandGroup(groupPosition); //设置第一个位置是那个组 mExlistview.setSelectedGroup(groupPosition); //重新对记录进行赋值 mCurrentGroupPosition = groupPosition; }else{ //不等于-1 --> 代表以前有组打开 if (groupPosition == mCurrentGroupPosition){ //如果当前点击的是以前打开过的组,那么就关闭它 mExlistview.collapseGroup(groupPosition); //记录回到最初 mCurrentGroupPosition = -1; }else{ //如果当前点击的不是以前打开过的组,而是其他组 //关闭以前的组 mExlistview.collapseGroup(mCurrentGroupPosition); //展开当前点击的组 mExlistview.expandGroup(groupPosition); //设置第一个位置是那个组 mExlistview.setSelectedGroup(groupPosition); //重新对记录进行赋值 mCurrentGroupPosition = groupPosition; } } //消费事件 return true; } }
关注点
设置点击监听时,返回值必须为true,否则不会消费事件。Child条目的点击监听还需要在适配器isChildSelectable函数中设置返回值为true。如果不设置Pading,文本会覆盖箭头。
运行效果图
本篇播客到此结束,欢迎大家多批评和指定,我将持续不断的刷新!
相关文章推荐
- Android Manifest 用法
- 什么是 GraphQL?
- 完美实现Android ListView中的TextView的跑马灯效果
- Spark RDD API详解(一) Map和Reduce
- Spring Boot 开发微服务
- android上改变listView的选中颜色
- lwn拾遗:[sn3218 led drivers]-api解释-1
- 一个小型js框架myJSFrame附API使用帮助
- Delphi7中Listview的常用功能汇总
- Delphi控件ListView的属性及使用方法详解
- 详细分析交换机、路由器、集线器的区别和联系
- PowerShell打开或关闭光驱
- 批处理的api WMIC学习体会有感第1/2页
- 批处理 API实现文件下载的代码第1/2页
- Lua教程(十七):C API简介
- 强制删除工具 xdelbox xdelbox1.5正式版下载
- 揪出交换机端口背后“凶手”导致网速太慢
- 电脑重启后突然检测不到硬盘的原因分析与解决办法
- C#中设计、使用Fluent API
- Google官方支持的NodeJS访问API,提供后台登录授权