expandablelistview详解

我在项目中使用到expandablelistview,然后我就在网上找了很多关于expandablelistview的文章,那么这里,将一些对去进行总结一些,并将自己踩过的坑填上。

expandablelistview就是类似QQ分组,点击分类,显示其各个详细的分类信息。下面是一些效果图

这样是完成了有父标题,和子标题,实现了分组,接下来看看如何布局的。

<ExpandableListView
    android:id="@+id/exlistview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:drawSelectorOnTop="false"
    android:groupIndicator="@null"
    android:cacheColorHint="@color/colorAccent"/>

看这个属性android:drawSelectorOnTop

android:drawSelectorOnTop="true"  点击某一条记录,颜色会显示在最上面,记录上的文字被遮住,所以点击文字不放,文字就看不到

android:drawSelectorOnTop="false" 点击某条记录不放,颜色会在记录的后面,成为背景色,但是记录内容的文字是可见的

去掉ExpandableListView 默认的箭头 
用到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));    
或xml设置:  
 android:groupIndicator="@drawable/groupIndicator_selector"  



将默认的箭头修改到右边显示: 
1首先ExpandableListViewelistview;  

elistview.setGroupIndicator(null);//将控件默认的左边箭头去掉,

 2在自定义的继承自BaseExpandableListAdapter的adapter中有一个方法。需要在自己的布局文件中添加一个ImageView,然后根据展开或者是关闭来设置不同的箭头的方法。如下代码:

/** * 父类view */ @Override   
public View getGroupView(intgroupPosition, booleanisExpanded, View convertView, ViewGroup parent)  
{ Log.i("zhaoxiong","parent view");   
     LinearLayoutparentLayout=(LinearLayout) View.inflate(context, R.layout.wowocoupons_parent_item, null);   
    TextViewparentTextView=(TextView)parentLayout.findViewById(R.id.parentitem);  
    parentTextView.setText(parentlist.get(groupPosition));   
    ImageViewparentImageViw=(ImageView) parentLayout.findViewById(R.id.arrow);   
    //判断isExpanded就可以控制是按下还是关闭,同时更换图片  
   if(isExpanded){   
       parentImageViw.setBackgroundResource(R.drawable.arrow_down);   
    }else{   
        parentImageViw.setBackgroundResource(R.drawable.arrow_up); }    
     return parentLayout;  
}  


展示Group

有时候,我们需要列表是展开的,默认是关闭的,可以在系统中设置exListView.expandGroup(0);展示第一个父列

如果想要所有的都展示 写下如何代码:

exListView.setAdapter(exlvAdapter);   
//遍历所有group,将所有项设置成默认展开  
 intgroupCount = exListView.getCount();   
for (inti=0; i<groupCount; i++)  
 {   
       exListView.expandGroup(i);  
 };   

同样它也有点击事件,它的点击事件有两种, setOnGroupClickListener()//点击父列表的时候触发该事件

setOnChildClickListener()//点击子列表的时候触发该事件。
不想要系统设置的分割线,设置exListView.setDivider(null);    
属性和方法都介绍了,接下来展示一个例子,这个例子是平常见到的,类似QQ的分组显示。
main.xml文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    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"
    tools:context="com.ice.expandablelistviewdemo.MainActivity">

    <ExpandableListView
        android:id="@+id/exlistview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:drawSelectorOnTop="false"
        android:groupIndicator="@null"
        android:cacheColorHint="@color/colorAccent"/>
</RelativeLayout>
父标题和子项的布局文件,如下:
item_parent.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="70dp"
    android:gravity="center">

    <View
        android:id="@+id/line_view"
        android:layout_width="match_parent"
        android:layout_height="20dp"
        android:background="#D7D7D7"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal"
        android:layout_below="@+id/line_view"
        android:gravity="center">
    <TextView
        android:id="@+id/parent_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="2016年5月"
        android:textSize="18sp"
        android:textColor="#000000"
        android:layout_gravity="center"/>

    <ImageView
        android:id="@+id/parent_image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingLeft="24dp"/>
    </LinearLayout>
</RelativeLayout>

item_child.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <View
        android:id="@+id/line_view1"
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:background="@android:color/darker_gray"/>

    <RelativeLayout
        android:id="@+id/rl_daiyu"
        android:layout_width="match_parent"
        android:layout_height="48dp"
        android:gravity="center_vertical"
        android:layout_below="@+id/line_view1">

        <TextView
            android:id="@+id/daiyu"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="待遇类型"
            android:textSize="16dp"
            android:layout_marginLeft="48dp"/>

        <TextView
            android:id="@+id/daiyu_content"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="失业保险金"
            android:textSize="16dp"
            android:layout_alignParentRight="true"
            android:layout_marginRight="48dp"/>

    </RelativeLayout>

    <View
        android:id="@+id/line_view2"
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="@android:color/darker_gray"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_below="@+id/rl_daiyu"/>

    <RelativeLayout
        android:id="@+id/rl_linqu"
        android:layout_width="match_parent"
        android:layout_height="48dp"
        android:gravity="center_vertical"
        android:layout_below="@+id/line_view2">

        <TextView
            android:id="@+id/linqu"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="待遇领取方式"
            android:textSize="16dp"
            android:layout_marginLeft="48dp"/>

        <TextView
            android:id="@+id/linqu_content"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="定期"
            android:textSize="16dp"
            android:layout_alignTop="@+id/linqu"
            android:layout_alignParentEnd="true"
            android:layout_marginEnd="95dp"/>

    </RelativeLayout>
</RelativeLayout>
接下来看看适配器,该适配器继承了BaseExpandableListAdapter,其中有很多方法,从代码来看更直接。其实可以将其当做两个List,一个标题ListView,还有一个是内容ListView.适配器就是两个BaseAdapter。这样理解后,下面的代码就可以看得更清晰。

public class ExpandListViewAdapter extends BaseExpandableListAdapter{

    private List<String> titleList;

    private List<List<ExpandListBean>> expandList;

    public void setTitleList(List<String> titleList) {
        this.titleList = titleList;
    }

    public void setExpandList(List<List<ExpandListBean>> expandList) {
        this.expandList = expandList;
    }

    private LayoutInflater mInflater;

    public ExpandListViewAdapter(Context context){
        mInflater = LayoutInflater.from(context);
        titleList = new ArrayList<>();
        expandList = new ArrayList<>();
    }

    //---------------Parent-----------------
    @Override
    public int getGroupCount() {
        return titleList.size();
    }
    @Override
    public Object getGroup(int groupPosition) {
        return titleList.get(groupPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }
    //---------------Parent  EDN-----------------

    //---------------Child start-----------------
    @Override
    public int getChildrenCount(int groupPosition) {
        //判断设置的是否有值(这个判断是在网络请求中出现了异常,没有给expandList设置值,如果在执行expandList.get(groupPosition)是会出现异常。)
        if (!expandList.isEmpty()){
            if (expandList.get(groupPosition).isEmpty()){  //有时候该列表是没有值的,没有值的时候,仅让它显示,并且显示的内容可以设置为(暂无数据)
                return 1;
            }else{
                return expandList.get(groupPosition).size();
            }
        }else{
            return 1;
        }
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        if (!expandList.isEmpty()){             //返回子列的值的时候,只有当子列表中设置了值的时候,才有值返回
            if (!expandList.get(groupPosition).isEmpty()){
                return expandList.get(groupPosition).get(childPosition);
            }
        }
        return null;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        if (!expandList.isEmpty()){
            if (!expandList.get(groupPosition).isEmpty()){
                return childPosition;
            }
        }
        return 0;
    }
    //---------------Child END-----------------

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

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        if (convertView == null){
            convertView = mInflater.inflate(R.layout.parent_item,parent,false);
        }

        TextView textView = (TextView) convertView.findViewById(R.id.parent_title);
        ImageView imageView = (ImageView) convertView.findViewById(R.id.parent_image);

        textView.setText(titleList.get(groupPosition));
        if (isExpanded){
            imageView.setBackgroundResource(R.drawable.arrow_bottom);
        }else{
            imageView.setBackgroundResource(R.drawable.arrow_right);
        }

        return convertView;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        //这里相当于ListView中适配器
        ViewHolder viewHolder = null;
        if (convertView == null){
            convertView = mInflater.inflate(R.layout.child_item,parent,false);

            viewHolder = new ViewHolder();
            viewHolder.daiyuContentTV = (TextView) convertView.findViewById(R.id.daiyu_content);
            viewHolder.linquContentTV = (TextView) convertView.findViewById(R.id.linqu_content);

            convertView.setTag(viewHolder);
        }else{
            viewHolder = (ViewHolder) convertView.getTag();
        }

        //设置值,当有数据的时候,就显示值,没有的时候,就设置为(暂无数据)
        if (!expandList.isEmpty() && !expandList.get(groupPosition).isEmpty()){
            viewHolder.daiyuContentTV.setText(expandList.get(groupPosition).get(childPosition).getDaiyu());
            viewHolder.linquContentTV.setText(expandList.get(groupPosition).get(childPosition).getLiqu());

        }else{
            viewHolder.daiyuContentTV.setText("暂无数据");
            viewHolder.linquContentTV.setText("暂无数据");
        }

        return convertView;
    }

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

    private class ViewHolder{
        private TextView daiyuContentTV;
        private TextView linquContentTV;
    }
}

写一个MainActivity,并写上一些测试数据,来让整个程序运行起来。

public class MainActivity extends AppCompatActivity {

    private List<String> titles ;

    private ExpandableListView mListView;

    private ExpandListViewAdapter mExpandListViewAdapter;

    private List<List<ExpandListBean>> expandList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initData();
        initView();
    }

    private void initView(){
        mListView = (ExpandableListView) findViewById(R.id.exlistview);
        mExpandListViewAdapter = new ExpandListViewAdapter(MainActivity.this);
        mExpandListViewAdapter.setTitleList(titles);
        //这里直接设置了值,如果是网络请求,出了设置值外,还需要调用 mExpandListViewAdapter.notifyDataSetChanged();方法来刷新页面
        mExpandListViewAdapter.setExpandList(expandList);

        mListView.setAdapter(mExpandListViewAdapter);
        mListView.setDivider(null);             //设置


        mListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
            @Override
            public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
                //点击效果
                Toast.makeText(MainActivity.this, "点击了", Toast.LENGTH_SHORT).show();
                return false;
            }
        });
    }
    //测试数据的显示
    private void initData(){
        titles = new ArrayList<>();
        titles.add("2016年5月");
        titles.add("2016年4月");
        titles.add("2016年3月");
        titles.add("2016年2月");
        titles.add("2016年1月");
        titles.add("2015年12月");

        expandList = new ArrayList<>();
        for (int i = 0; i < 5;i++){
            List<ExpandListBean> list = new ArrayList<>();
            list.add(new ExpandListBean("失业保险" + i,"定期"));
            expandList.add(list);
        }
        expandList.add(new ArrayList<ExpandListBean>());        //这里设置为空值,子列中将显示为暂无数据
    }
}

运行的效果如下

 









  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值