版权声明:本文为Xzhi博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/CSDN_zhi/article/details/61200010
转载请注明本文出自Xzhi的博客(http://blog.csdn.net/CSDN_zhi/article/details/61200010),请尊重他人的辛勤劳动成果,谢谢!
前言
过几天就要开始物联网比赛了,从百忙之中抽出一点时间总结下在备赛中遇到ExpandableListView的使用。之前也遇到过ExpandableListView,但只是使用SimpleExpandableListAdapter适配器简单的实现。SimpleExpandableListAdapter虽然使用简单,但不适合一些复杂的功能,获取数据源也不方便。比如,我在前天备赛训练中遇到这样的麻烦:Android端从PC端获取数据并以ExpandableListView形式展示,这时候的数据源就不是死的了,SimpleExpandableListAdapter是也可以实现但本人感觉还是相当的麻烦。这就需要用到BaseExpandableListAdapter适配器了。
效果图
我就简单使用BaseExpandableListAdapter实现下仿QQ添加分组功能。
先看下效果图:
xml代码
直接上代码
activity_main.xml
<RelativeLayout 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"
tools:context=".MainActivity" >
<ImageView
android:id="@+id/add"
android:layout_width="60dp"
android:layout_height="60dp"
android:onClick="add"
android:src="@drawable/add" />
<ExpandableListView
android:id="@+id/expandableListView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/add"
>
</ExpandableListView>
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/add"
android:layout_marginBottom="20dp"
android:layout_toRightOf="@+id/add"
android:text="添加分组" />
</RelativeLayout>
child_item.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/cName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/imageView1"
android:layout_marginBottom="32dp"
android:layout_marginLeft="16dp"
android:layout_toRightOf="@+id/imageView1"
android:text="TextView" />
<ImageView
android:id="@+id/imageView1"
android:layout_width="80dp"
android:layout_height="90dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="30dp"
android:layout_marginTop="16dp"
android:src="@drawable/ic_launcher" />
</RelativeLayout>
group_item.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/gCount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/gName"
android:layout_alignBottom="@+id/gName"
android:layout_alignParentRight="true"
android:layout_marginRight="66dp"
android:text="TextView" />
<TextView
android:id="@+id/gName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="30dp"
android:layout_marginTop="10dp"
android:text="TextView" />
</RelativeLayout>
java代码
MainActivity.java
public class MainActivity extends Activity
{
MyAdapter adapter;
ExpandableListView ep;
/* 一级数据源 */
List<GroupInfo> groupList;
/* 二级数据源 */
List<List<ChildInfo>> childList;
List<ChildInfo>cList;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ep = (ExpandableListView)findViewById(R.id.expandableListView1);
init();
epClick();
}
public void init()
{
groupList = new ArrayList<GroupInfo>();
groupList.add(new GroupInfo("我的好友"));
cList = new ArrayList<ChildInfo>();
cList.add(new ChildInfo("张三", BitmapFactory.decodeResource(getResources(), R.drawable.pic6)));
cList.add(new ChildInfo("李四", BitmapFactory.decodeResource(getResources(), R.drawable.pic5)));
cList.add(new ChildInfo("王五", BitmapFactory.decodeResource(getResources(), R.drawable.pic3)));
cList.add(new ChildInfo("小红", BitmapFactory.decodeResource(getResources(), R.drawable.pic4)));
cList.add(new ChildInfo("小花", BitmapFactory.decodeResource(getResources(), R.drawable.pic1)));
cList.add(new ChildInfo("小诗", BitmapFactory.decodeResource(getResources(), R.drawable.pic2)));
childList = new ArrayList<List<ChildInfo>>();
childList.add(cList);
adapter = new MyAdapter(groupList, childList, this);
ep.setAdapter(adapter);
}
/**
* ep点击事件
*
* @time 2017-3-10 上午10:55:41
*/
public void epClick()
{
ep.setOnItemLongClickListener(new OnItemLongClickListener()
{
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id)
{
final EditText editText = new EditText(MainActivity.this);
editText.setHint("请输入姓名");
new AlertDialog.Builder(MainActivity.this)
.setTitle("添加好友")
.setView(editText)
.setNegativeButton("取消", new OnClickListener()
{
public void onClick(DialogInterface dialog, int which)
{
try
{
/** 利用反射技术不关闭对话框 */
Field field = dialog.getClass().getSuperclass().getDeclaredField("mShowing");
field.setAccessible(true);
// true 为关闭
field.set(dialog, true);
dialog.dismiss();
}
catch (Exception e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
// TODO Auto-generated method stub
}
})
.setPositiveButton("确定", new OnClickListener()
{
public void onClick(DialogInterface dialog, int which)
{
Field field = null;
try
{
field = dialog.getClass().getSuperclass().getDeclaredField("mShowing");
field.setAccessible(true);
String string = editText.getText().toString();
if (!(string.equals("")))
{
field.set(dialog, true);
dialog.dismiss();
// cList.clear();
cList = new ArrayList<ChildInfo>();
cList.add(new ChildInfo(string, BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher)));
// childList = new ArrayList<List<ChildInfo>>();
childList.add(cList);
adapter.notifyDataSetChanged();
}
else
{
Toast.makeText(MainActivity.this, "姓名不能为空", 0).show();
field.set(dialog, false);
dialog.dismiss();
}
}
catch (Exception e)
{
// TODO: handle exception
}
}
}).show();
return false;
}
});
}
public void add(View v)
{
final EditText editText = new EditText(this);
editText.setHint("请输入分组名");
new AlertDialog.Builder(this)
.setTitle("添加分组")
.setView(editText)
.setNegativeButton("取消", new OnClickListener()
{
public void onClick(DialogInterface dialog, int which)
{
try
{
Field field = dialog.getClass().getSuperclass().getDeclaredField("mShowing");
field.setAccessible(true);
// 为true,表示关闭对话框
field.set(dialog, true);
dialog.dismiss();
}
catch (Exception e)
{
// TODO: handle exception
}
}
})
.setPositiveButton("确定", new OnClickListener()
{
public void onClick(DialogInterface dialog, int which)
{
Field field = null;
try
{
/** 利用反射技术不关闭对话框 */
field = dialog.getClass().getSuperclass().getDeclaredField("mShowing");
field.setAccessible(true);
String string = editText.getText().toString();
if (!(string.equals("")))
{
groupList.add(new GroupInfo(string));
adapter.notifyDataSetChanged();
// 为true,表示关闭对话框
field.set(dialog, true);
dialog.dismiss();
}
else
{
// 为false,表示不关闭对话框
field.set(dialog, false);
dialog.dismiss();
Toast.makeText(MainActivity.this, "请输入分组名", 0).show();
}
}
catch (Exception e)
{
// TODO: handle exception
}
}
}).show();
}
}
MyAdapter.java
public class MyAdapter extends BaseExpandableListAdapter
{
/* 一级数据源 */
List<GroupInfo> groupList;
/* 二级数据源 */
List<List<ChildInfo>> childList;
Context context;
public MyAdapter(List<GroupInfo> groupList, List<List<ChildInfo>> childList, Context context)
{
super();
this.groupList = groupList;
this.childList = childList;
this.context = context;
}
class GroupView
{
TextView groupName;
TextView count;
}
class ChildView
{
ImageView pic;
TextView childName;
}
/********************* Group ************************/
public int getGroupCount()
{
return groupList.size();
}
public Object getGroup(int groupPosition)
{
return groupList.get(groupPosition);
}
public long getGroupId(int groupPosition)
{
return groupPosition;
}
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent)
{
GroupView gView = null;
if (convertView == null)
{
gView = new GroupView();
convertView = LayoutInflater.from(context).inflate(R.layout.group_item, null);
gView.groupName = (TextView) convertView.findViewById(R.id.gName);
gView.count = (TextView) convertView.findViewById(R.id.gCount);
convertView.setTag(gView);
}
else
{
gView = (GroupView) convertView.getTag();
}
GroupInfo groupInfo = groupList.get(groupPosition);
gView.groupName.setText(groupInfo.getName());
if(groupPosition==0)
{
gView.count.setText(""+childList.get(groupPosition).size());
}
else
{
gView.count.setText("0");
}
return convertView;
}
/**************************** Child ***************************/
int i = 0;
public int getChildrenCount(int groupPosition)
{
if(groupPosition == 0)
{
i = childList.get(groupPosition).size();
}
else
{
Toast.makeText(context, "长按添加好友", 0).show();
i = 0;
}
return i;
}
public Object getChild(int groupPosition, int childPosition)
{
return childList.get(groupPosition).get(childPosition);
}
public long getChildId(int groupPosition, int childPosition)
{
return childPosition;
}
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent)
{
ChildView cView = null;
if (convertView == null)
{
cView = new ChildView();
convertView = LayoutInflater.from(context).inflate(R.layout.child_item, null);
cView.childName = (TextView) convertView.findViewById(R.id.cName);
cView.pic = (ImageView) convertView.findViewById(R.id.imageView1);
convertView.setTag(cView);
}
else
{
cView = (ChildView) convertView.getTag();
}
ChildInfo childInfo = childList.get(groupPosition).get(childPosition);
cView.pic.setImageBitmap(childInfo.getChildPic());
cView.childName.setText(childInfo.getChildName());
return convertView;
}
public boolean hasStableIds()
{
return false;
}
public boolean isChildSelectable(int groupPosition, int childPosition)
{
return false;
}
}
ChildInfo.java
/**
*
* @function Child实体类
* @author Xzhi
* @time 2017-3-10 上午10:00:41
* Copyright (c) 2017 Xzhi All Rights Reserved.
*
*/
public class ChildInfo
{
String childName;
Bitmap childPic;
public ChildInfo(String childName, Bitmap childPic)
{
super();
this.childName = childName;
this.childPic = childPic;
}
public String getChildName()
{
return childName;
}
public void setChildName(String childName)
{
this.childName = childName;
}
public Bitmap getChildPic()
{
return childPic;
}
public void setChildPic(Bitmap childPic)
{
this.childPic = childPic;
}
}
GroupInfo.java
/**
*
* @function Group实体类
* @author Xzhi
* @time 2017-3-10 上午9:55:41
* Copyright (c) 2017 Xzhi All Rights Reserved.
*
*/
public class GroupInfo
{
String name;
int count;
public GroupInfo(String name)
{
super();
this.name = name;
this.count = count;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public int getCount()
{
return count;
}
public void setCount(int count)
{
this.count = count;
}
}
上面代码很简单就不需要解释了 。
由于时间比较紧迫,所以【添加好友】这个功能还没实现,打算比完赛后再将它实现。
如果项目对您有帮助,欢迎大家在github上Star我的项目,当然如果大家有什么建议欢迎留言或fork。
源码点击下载
赞赏支持 |
如果您觉得我的文章对您有帮助的话,请随意打赏。您的支持将鼓励我继续创作!