Android ExpandableListView單選以及多選實(shí)現(xiàn)代碼
一、概述
ExpandableListView是常用的一個(gè)控件,今天自己做了個(gè)小練習(xí),主要需求是單選以及多選的實(shí)現(xiàn),看似比較簡(jiǎn)單,但是還是比較復(fù)雜,把代碼貼給大家,有這種需求的可以參考一下。
二、效果截圖

三、實(shí)現(xiàn)過(guò)程
activity_main.xml
<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=".MainActivity" >
<ExpandableListView
android:id="@+id/exlistview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:listSelector="@android:color/transparent" >
</ExpandableListView>
</LinearLayout>
group_item.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"
android:descendantFocusability="blocksDescendants"
android:padding="10dp" >
<TextView
android:id="@+id/id_group_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:padding="10dp"
android:text="hao"
android:textColor="@android:color/black"
android:textIsSelectable="true"
android:textSize="15sp" >
</TextView>
<CheckBox
android:id="@+id/id_group_checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true" />
</RelativeLayout>
listview_item.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"
android:descendantFocusability="blocksDescendants"
android:padding="10dp" >
<TextView
android:id="@+id/id_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:padding="10dp"
android:layout_marginLeft="30dp"
android:textColor="#55acac"
android:textIsSelectable="true"
android:textSize="15sp" >
</TextView>
<CheckBox
android:id="@+id/id_checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="false"
android:layout_alignParentRight="true"
android:layout_centerVertical="true" />
</RelativeLayout>
MainAcitivity.java
public class MainActivity extends Activity {
private List<Map<String, String>> parentList = new ArrayList<Map<String, String>>();
private List<List<Map<String, String>>> childData = new ArrayList<List<Map<String, String>>>();
private ExpandableListView exListView;
private Context context = this;
private MyAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
initData();
setListener();
}
/**
* 記錄正在選中的子listview的item條目 用hashset是為了去除重復(fù)值
*/
private HashSet<String> hashSet;
private void setListener()
{
exListView.setOnGroupExpandListener(new OnGroupExpandListener()
{
@Override
public void onGroupExpand(int groupPosition)
{
//存取已選定的集合
hashSet = new HashSet<String>();
}
});
// ExpandableListView的Group的點(diǎn)擊事件
exListView.setOnGroupClickListener(new OnGroupClickListener()
{
@Override
public boolean onGroupClick(ExpandableListView parent, View v,
int groupPosition, long id)
{
// 可以寫(xiě)點(diǎn)擊后實(shí)現(xiàn)的功能
return false;
}
});
// ExpandableListView的child的點(diǎn)擊事件
exListView.setOnChildClickListener(new OnChildClickListener()
{
@Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id)
{
Map<String, String> map = childData.get(groupPosition).get(
childPosition);
String childChecked = map.get("isChecked");
if ("No".equals(childChecked))
{
map.put("isChecked", "Yes");
hashSet.add("選定" + childPosition);
} else
{
map.put("isChecked", "No");
if (hashSet.contains("選定" + childPosition))
{
hashSet.remove("選定" + childPosition);
}
}
System.out.println("選定的長(zhǎng)度==1" + hashSet.size());
System.out.println("選定的長(zhǎng)度==2"
+ childData.get(groupPosition).size());
if (hashSet.size() == childData.get(groupPosition).size())
{
parentList.get(groupPosition).put("isGroupCheckd", "Yes");
} else
{
parentList.get(groupPosition).put("isGroupCheckd", "No");
}
adapter.notifyDataSetChanged();
return false;
}
});
}
// 初始化數(shù)據(jù)
private void initData()
{
for (int i = 0; i < 10; i++)
{
Map<String, String> groupMap = new HashMap<String, String>();
groupMap.put("groupText", "item" + i);
groupMap.put("isGroupCheckd", "No");
parentList.add(groupMap);
}
for (int i = 0; i < 10; i++)
{
List<Map<String, String>> list = new ArrayList<Map<String, String>>();
for (int j = 0; j < 4; j++)
{
Map<String, String> map = new HashMap<String, String>();
map.put("childItem", "childItem" + j);
map.put("isChecked", "No");
list.add(map);
}
childData.add(list);
}
adapter = new MyAdapter();
exListView.setAdapter(adapter);
exListView.expandGroup(0);
hashSet = new HashSet<String>();
}
private void initView()
{
exListView = (ExpandableListView) findViewById(R.id.exlistview);
}
/**
* 適配adapter
*/
private class MyAdapter extends BaseExpandableListAdapter {
@Override
public Object getChild(int groupPosition, int childPosition)
{
// TODO Auto-generated method stub
return childData.get(groupPosition).get(childPosition);
}
@Override
public long getChildId(int groupPosition, int childPosition)
{
// TODO Auto-generated method stub
return childPosition;
}
@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent)
{
ViewHolder holder = null;
if (convertView == null)
{
holder = new ViewHolder();
convertView = View.inflate(context, R.layout.listview_item,
null);
holder.childText = (TextView) convertView
.findViewById(R.id.id_text);
holder.childBox = (CheckBox) convertView
.findViewById(R.id.id_checkbox);
convertView.setTag(holder);
} else
{
holder = (ViewHolder) convertView.getTag();
}
holder.childText.setText(childData.get(groupPosition)
.get(childPosition).get("childItem"));
String isChecked = childData.get(groupPosition).get(childPosition)
.get("isChecked");
if ("No".equals(isChecked))
{
holder.childBox.setChecked(false);
} else
{
holder.childBox.setChecked(true);
}
return convertView;
}
@Override
public int getChildrenCount(int groupPosition)
{
// TODO Auto-generated method stub
return childData.get(groupPosition).size();
}
@Override
public Object getGroup(int groupPosition)
{
return parentList.get(groupPosition);
}
@Override
public int getGroupCount()
{
// TODO Auto-generated method stub
return parentList.size();
}
@Override
public long getGroupId(int groupPosition)
{
// TODO Auto-generated method stub
return groupPosition;
}
@Override
public View getGroupView(final int groupPosition,
final boolean isExpanded, View convertView, ViewGroup parent)
{
ViewHolder holder = null;
if (convertView == null)
{
holder = new ViewHolder();
convertView = View.inflate(context, R.layout.group_item, null);
holder.groupText = (TextView) convertView
.findViewById(R.id.id_group_text);
holder.groupBox = (CheckBox) convertView
.findViewById(R.id.id_group_checkbox);
convertView.setTag(holder);
} else
{
holder = (ViewHolder) convertView.getTag();
}
holder.groupText.setText(parentList.get(groupPosition).get(
"groupText"));
final String isGroupCheckd = parentList.get(groupPosition).get(
"isGroupCheckd");
if ("No".equals(isGroupCheckd))
{
holder.groupBox.setChecked(false);
} else
{
holder.groupBox.setChecked(true);
}
/*
* groupListView的點(diǎn)擊事件
*/
holder.groupBox.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
CheckBox groupBox = (CheckBox) v
.findViewById(R.id.id_group_checkbox);
if (!isExpanded)
{
//展開(kāi)某個(gè)group view
exListView.expandGroup(groupPosition);
} else
{
//關(guān)閉某個(gè)group view
exListView.collapseGroup(groupPosition);
}
if ("No".equals(isGroupCheckd))
{
exListView.expandGroup(groupPosition);
groupBox.setChecked(true);
parentList.get(groupPosition).put("isGroupCheckd",
"Yes");
List<Map<String, String>> list = childData
.get(groupPosition);
for (Map<String, String> map : list)
{
map.put("isChecked", "Yes");
}
} else
{
groupBox.setChecked(false);
parentList.get(groupPosition)
.put("isGroupCheckd", "No");
List<Map<String, String>> list = childData
.get(groupPosition);
for (Map<String, String> map : list)
{
map.put("isChecked", "No");
}
}
notifyDataSetChanged();
}
});
return convertView;
}
@Override
public boolean hasStableIds()
{
return true;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition)
{
return true;
}
}
private class ViewHolder {
TextView groupText, childText;
CheckBox groupBox, childBox;
}
}
四、總結(jié)及注意點(diǎn)
1、設(shè)置CheckBox的點(diǎn)擊事件,而非別的
2、exListView.collapseGroup(groupPosition); 關(guān)閉正展開(kāi)的子ListView.
這是demo地址,歡迎下載:
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android ExpandableListView雙層嵌套實(shí)現(xiàn)三級(jí)樹(shù)形菜單
- Android ExpandableListView實(shí)現(xiàn)下拉刷新和加載更多效果
- Android ScrollView嵌套ExpandableListView顯示不正常的問(wèn)題的解決辦法
- Android listview ExpandableListView實(shí)現(xiàn)多選,單選,全選,edittext實(shí)現(xiàn)批量輸入的實(shí)例代碼
- Android 關(guān)于ExpandableListView刷新問(wèn)題的解決方法
- Android 關(guān)于ExpandableListView去掉里頭分割線的方法
- Android UI控件ExpandableListView基本用法詳解
- Android改變ExpandableListView的indicator圖標(biāo)實(shí)現(xiàn)方法
- Android中ExpandableListView的用法實(shí)例
- Android ExpandableListView展開(kāi)列表控件使用實(shí)例
- Android ExpandableListView用法示例詳解
相關(guān)文章
android 復(fù)制 粘貼 剪切功能應(yīng)用
網(wǎng)上有很多android 復(fù)制 粘貼 剪切功能的文章,只是放到自己的程序中不知道如何處理,現(xiàn)在尋得一可行方法,需要的朋友可以參考下2012-11-11
Android底部導(dǎo)航欄的動(dòng)態(tài)替換方案
這篇文章主要為大家詳細(xì)介紹了Android底部導(dǎo)航欄的動(dòng)態(tài)替換方案,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-12-12
Android實(shí)現(xiàn)定制返回按鈕動(dòng)畫(huà)效果的方法
這篇文章主要介紹了Android實(shí)現(xiàn)定制返回按鈕動(dòng)畫(huà)效果的方法,涉及Android控件及動(dòng)畫(huà)的相關(guān)操作技巧,需要的朋友可以參考下2016-02-02
Android實(shí)現(xiàn)時(shí)間倒計(jì)時(shí)功能
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)時(shí)間倒計(jì)時(shí)功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-10-10
Android 中IntentFilter的匹配規(guī)則實(shí)例詳解
這篇文章主要介紹了Android 中IntentFilter的匹配規(guī)則實(shí)例詳解的相關(guān)資料,希望通過(guò)本文大家能了解掌握IntentFilter的匹配規(guī)則問(wèn)題,需要的朋友可以參考下2017-09-09
Android UI設(shè)計(jì)系列之HTML標(biāo)簽實(shí)現(xiàn)TextView設(shè)置中文字體加粗效果(6)
這篇文章主要介紹了Android UI設(shè)計(jì)系列之使用HTML標(biāo)簽,實(shí)現(xiàn)在TextView中對(duì)中文字體加粗的效果,具有一定的實(shí)用性和參考價(jià)值,感興趣的小伙伴們可以參考一下2016-06-06
關(guān)于Android SDCard存儲(chǔ)的問(wèn)題
本篇文章小編為大家介紹,關(guān)于Android SDCard存儲(chǔ)的問(wèn)題。需要的朋友參考下2013-04-04
Android基于ViewDragHelper仿QQ5.0側(cè)滑界面效果
這篇文章主要介紹了Android基于ViewDragHelper仿QQ5.0側(cè)滑界面效果,具有一定的,感興趣的小伙伴們可以參考一下2016-07-07
解析Android應(yīng)用程序運(yùn)行機(jī)制
這篇文章主要介紹了Android應(yīng)用程序運(yùn)行機(jī)制,有需要的朋友可以參考一下2014-01-01

