Android仿QQ好友列表分組實(shí)現(xiàn)增刪改及持久化
Android自帶的控件ExpandableListView實(shí)現(xiàn)了分組列表功能,本案例在此基礎(chǔ)上進(jìn)行優(yōu)化,為此控件添加增刪改分組及子項(xiàng)的功能,以及列表數(shù)據(jù)的持久化。
Demo實(shí)現(xiàn)效果:

GroupListDemo具體實(shí)現(xiàn):
①demo中將列表頁(yè)面設(shè)計(jì)為Fragment頁(yè)面,方便后期調(diào)用;在主界面MainActivity中動(dòng)態(tài)添加GroupListFragment頁(yè)面;
MainActivity.java
package com.eric.grouplistdemo;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.os.Bundle;
import android.widget.RelativeLayout;
public class MainActivity extends Activity {
public static GroupListFragment fragment;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fragment = new GroupListFragment();
getFragmentManager().beginTransaction()
.replace(R.id.fragContainer, fragment).commit();
}
}
動(dòng)態(tài)添加GroupListFragment實(shí)例到界面的fragContainer布局中;將fragment聲明為static用于在Adapter中組添加子項(xiàng)時(shí)進(jìn)行調(diào)用。
activity_main.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="match_parent" android:orientation="vertical" > <RelativeLayout android:id="@+id/fragContainer" android:layout_width="match_parent" android:layout_height="match_parent" > </RelativeLayout> </LinearLayout>
②實(shí)現(xiàn)自定義適配器類MyAdapter,繼承自BaseExpandableListAdapter;組項(xiàng)布局及子項(xiàng)布局;
list_item_parent.xml組項(xiàng)布局文件,展開圖標(biāo)及名稱,增刪圖標(biāo);
<?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="50dp" android:background="#0099ff" android:orientation="horizontal"> <ImageView android:id="@+id/image_parent" android:layout_width="50dp" android:layout_height="50dp" android:src="@drawable/image_parent1"/> <TextView android:id="@+id/text_parent" android:layout_width="wrap_content" android:layout_height="50dp" android:textColor="#FFF" android:textSize="20sp" android:text="parent1" android:layout_toRightOf="@id/image_parent" android:gravity="center"/> <ImageView android:id="@+id/image_delete" android:layout_width="40dp" android:layout_height="40dp" android:layout_centerVertical="true" android:layout_alignParentRight="true" android:src="@drawable/delete"/> <ImageView android:id="@+id/image_add" android:layout_width="40dp" android:layout_height="40dp" android:layout_centerVertical="true" android:layout_toLeftOf="@id/image_delete" android:src="@drawable/add"/> </RelativeLayout>
list_item_child.xml子項(xiàng)布局文件,名稱及刪除圖標(biāo);
<?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="40dp" > <TextView android:id="@+id/text_child" android:layout_width="wrap_content" android:layout_height="40dp" android:layout_margin="5dp" android:textColor="#0099ff" android:text="child" android:layout_centerInParent="true" android:gravity="center"/> <ImageView android:id="@+id/image_delete" android:layout_width="40dp" android:layout_height="40dp" android:layout_alignParentRight="true" android:layout_centerVertical="true" android:src="@drawable/delete"/> </RelativeLayout>
MyAdapter.java自定義適配器
package com.eric.grouplistdemo;
import java.util.List;
import java.util.Map;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
public class MyAdapter extends BaseExpandableListAdapter{
private List<String> parentList;
private Map<String,List<String>> map;
private Context context;
private EditText edit_modify;
private ModifyDialog dialog;
//構(gòu)造函數(shù)
public MyAdapter(Context context, List<String> parentList, Map<String,List<String>> map) {
this.context = context;
this.parentList = parentList;
this.map = map;
}
//獲取分組數(shù)
@Override
public int getGroupCount() {
return parentList.size();
}
//獲取當(dāng)前組的子項(xiàng)數(shù)
@Override
public int getChildrenCount(int groupPosition) {
String groupName = parentList.get(groupPosition);
int childCount = map.get(groupName).size();
return childCount;
}
//獲取當(dāng)前組對(duì)象
@Override
public Object getGroup(int groupPosition) {
String groupName = parentList.get(groupPosition);
return groupName;
}
//獲取當(dāng)前子項(xiàng)對(duì)象
@Override
public Object getChild(int groupPosition, int childPosition) {
String groupName = parentList.get(groupPosition);
String chidlName = map.get(groupName).get(childPosition);
return chidlName;
}
//獲取組ID
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
//獲取子項(xiàng)ID
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public boolean hasStableIds() {
return true;
}
//組視圖初始化
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
final int groupPos = groupPosition;
if(convertView == null){
convertView = LayoutInflater.from(context).inflate(R.layout.list_item_parent, null);
}
ImageView image = (ImageView) convertView.findViewById(R.id.image_parent);
ImageView image_add = (ImageView) convertView.findViewById(R.id.image_add);
ImageView image_delete = (ImageView) convertView.findViewById(R.id.image_delete);
if(isExpanded){
image.setImageResource(R.drawable.image_parent2);
}else{
image.setImageResource(R.drawable.image_parent1);
}
image_add.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
alertAddDialog(MainActivity.fragment.getActivity(), "新增子項(xiàng)", groupPos);
}
});
image_delete.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
GroupListFragment.deleteGroup(groupPos);
}
});
TextView parentText = (TextView) convertView.findViewById(R.id.text_parent);
parentText.setText(parentList.get(groupPosition));
return convertView;
}
//子項(xiàng)視圖初始化
@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
final int groupPos = groupPosition;
final int childPos = childPosition;
if(convertView == null){
convertView = LayoutInflater.from(context).inflate(R.layout.list_item_child, null);
}
TextView childText = (TextView) convertView.findViewById(R.id.text_child);
ImageView image_delete = (ImageView) convertView.findViewById(R.id.image_delete);
String parentName = parentList.get(groupPosition);
String childName = map.get(parentName).get(childPosition);
childText.setText(childName);
image_delete.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
GroupListFragment.deleteChild(groupPos, childPos);
}
});
return convertView;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
//彈新增子項(xiàng)對(duì)話框
public void alertAddDialog(Context context, String title, int currentGroup){
final int group = currentGroup;
dialog = new ModifyDialog(context, title, null);
edit_modify = dialog.getEditText();
dialog.setOnClickCommitListener(new OnClickListener() {
@Override
public void onClick(View v) {
GroupListFragment.addChild(group, edit_modify.getText().toString());
dialog.dismiss();
}
});
dialog.show();
}
}
構(gòu)造函數(shù):將傳入的parentList和map進(jìn)行數(shù)據(jù)同步,parentList保存組數(shù)據(jù),map保存對(duì)應(yīng)組及其子項(xiàng)list數(shù)據(jù);
獲取分組數(shù)及子項(xiàng)數(shù)等很簡(jiǎn)單,就不介紹了,主要講述一下,組視圖初始化和子項(xiàng)視圖初始化這兩個(gè)函數(shù);
組視圖初始化getGroupView():盡量復(fù)用convertView防止內(nèi)存泄露,首先是進(jìn)行判斷,若convertView為空,則進(jìn)行組視圖初始化,加載list_item_parent子項(xiàng)布局;獲取到相應(yīng)的組布局控件:展開圖標(biāo)image、添加圖標(biāo)image_add、刪除圖標(biāo)image_delete;通過(guò)傳遞過(guò)來(lái)的布爾類型參數(shù)isExpanded,進(jìn)行判斷給image賦值展開圖標(biāo)或合并圖標(biāo);分別給添加圖標(biāo)和刪除圖標(biāo)添加點(diǎn)擊事件,分別調(diào)用GroupListFragment中的彈出添加窗口函數(shù)alertAddDialog()和刪除組函數(shù)deleteGroup();
子項(xiàng)視圖初始化getChildView():同樣盡量復(fù)用convertView防止內(nèi)存泄露,首先是進(jìn)行判斷,若convertView為空,則進(jìn)行子項(xiàng)視圖初始化,加載list_item_child子項(xiàng)布局;獲取到相應(yīng)的子布局控件:內(nèi)容文本ChildText和刪除圖標(biāo)Image_delete;從parentList和map中分別獲取到,當(dāng)前子項(xiàng)的組名parentName和子項(xiàng)名childName,賦值ChildText,刪除圖標(biāo)添加點(diǎn)擊事件,調(diào)用刪除子項(xiàng)函數(shù)deleteChild();
③實(shí)現(xiàn)自定義對(duì)話框類ModifyDialog,繼承自Dialog類,對(duì)輸入修改內(nèi)容,或新增項(xiàng)內(nèi)容進(jìn)行過(guò)渡;
no_title_dialog.xml,在values目錄下自定義Dialog的style類型xml,除去Dialog的標(biāo)題欄;
<?xml version="1.0" encoding="utf-8"?> <resources xmlns:android="http://schemas.android.com/apk/res/android"> <style name="noTitleDialog" parent="android:style/Theme.Dialog"> <item name="android:width">300dp</item> <item name="android:height">40dp</item> <item name="android:windowNoTitle">true</item> </style> </resources>
dialog_modify.xml 自定義對(duì)話框的布局文件,標(biāo)題文本,輸入框,確定按鈕;
<?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="match_parent" android:orientation="vertical" > <TextView android:id="@+id/text_title" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:gravity="center" android:background="#0099ff" android:text="修改名稱" android:textColor="#FFF" android:textSize="20sp"/> <EditText android:id="@+id/edit_modify" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="組名稱"/> <Button android:id="@+id/btn_commit" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="確定" android:textColor="#FFF" android:background="#0099ff" /> </LinearLayout>
ModifyDialog.java
package com.eric.grouplistdemo;
import android.app.Dialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class ModifyDialog extends Dialog{
private TextView text_title;
private EditText edit_modify;
private Button btn_commit;
public ModifyDialog(Context context, String title, String name) {
super(context, R.style.noTitleDialog);
View view = LayoutInflater.from(getContext())
.inflate(R.layout.dialog_modify, null);
text_title = (TextView) view.findViewById(R.id.text_title);
edit_modify = (EditText)view.findViewById(R.id.edit_modify);
btn_commit = (Button) view.findViewById(R.id.btn_commit);
text_title.setText(title);
edit_modify.setText(name);
super.setContentView(view);
}
public EditText getEditText(){
return edit_modify;
}
public void setOnClickCommitListener(View.OnClickListener listener){
btn_commit.setOnClickListener(listener);
}
}
ModifyDialog自定義構(gòu)造函數(shù)中,通過(guò)super()加載剛剛自定義的no_title_dialog.xml,聲明View加載layout布局dialog_modify.xml;并且獲取布局中的相應(yīng)控件,將構(gòu)造函數(shù)中傳來(lái)的字符串title和name,分別賦值到標(biāo)題文本和輸入框控件中;最后調(diào)用setContentView()初始化對(duì)話框視圖;
添加一個(gè)返回輸入框控件的函數(shù)getEditText(),用于獲取輸入框輸入的內(nèi)容;
還需要一個(gè)自定義的點(diǎn)擊事件監(jiān)聽(tīng)器,綁定在確定按鈕上;
④準(zhǔn)備工作都完成了,下面就實(shí)現(xiàn)GroupListFragment,包括數(shù)據(jù)的初始化及持久化保存,組項(xiàng)和子項(xiàng)的增刪改操作,列表子項(xiàng)點(diǎn)擊事件,列表組項(xiàng)和子項(xiàng)的長(zhǎng)按事件;
fragment_group_list.xml 頁(yè)面的布局文件ExpandableListView列表以及一個(gè)添加組圖標(biāo);
<?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" > <ExpandableListView android:id="@+id/expandablelistview" android:layout_width="match_parent" android:layout_height="match_parent" android:groupIndicator="@null" > </ExpandableListView> <ImageView android:id="@+id/image_add" android:layout_width="40dp" android:layout_height="40dp" android:layout_centerHorizontal="true" android:layout_alignParentBottom="true" android:src="@drawable/add"/> </RelativeLayout>
這里需要將ExpandableListView的groupIndicator屬性設(shè)置為@null,不使用其自帶的展開圖標(biāo);
GroupListFragment.java 加載列表的頁(yè)面
package com.eric.grouplistdemo;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.R.integer;
import android.app.Fragment;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.Toast;
public class GroupListFragment extends Fragment{
private View view;
private ExpandableListView expandableListView;
public static MyAdapter adapter;
public static List<String> parentList;
public static Map<String,List<String>> map;
private ModifyDialog dialog;
private EditText edit_modify;
private ImageView image_add;
private int currentGroup,currentChild;
public static SharedPreferences sp;
public static Editor editor;
public static String dataMap,dataParentList;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_group_list, container, false);
expandableListView = (ExpandableListView) view.findViewById(R.id.expandablelistview);
image_add = (ImageView) view.findViewById(R.id.image_add);
image_add.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
alertAddDialog(getActivity(), "新增組");
}
});
initData();
adapter = new MyAdapter(getActivity().getApplicationContext(), parentList, map);
expandableListView.setAdapter(adapter);
//設(shè)置子項(xiàng)點(diǎn)擊事件
MyOnClickListener myListener = new MyOnClickListener();
expandableListView.setOnChildClickListener(myListener);
//設(shè)置長(zhǎng)按點(diǎn)擊事件
MyOnLongClickListener myLongListener = new MyOnLongClickListener();
expandableListView.setOnItemLongClickListener(myLongListener);
return view;
}
public void initData(){
map = new HashMap<String, List<String>>();
parentList = new ArrayList<String>();
sp = getActivity().getApplicationContext().getSharedPreferences("spfile", getActivity().MODE_PRIVATE);
dataMap = sp.getString("dataMap", null);
dataParentList = sp.getString("dataParentList", null);
if(dataMap == null || dataParentList == null){
parentList = new ArrayList<String>();
parentList.add("客廳");
parentList.add("廚房");
parentList.add("臥室");
List<String> list1 = new ArrayList<String>();
list1.add("客廳空調(diào)");
list1.add("客廳電視");
list1.add("客廳電燈");
map.put("客廳", list1);
List<String> list2 = new ArrayList<String>();
list2.add("廚房油煙機(jī)");
list2.add("廚房電燈");
list2.add("廚房電器");
map.put("廚房", list2);
List<String> list3 = new ArrayList<String>();
list3.add("臥室空調(diào)");
list3.add("臥室燈光");
list3.add("臥室電視");
map.put("臥室", list3);
}else{
try {
//初始化parentList
JSONArray jsonArray = new JSONArray(dataParentList);
for (int i = 0; i < jsonArray.length(); i++) {
parentList.add(jsonArray.get(i).toString());
}
//初始化map
JSONObject jsonObject = new JSONObject(dataMap);
for (int i = 0; i < jsonObject.length(); i++) {
String key = jsonObject.getString(parentList.get(i));
JSONArray array = new JSONArray(key);
List<String> list = new ArrayList<String>();
for (int j = 0; j < array.length(); j++) {
list.add(array.get(j).toString());
}
map.put(parentList.get(i), list);
}
Log.d("eric", "①:"+map+"②:"+parentList);
} catch (JSONException e) {
e.printStackTrace();
Log.e("eric","String轉(zhuǎn)Map或List出錯(cuò)"+e);
}
}
Log.e("eric", dataMap+"!&&!"+dataParentList);
saveData();
}
//自定義點(diǎn)擊監(jiān)聽(tīng)事件
public class MyOnClickListener implements ExpandableListView.OnChildClickListener{
@Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
String str = "choose"+groupPosition+"-"+childPosition;
Toast.makeText(getActivity(), str, Toast.LENGTH_SHORT).show();
return false;
}
}
//自定義長(zhǎng)按監(jiān)聽(tīng)事件
public class MyOnLongClickListener implements AdapterView.OnItemLongClickListener{
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view,
int position, long id) {
//長(zhǎng)按子項(xiàng)
if (ExpandableListView.getPackedPositionType(id) == ExpandableListView.PACKED_POSITION_TYPE_CHILD){
long packedPos = ((ExpandableListView) parent).getExpandableListPosition(position);
int groupPosition = ExpandableListView.getPackedPositionGroup(packedPos);
int childPosition = ExpandableListView.getPackedPositionChild(packedPos);
currentGroup = groupPosition;
currentChild = childPosition;
String str = (String)adapter.getChild(groupPosition, childPosition);
alertModifyDialog("修改此項(xiàng)名稱",str);
Toast.makeText(getActivity(),str,Toast.LENGTH_SHORT).show();
return true;
//長(zhǎng)按組
}else if(ExpandableListView.getPackedPositionType(id) == ExpandableListView.PACKED_POSITION_TYPE_GROUP){
long packedPos = ((ExpandableListView) parent).getExpandableListPosition(position);
int groupPosition = ExpandableListView.getPackedPositionGroup(packedPos);
int childPosition = ExpandableListView.getPackedPositionChild(packedPos);
currentGroup = groupPosition;
currentChild = childPosition;
String group = parentList.get(groupPosition);
alertModifyDialog("修改組名稱", group);
String str = (String)adapter.getGroup(groupPosition);
Toast.makeText(getActivity(),str,Toast.LENGTH_SHORT).show();
}
return false;
}
}
//新增組
public static void addGroup(String newGroupName){
parentList.add(newGroupName);
List<String> list = new ArrayList<String>();
map.put(newGroupName, list);
adapter.notifyDataSetChanged();
saveData();
}
//新增子項(xiàng)到指定組
public static void addChild(int groupPosition, String newChildName){
String groupName = parentList.get(groupPosition);
List<String> list = map.get(groupName);
list.add(newChildName);
adapter.notifyDataSetChanged();
saveData();
}
//刪除指定組
public static void deleteGroup(int groupPos){
String groupName = parentList.get(groupPos);
map.remove(groupName);
parentList.remove(groupPos);
adapter.notifyDataSetChanged();
saveData();
}
//刪除指定子項(xiàng)
public static void deleteChild(int groupPos, int childPos){
String groupName = parentList.get(groupPos);
List<String> list = map.get(groupName);
list.remove(childPos);
adapter.notifyDataSetChanged();
saveData();
}
//修改該項(xiàng)名稱
public void modifyName(int groupPosition, int childPosition, String modifyName){
Toast.makeText(getActivity(), String.valueOf(groupPosition)+'-'+String.valueOf(childPosition), Toast.LENGTH_SHORT).show();
if(childPosition<0){
//修改組名稱
String groupName = parentList.get(groupPosition);
if(!groupName.equals(modifyName)){
map.put(modifyName, map.get(groupName));
map.remove(groupName);
parentList.set(groupPosition, modifyName);
}
}else{
//修改子項(xiàng)名稱
String group = parentList.get(groupPosition);
List<String> list =map.get(group);
list.set(childPosition, modifyName);
map.put(group, list);
}
adapter.notifyDataSetChanged();
saveData();
}
//彈修改對(duì)話框
public void alertModifyDialog(String title, String name){
dialog = new ModifyDialog(getActivity(), title, name);
edit_modify = dialog.getEditText();
dialog.setOnClickCommitListener(new OnClickListener() {
@Override
public void onClick(View v) {
modifyName(currentGroup, currentChild, edit_modify.getText().toString());
dialog.dismiss();
}
});
dialog.show();
}
//彈新增組對(duì)話框
public void alertAddDialog(Context context, String title){
dialog = new ModifyDialog(context, title, null);
edit_modify = dialog.getEditText();
dialog.setOnClickCommitListener(new OnClickListener() {
@Override
public void onClick(View v) {
addGroup(edit_modify.getText().toString());
dialog.dismiss();
}
});
dialog.show();
}
//保存數(shù)據(jù)
public static void saveData(){
JSONObject jsonObject = new JSONObject(map);
dataMap = jsonObject.toString();
dataParentList = parentList.toString();
editor = sp.edit();
editor.putString("dataMap", dataMap);
editor.putString("dataParentList", dataParentList);
editor.commit();
}
}
內(nèi)容有點(diǎn)多,一個(gè)個(gè)來(lái):
初始化Fragment頁(yè)面函數(shù)onCreateView():
首先,要進(jìn)行l(wèi)ayout布局fragment_group_list.xml加載,獲取相應(yīng)控件的實(shí)例,expandableListView列表控件以及添加組圖標(biāo)image_add,添加組圖標(biāo)添加點(diǎn)擊事件;調(diào)用initData()方法進(jìn)行組數(shù)據(jù)parentList和組對(duì)應(yīng)子項(xiàng)map的初始化;然后,實(shí)例化adapter傳入parentList和map數(shù)據(jù)到自定義適配器MyAdapter中,并將其綁定到expandableListView上;最后,給expandableListView添加自定義子項(xiàng)點(diǎn)擊事件監(jiān)聽(tīng)器,組項(xiàng)和子項(xiàng)長(zhǎng)按事件監(jiān)聽(tīng)器;
初始化數(shù)據(jù)函數(shù)initData():該函數(shù)通過(guò)ShareReference來(lái)保存數(shù)據(jù);
首先,實(shí)例化parentList和map,從ShareReference中獲取到保存的String類型的parentList和map實(shí)際數(shù)據(jù),賦值到dataMap和dataParentList中,若當(dāng)中數(shù)據(jù)不存在,默認(rèn)返回null,第一次運(yùn)行程序的時(shí)候肯定是沒(méi)有數(shù)據(jù)的,故進(jìn)行判斷,若沒(méi)獲取到數(shù)據(jù),那就給parentList和Map賦值“客廳”、“廚房”、“臥室”等一系列數(shù)據(jù);如果有數(shù)據(jù)的話,那就得進(jìn)行數(shù)據(jù)的轉(zhuǎn)換,因?yàn)橹氨4娴腟tring類型數(shù)據(jù),parentList初始化:將dataParentList轉(zhuǎn)換為JSONArray類型,通過(guò)循環(huán)將數(shù)據(jù)賦值到parentList中;同理,將dataMap轉(zhuǎn)換為JSONObject類型,通過(guò)兩層for循環(huán)將數(shù)據(jù)賦值到map中。
自定義點(diǎn)擊子項(xiàng)監(jiān)聽(tīng)其類MyOnClickListener:實(shí)現(xiàn)ExpandableListView.OnChildClickListener接口,這里就簡(jiǎn)單的進(jìn)行Toast操作,讀者可以修改為跳轉(zhuǎn)等其他自定義功能;
自定義長(zhǎng)按組項(xiàng)或子項(xiàng)監(jiān)聽(tīng)器類MyOnLongClickListener:實(shí)現(xiàn)AdapterView.OnItemLongClickListener接口,通過(guò)調(diào)用ExpandableListView的getPackedPositionType()方法來(lái)判斷此時(shí)長(zhǎng)按的是組項(xiàng)還是子項(xiàng);這里將長(zhǎng)按組和子項(xiàng)分離出來(lái),方便根據(jù)功能修改;無(wú)論是組項(xiàng)還是子項(xiàng)長(zhǎng)按后,都調(diào)用alertModifyDialog()彈修改對(duì)話框,傳入當(dāng)前項(xiàng)的名稱;
新增組addGroup函數(shù):將傳遞過(guò)來(lái)的newGroupName,添加parentList中,且定義一個(gè)空的list,綁定newGroupName,將這對(duì)string和list,添加到map中;最后,調(diào)用adapter.notifyDataSetChanged()刷新列表,調(diào)用saveData()保存數(shù)據(jù)到ShareReference;
同理,新增子項(xiàng)到指定組addChild(),刪除指定組deleteGroup(),刪除指定子項(xiàng)deleteChild(),都是對(duì)parentList和map進(jìn)行操作,最后刷新列表和保存數(shù)據(jù);
修改該項(xiàng)名稱modifyName():通過(guò)傳遞過(guò)來(lái)childPosition進(jìn)行判斷,當(dāng)修改項(xiàng)為組項(xiàng)時(shí),childPosition的值為-1,以此區(qū)分組項(xiàng)和子項(xiàng);這里遇到一個(gè)問(wèn)題,當(dāng)組項(xiàng)提交的名稱與原名稱相同會(huì)報(bào)錯(cuò),故添加一個(gè)判斷,僅提交的名稱不同時(shí)才進(jìn)行修改操作;修改的具體實(shí)現(xiàn)還是對(duì)parentList和map的操作,以修改組項(xiàng)為例,同新增組,添加一個(gè)modifyName的組,其對(duì)應(yīng)的List為原來(lái)組名對(duì)應(yīng)的List數(shù)據(jù),然后再將原來(lái)的groupName組及其對(duì)應(yīng)的list刪除,實(shí)現(xiàn)修改;最后同樣要刷新列表和保存數(shù)據(jù);
彈修改對(duì)話框alertModifyDialog()函數(shù):首先對(duì)自定義的dialog進(jìn)行實(shí)例化,初始化標(biāo)題和輸入框中的數(shù)據(jù);調(diào)用getEditText()函數(shù)獲取輸入框?qū)嵗?,添加提交按鈕點(diǎn)擊事件,調(diào)用modifyName方法傳入當(dāng)前組和子項(xiàng)數(shù)據(jù),以及輸入框中提交的文本;
彈新增組對(duì)話框alertAddDialog()函數(shù):同樣,實(shí)例化dialog,獲取輸入框控件,點(diǎn)擊事件中調(diào)用addGroup()方法添加數(shù)據(jù);
保存數(shù)據(jù)saveData()函數(shù):將parentList和map數(shù)據(jù)轉(zhuǎn)化為String類型,分別賦值,保存至ShareReference的Editor中并提交;
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助。
相關(guān)文章
Android使用DatePickerDialog顯示時(shí)間
本文將結(jié)合實(shí)例代碼,介紹Android使用DatePickerDialog顯示時(shí)間,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-07-07
Android實(shí)現(xiàn)多級(jí)樹形菜單并支持多選功能
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)多級(jí)樹形菜單并支持多選功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-07-07
Android自動(dòng)編輯文本框(AutoCompleteTextView)使用方法詳解
這篇文章主要為大家詳細(xì)介紹了Android自動(dòng)編輯文本框AutoCompleteTextView的使用方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-02-02
Android使用httpPost向服務(wù)器發(fā)送請(qǐng)求的方法
這篇文章主要介紹了Android使用httpPost向服務(wù)器發(fā)送請(qǐng)求的方法,實(shí)例分析了Android針對(duì)HttpPost類的操作技巧,需要的朋友可以參考下2015-12-12
Android學(xué)習(xí)筆記之Shared Preference
在之前遇到有個(gè)需求是要改settings里面自動(dòng)轉(zhuǎn)屏的首選項(xiàng),于是就學(xué)習(xí)了下Shared Preference。Shared Preference是一種簡(jiǎn)單的、輕量級(jí)的鍵/值對(duì)機(jī)制,用于保存原始應(yīng)用程序數(shù)據(jù),最常見(jiàn)的就是首選項(xiàng)2013-09-09
Android實(shí)現(xiàn)倒計(jì)時(shí)方法匯總
這篇文章主要為大家詳細(xì)總結(jié)了Android實(shí)現(xiàn)倒計(jì)時(shí)的3種方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-01-01
使用androidx BiometricPrompt實(shí)現(xiàn)指紋驗(yàn)證功能
這篇文章主要介紹了使用androidx BiometricPrompt實(shí)現(xiàn)指紋驗(yàn)證功能,對(duì)android指紋驗(yàn)證相關(guān)知識(shí)感興趣的朋友跟隨小編一起看看吧2021-07-07
Android對(duì)so進(jìn)行簡(jiǎn)單hook思路解析
這篇文章主要為大家介紹了Android對(duì)so進(jìn)行簡(jiǎn)單hook思路解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-04-04
Android 創(chuàng)建與解析XML(四)——詳解Pull方式
本篇文章主要介紹了Android創(chuàng)建與解析XML(二)——詳解Pull方式,這里整理了詳細(xì)的代碼,有需要的小伙伴可以參考下。2016-11-11

