最近项目中遇到这类需求 可以多选全选 还可以展开和闭合 ,当然作为安卓开发人员首先就想到 ExpandableListView
看到这个布局首先想到分割线 ,首先不让关闭自身的分割线 把分割线单独写到每个Item里面
话不多说 上代码
item_group_expan.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:background="#F5F6FA" android:orientation="horizontal"> <!--android:dividerHeight="@dimen/px30"--> <!--android:childDivider="#ffffff"--> <LinearLayout android:layout_marginTop="@dimen/px30" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#ffffff" android:orientation="horizontal" android:paddingBottom="@dimen/px10" android:paddingTop="@dimen/px10"> <ImageView android:id="@+id/image" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:layout_marginLeft="@dimen/px30" android:src="@drawable/hot_normor" /> <TextView android:id="@+id/textview1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_centerVertical="true" android:layout_weight="1" android:padding="@dimen/px20" /> <TextView android:id="@+id/tv_quan" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:layout_marginRight="@dimen/px30" android:text="全选" /> <ImageView android:id="@+id/imageview1" android:layout_width="@dimen/px50" android:layout_height="@dimen/px50" android:layout_gravity="center_vertical" android:layout_marginRight="@dimen/px30" android:background="@drawable/check_selecter" /> </LinearLayout> </LinearLayout>
item_expan.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:background="#ffffff" android:orientation="vertical"> <View android:layout_width="match_parent" android:layout_height="@dimen/px2" android:layout_marginLeft="@dimen/font30" android:background="#E4E6ED" /> <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:paddingBottom="@dimen/px10" android:paddingTop="@dimen/px10"> <TextView android:id="@+id/textview1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_centerVertical="true" android:padding="10dip" /> <ImageView android:id="@+id/imageview1" android:layout_width="25dp" android:layout_height="25dp" android:layout_alignParentRight="true" android:layout_centerVertical="true" android:layout_marginRight="15dp" android:background="@drawable/check_selecter" /> <CheckBox android:id="@+id/check" android:layout_width="25dp" android:layout_height="25dp" android:layout_alignParentRight="true" android:layout_centerVertical="true" android:layout_marginRight="15dp" android:background="@drawable/check_selecter" android:button="@null" android:visibility="gone" /> </RelativeLayout> </LinearLayout>
主要实现方法 里面注视挺详细 的 就不做过多的说明了
package com.example.administrator.exlistviewgpf; import android.app.Activity; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.ExpandableListView; import java.util.ArrayList; import java.util.List; public class MainActivity extends Activity implements MyExpandableListAdapter.ExpandableListListener{ ExpandableListView exlistview; private MyExpandableListAdapter adapter; /** * 父数据源 */ private List<QudaoBean> parents; /** * 子数据源 */ private List<List<QudaoBean>> childs; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); exlistview= (ExpandableListView) findViewById(R.id.exlistview); parents = new ArrayList<QudaoBean>(); parents.add(new QudaoBean("地推活动", false)); parents.add(new QudaoBean("网络渠道1", false)); parents.add(new QudaoBean("网络渠道2", false)); parents.add(new QudaoBean("网络渠道3", false)); childs = new ArrayList<List<QudaoBean>>(); List<QudaoBean> c = new ArrayList<QudaoBean>(); c.add(new QudaoBean("小区推广", false)); c.add(new QudaoBean("学校推广", false)); childs.add(c); List<QudaoBean> c1 = new ArrayList<QudaoBean>(); c1.add(new QudaoBean("微信平台", false)); c1.add(new QudaoBean("机构官网", false)); c1.add(new QudaoBean("新媒体", false)); childs.add(c1); List<QudaoBean> c2 = new ArrayList<QudaoBean>(); c2.add(new QudaoBean("微信平台", false)); c2.add(new QudaoBean("机构官网", false)); c2.add(new QudaoBean("新媒体", false)); childs.add(c2); List<QudaoBean> c3 = new ArrayList<QudaoBean>(); c3.add(new QudaoBean("微信平台", false)); c3.add(new QudaoBean("机构官网", false)); c3.add(new QudaoBean("新媒体", false)); childs.add(c3); adapter = new MyExpandableListAdapter(this, parents, childs); adapter.setExpandableListListener(this); exlistview.setAdapter(adapter); /** 设置展开的监听事件 */ exlistview.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() { @Override public void onGroupExpand(int groupPosition) { /** 循环遍历,把不是当前选中的其他组关闭 */ // for (int i = 0; i < parents.size(); i++) { // if (i != groupPosition) { // exlistview.collapseGroup(i); // } // } } }); /** 默认选中第一组 */ for(int i=0;i<parents.size();i++){ exlistview.expandGroup(i);} /** 去除每组对应的显示效果,默认是有箭头的,这里我们需要自己的效果,就把默认的去除掉 */ exlistview.setGroupIndicator(null); /** 设置子item的单击事件 */ exlistview.setOnChildClickListener(new ExpandableListView.OnChildClickListener() { @Override public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) { return false; } }); } /** * 在适配器里面写的Child点击事件的接口 * */ @Override public void ChildListener(int groupPosition, int childPosition) { if(childs.get(groupPosition).get(childPosition).isCheck()){ childs.get(groupPosition).get(childPosition).setCheck(false); }else { childs.get(groupPosition).get(childPosition).setCheck(true); } parents.get(groupPosition).setCheck(true); for(int i=0;i<childs.get(groupPosition).size();i++){ if(!childs.get(groupPosition).get(i).isCheck()){ parents.get(groupPosition).setCheck(false); break; } } adapter.notifyDataSetChanged(); } /** * 在适配器里面写的Group点击事件的接口 * */ @Override public void GroupListener(int groupPosition) { if(parents.get(groupPosition).isCheck()){ parents.get(groupPosition).setCheck(false); for(int i=0;i<childs.get(groupPosition).size();i++){ childs.get(groupPosition).get(i).setCheck(false); } }else { for(int i=0;i<childs.get(groupPosition).size();i++){ childs.get(groupPosition).get(i).setCheck(true); } parents.get(groupPosition).setCheck(true); } adapter.notifyDataSetChanged(); } }适配器
在适配器定义ExpandableListListener接口
有两个方法
/** * Child点击事件 * */ void ChildListener(int groupPosition, int childPosition); /** * Group点击事件 * */ void GroupListener(int groupPosition);
public class MyExpandableListAdapter extends BaseExpandableListAdapter { /** * 父数据源 */ private List<QudaoBean> parents; /** * 子数据源 */ private List<List<QudaoBean>> childs; private Context context; public MyExpandableListAdapter(Context context, List<QudaoBean> parents, List<List<QudaoBean>> childs) { this.parents = parents; this.childs = childs; this.context = context; } @Override public int getGroupCount() { return parents.size(); } @Override public int getChildrenCount(int groupPosition) { return childs.get(groupPosition).size(); } @Override public Object getGroup(int groupPosition) { return parents.get(groupPosition); } @Override public Object getChild(int groupPosition, int childPosition) { return childs.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 false; } @Override public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { if (convertView == null) { convertView = LayoutInflater.from(context).inflate( R.layout.item_group_expan, null); } TextView tv = (TextView) convertView.findViewById(R.id.textview1); TextView qu = (TextView) convertView.findViewById(R.id.tv_quan); ImageView iv = (ImageView) convertView .findViewById(R.id.imageview1); tv.setText(parents.get(groupPosition).getName()); ImageView imageView = (ImageView) convertView .findViewById(R.id.image); tv.setText(parents.get(groupPosition).getName()); iv.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { expandableListListener.GroupListener(groupPosition); } }); /** 判断当前是展开还是闭合,为了显示不同的箭头 */ if (isExpanded) { iv.setVisibility(View.VISIBLE); qu.setVisibility(View.VISIBLE); imageView.setImageResource(R.drawable.hot_iconq); } else { iv.setVisibility(View.GONE); qu.setVisibility(View.GONE); imageView.setImageResource(R.drawable.hot_normor); } if (parents.get(groupPosition).isCheck()) { iv.setImageResource(R.drawable.icon_xuanzhong); } else { iv.setImageResource(R.drawable.icon_unxuanzhong); } return convertView; } @Override public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { if (convertView == null) { convertView = LayoutInflater.from(context).inflate( R.layout.item_expan, null); } ImageView iv = (ImageView) convertView .findViewById(R.id.imageview1); TextView tv = (TextView) convertView.findViewById(R.id.textview1); tv.setText(childs.get(groupPosition).get(childPosition).getName()); iv.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { expandableListListener.ChildListener(groupPosition, childPosition); } }); if (childs.get(groupPosition).get(childPosition).isCheck()) { iv.setImageResource(R.drawable.icon_xuanzhong); } else { iv.setImageResource(R.drawable.icon_unxuanzhong); } return convertView; } @Override public boolean isChildSelectable(int groupPosition, int childPosition) { /** 注意这里必须返回true 不然点击不到子item */ return true; } public interface ExpandableListListener { void ChildListener(int groupPosition, int childPosition); void GroupListener(int groupPosition); } private ExpandableListListener expandableListListener; public void setExpandableListListener(ExpandableListListener expandableListListener) { this.expandableListListener = expandableListListener; }
demo链接:http://download.youkuaiyun.com/download/u012941592/10027809