今天在项目中用到 Group 中含有 CheckBox Child 中也含有 CheckBox 并且 当Group 选中时 Child 也全部选中 如图 :
当然之前也在项目中写过,但是为了防止忘记 在简书记载一下,直接上代码。
EListAdapter.java
package text.com.myapplication;
/**
* Created by Administrator on 2018/05/09.
*/
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.CheckBox;
import android.widget.ExpandableListView;
import android.widget.TextView;
import java.util.List;
public class EListAdapterextends BaseExpandableListAdapterimplements ExpandableListView.OnChildClickListener {
private Contextcontext;
private Listgroups;
private ExpandableListViewlistView;
public EListAdapter(Context context, List groups, ExpandableListView listView) {
this.context = context;
this.groups = groups;
this.listView = listView;
}
public ObjectgetChild(int groupPosition, int childPosition) {
return groups.get(groupPosition).getChildren();
}
/**
* 获取 选中状态的数据
*
* @return
*/
public ListgetData() {
return groups;
}
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
public int getChildrenCount(int groupPosition) {
return groups.get(groupPosition).getChildren().size();
}
public ObjectgetGroup(int groupPosition) {
return groups.get(groupPosition);
}
public int getGroupCount() {
return groups.size();
}
public long getGroupId(int groupPosition) {
return groupPosition;
}
public boolean hasStableIds() {
return true;
}
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
/**
* 設定 Group 資料
*/
public ViewgetGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
Group group = (Group) getGroup(groupPosition);
if (convertView ==null) {
LayoutInflater infalInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.group_layout, null);
}
TextView tv = (TextView) convertView.findViewById(R.id.tvGroup);
tv.setText(group.getTitle());
// 重新產生 CheckBox 時,將存起來的 isChecked 狀態重新設定
CheckBox checkBox = (CheckBox) convertView.findViewById(R.id.chbGroup);
checkBox.setChecked(group.isChecked());
// 點擊 CheckBox 時,將狀態存起來
checkBox.setOnClickListener(new Group_CheckBox_Click(groupPosition));
return convertView;
}
/**
* 勾選 Group CheckBox 時,存 Group CheckBox 的狀態,以及改變 Child CheckBox 的狀態
*/
class Group_CheckBox_Clickimplements OnClickListener {
private int groupPosition;
Group_CheckBox_Click(int groupPosition) {
this.groupPosition= groupPosition;
}
public void onClick(View v) {
int childrenCount =groups.get(groupPosition).getChildren().size();
// 在 对 Group 点击数据进行处理时 先判断 现在的状态(isChecked) 如果已经勾选 则取消
if (groups.get(groupPosition).isChecked()){
groups.get(groupPosition).setChecked(false);
listView.collapseGroup(groupPosition);
}
else {
groups.get(groupPosition).setChecked(true);
listView.expandGroup(groupPosition);
}
boolean groupIsChecked =groups.get(groupPosition).isChecked();
// 將 Children 的 isChecked 全面設成跟 Group 一樣
for (int i =0; i < childrenCount; i++)
groups.get(groupPosition).getChildren().get(i).setChecked(groupIsChecked);
// 注意,一定要通知 ExpandableListView 资料已经变更 ExpandableListView需要刷新
notifyDataSetChanged();
}
}
/**
* 設定 Children 資料
*/
public ViewgetChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
Child child =groups.get(groupPosition).getChildren().get(childPosition);
if (convertView ==null) {
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.child_layout, null);
}
TextView tv = (TextView) convertView.findViewById(R.id.tvChild);
tv.setText(child.getUsername());
// 重新產生 CheckBox 時,將存起來的 isChecked 狀態重新設定
CheckBox checkBox = (CheckBox) convertView.findViewById(R.id.chbChild);
checkBox.setChecked(child.isChecked());
// 點擊 CheckBox 時,將狀態存起來
checkBox.setOnClickListener(new Child_CheckBox_Click(groupPosition, childPosition));
return convertView;
}
/**
* 勾選 Child CheckBox 時,存 Child CheckBox 的狀態
*/
class Child_CheckBox_Clickimplements OnClickListener {
private int groupPosition;
private int childPosition;
Child_CheckBox_Click(int groupPosition, int childPosition) {
this.groupPosition = groupPosition;
this.childPosition = childPosition;
}
public void onClick(View v) {
handleClick(childPosition, groupPosition);
}
}
public void handleClick(int childPosition, int groupPosition) {
int childrenCount =groups.get(groupPosition).getChildren().size();
// 在 Group 的 Child 点击数据进行处理时 先判断 现在的状态(isChecked) 如果已经勾选 则取消
if (groups.get(groupPosition).getChildren().get(childPosition).isChecked())
groups.get(groupPosition).getChildren().get(childPosition).setChecked(false);
else groups.get(groupPosition).getChildren().get(childPosition).setChecked(true);
// 檢查 Child CheckBox 是否有全部勾選,以控制 Group CheckBox 默认Group为勾选状态
// 当遍历Group 自数据发现有 未勾选数据时 返回 fale
boolean childrenAllIsChecked =true;
for (int i =0; i < childrenCount; i++) {
// 主意 if (!groups.get(groupPosition).getChildren().get(i).isChecked())
// 是 i 不是 childPosition
if (!groups.get(groupPosition).getChildren().get(i).isChecked())
childrenAllIsChecked =false;
}
groups.get(groupPosition).setChecked(childrenAllIsChecked);
// 注意,一定要通知 ExpandableListView 资料已经变更 ExpandableListView需要刷新
notifyDataSetChanged();
}
@Override
public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
handleClick(childPosition, groupPosition);
return true;
}
}
Group.java
package text.com.myapplication;
/**
* Created by Administrator on 2018/05/09.
*/
import java.util.ArrayList;
public class Group {
private Stringtitle;
private ArrayListchildren;
private boolean isChecked=false;
public StringgetTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public ArrayListgetChildren() {
return children;
}
public void setChildren(ArrayList children) {
this.children = children;
}
public boolean isChecked() {
return isChecked;
}
public void setChecked(boolean checked) {
isChecked = checked;
}
}
Child.java
package text.com.myapplication;
/**
* Created by Administrator on 2018/05/09.
*/
public class Child {
private Stringusername;
private boolean isChecked=false;
public StringgetUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public boolean isChecked() {
return isChecked;
}
public void setChecked(boolean checked) {
isChecked = checked;
}
}
MainActivity.java
package text.com.myapplication;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ExpandableListView;
import java.util.ArrayList;
import java.util.List;
public class MainActivityextends AppCompatActivity {
ExpandableListViewlistView;
EListAdapteradapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ExpandableListView) findViewById(R.id.listView);
adapter =new EListAdapter(MainActivity.this, getData(),listView);
listView.setAdapter(adapter);
listView.setOnChildClickListener(adapter);
}
private ListgetData() {
List groups =new ArrayList<>();
for (int i =0; i <15; i++) {
List children =new ArrayList<>();
text.com.myapplication.Group group =new Group();
group.setTitle("group" + i);
for (int j =0; j <10; j++) {
Child child =new Child();
child.setUsername("child" + j);
children.add(child);
}
group.setChildren((ArrayList) children);
groups.add(group);
}
return groups;
}
}
activity_main.xml
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
group_layout.xml
android:layout_width="match_parent"
android:layout_height="45dp"
android:orientation="horizontal">
android:id="@+id/tvGroup"
android:layout_width="wrap_content"
android:layout_height="45dip"
android:layout_marginLeft="30dp"
android:gravity="center_vertical|left"
android:textSize="17dip"
android:textStyle="bold">
android:id="@+id/chbGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerHorizontal="true"
android:layout_marginRight="10dp"
android:focusable="false" />
child_layout.xml
android:layout_width="match_parent"
android:layout_height="45dip"
android:background="#5f6c68"
android:orientation="horizontal">
android:id="@+id/tvChild"
android:layout_width="wrap_content"
android:layout_height="45dip"
android:layout_marginLeft="35dp"
android:gravity="center_vertical"
android:textSize="17dip"
android:textStyle="bold">
android:id="@+id/chbChild"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerHorizontal="true"
android:layout_marginRight="10dp"
android:clickable="false"
android:focusable="false" />