Android仿QQ好友列表分组实现增删改及持久化

2019-12-10 19:14:55于海丽

构造函数:将传入的parentList和map进行数据同步,parentList保存组数据,map保存对应组及其子项list数据;
获取分组数及子项数等很简单,就不介绍了,主要讲述一下,组视图初始化和子项视图初始化这两个函数;

组视图初始化getGroupView():尽量复用convertView防止内存泄露,首先是进行判断,若convertView为空,则进行组视图初始化,加载list_item_parent子项布局;获取到相应的组布局控件:展开图标image、添加图标image_add、删除图标image_delete;通过传递过来的布尔类型参数isExpanded,进行判断给image赋值展开图标或合并图标;分别给添加图标和删除图标添加点击事件,分别调用GroupListFragment中的弹出添加窗口函数alertAddDialog()和删除组函数deleteGroup();

子项视图初始化getChildView():同样尽量复用convertView防止内存泄露,首先是进行判断,若convertView为空,则进行子项视图初始化,加载list_item_child子项布局;获取到相应的子布局控件:内容文本ChildText和删除图标Image_delete;从parentList和map中分别获取到,当前子项的组名parentName和子项名childName,赋值ChildText,删除图标添加点击事件,调用删除子项函数deleteChild();

③实现自定义对话框类ModifyDialog,继承自Dialog类,对输入修改内容,或新增项内容进行过渡;

no_title_dialog.xml,在values目录下自定义Dialog的style类型xml,除去Dialog的标题栏;

<?xml version="1.0" encoding="utf-8"?> 
<resources xmlns:android="http://www.easck.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 自定义对话框的布局文件,标题文本,输入框,确定按钮;

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://www.easck.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>