仿QQ列表ExpandableListView详细写法(通过Java bean封装数据)
2017-10-10 11:42
501 查看
下面写一个仿qq列表的二级列表,利用了类体封装,还算比较出色
//主xml写法activity_list_view__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:background="#edf0f6"
xmlns:tools="http://schemas.android.com/tools">
<ExpandableListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#edf0f6"
>
</ExpandableListView>
</LinearLayout>
接下来是一级列表的写法,我这里为方便布局先做了假数据,后面加载数据源的时候会直接覆盖,文件名first.xml
先贴JavaBean的封装代码
第一级列表的FirstBean.class
下面是主activity的Java代码QQ_main.class
//主xml写法activity_list_view__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:background="#edf0f6"
xmlns:tools="http://schemas.android.com/tools">
<ExpandableListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#edf0f6"
>
</ExpandableListView>
</LinearLayout>
接下来是一级列表的写法,我这里为方便布局先做了假数据,后面加载数据源的时候会直接覆盖,文件名first.xml
first.xml
//first.xml<?xml version="1.0" encoding="utf-8"?>然后是二级列表的布局文件,文件名 list_cell.xml
<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/first_tv"
android:text="我的好友"
android:layout_marginBottom="15dp"
android:layout_marginLeft="33dp"
android:layout_marginTop="15dp"
android:textColor="@color/nomerName"
android:textSize="22sp"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</LinearLayout>
<?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"> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal"> <ImageView android:layout_gravity="center" android:id="@+id/touXiang" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10dp" /> <LinearLayout android:layout_width="wrap_content" android:layout_height="75dp" android:orientation="vertical"> <TextView android:text="中文" android:textSize="18dp" android:layout_marginTop="12dp" android:id="@+id/xinMing" android:layout_width="wrap_content" android:layout_height="wrap_content" 4000 android:layout_marginLeft="15dp" android:textColor="@color/nomerName"/> <TextView android:singleLine="true" android:text="今天天气真好" android:id="@+id/qianMing" android:textSize="14dp" android:layout_marginLeft="15dp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="@color/content" android:paddingTop="3dp" /> </LinearLayout> </LinearLayout> </LinearLayout>接下来是Java的内容
先贴JavaBean的封装代码
第一级列表的FirstBean.class
public class FirstBean { String group; List<Bean>childBean; public FirstBean(String group) { this.group = group; childBean=new ArrayList<Bean>(); } public void addChild(Bean bean){ childBean.add(bean); } public int getChilidCount(){ return childBean.size(); } public String getGroup() { return group; } public Bean getChild(int childposition){ return childBean.get(childposition); } }第二级列表的Bean.class
public class Bean { private int imageView; private String name,content; public Bean(int imageView, String name, String content) { this.imageView = imageView; this.name = name; this.content = content; } public int getImageView() { return imageView; } public String getName() { return name; } public String getContent() { return content; } }先贴MyExpandableListAdapter继承自BaseExpandableListAdapter的代码MyExpandableListAdapter.class
package com.example.second_week_listview; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseExpandableListAdapter; import android.widget.ImageView; import android.widget.TextView; import java.util.List; /** * Created by Administrator on 2017/9/20. */ public class MyExpandListViewAdapter extends BaseExpandableListAdapter { private Context context; private List<FirstBean>firstBeanList; private LayoutInflater mlayoutInflater,layoutInflater; public MyExpandListViewAdapter(Context context, List<FirstBean> firstBeanList) { this.context = context; this.firstBeanList = firstBeanList; this.mlayoutInflater = LayoutInflater.from(context); this.layoutInflater=LayoutInflater.from(context); } @Override public int getGroupCount() { return firstBeanList.size(); } @Override public int getChildrenCount(int groupPosition) { return firstBeanList.get(groupPosition).getChilidCount(); } @Override public Object getGroup(int groupPosition) { return firstBeanList.get(groupPosition); } @Override public Object getChild(int groupPosition, int childPosition) { return firstBeanList.get(groupPosition).getChild(childPosition); } @Override public long getGroupId(int groupPosition) { return groupPosition; } @Override public long getChildId(int groupPosition, int childPosition) { return childPosition; } @Override public boolean hasStableIds() { return false; } @Override public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { FirstViewHoder firstViewHoder=null; if (convertView==null){ firstViewHoder=new FirstViewHoder(); convertView=mlayoutInflater.inflate(R.layout.first,null); firstViewHoder.textView= (TextView) convertView.findViewById(R.id.first_tv); convertView.setTag(firstViewHoder); } else { firstViewHoder= (FirstViewHoder) convertView.getTag(); } FirstBean firstBean=firstBeanList.get(groupPosition); firstViewHoder.textView.setText(firstBean.getGroup()); return convertView; } @Override public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { ChildViewHoder childViewHoder=null; if (convertView==null){ childViewHoder=new ChildViewHoder(); convertView=layoutInflater.inflate(R.layout.list_cell,null); childViewHoder.ima= (ImageView) convertView.findViewById(R.id.touXiang); childViewHoder.name= (TextView) convertView.findViewById(R.id.xinMing); childViewHoder.content= (TextView) convertView.findViewById(R.id.qianMing); convertView.setTag(childViewHoder); } else { childViewHoder= (ChildViewHoder) convertView.getTag(); } Bean bean=firstBeanList.get(groupPosition).getChild(childPosition); childViewHoder.ima.setImageResource(bean.getImageView()); childViewHoder.content.setText(bean.getContent()); childViewHoder.name.setText(bean.getName()); return convertView; } @Override public boolean isChildSelectable(int groupPosition, int childPosition) { return false; } class FirstViewHoder{ TextView textView; } class ChildViewHoder{ ImageView ima; TextView name,content; } }这里重写了10个必要方法,主要部分在以下两个方面
getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent)
getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent)groupPosition是第一级点击的位置,childPosition是第二级列表点击的位置,然后其他写法和做了出色封装的BaseAdapter的写法一样,利用viewhoder进行listview优化机制。
下面是主activity的Java代码QQ_main.class
package com.example.second_week_listview; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v7.app.AppCompatActivity; import android.widget.ExpandableListView; import java.util.ArrayList; import java.util.List; /** * Created by Administrator on 2017/9/20. */ public class QQ_main extends AppCompatActivity { private List<FirstBean>firstBeanList=new ArrayList<FirstBean>(); private MyExpandListViewAdapter adapter; private ExpandableListView listView; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_list_view__main); init(); adapter=new MyExpandListViewAdapter(QQ_main.this,firstBeanList); listView.setAdapter(adapter); } private void init() { listView= (ExpandableListView) findViewById(R.id.listView); FirstBean f1=new FirstBean("我的好友"); f1.addChild(new Bean(R.drawable.one,"钟玲","[TIM在线]生活就像一场戏,我希望每天都好。")); f1.addChild(new Bean(R.drawable.two,"若原","[wifi在线]不愿种花就是不愿意开见花凋零。")); f1.addChild(new Bean(R.drawable.three,"王欢","[手机在线]我们需要完美!")); f1.addChild(new Bean(R.drawable.four,"李明","[wifi在线]事无巨细,用心就好。")); f1.addChild(new Bean(R.drawable.five,"姜晴","[4G在线]早安~ 凉凉的天气~")); f1.addChild(new Bean(R.drawable.six,"刘艺莲","[wifi在线]徒步,去探寻人生的意义。")); f1.addChild(new Bean(R.drawable.seven,"欧舒云","[wifi在线]种好你的红豆,别管他人的花生。")); f1.addChild(new Bean(R.drawable.eight,"海萌","[wifi在线]我有所念人,隔在远远乡。")); f1.addChild(new Bean(R.drawable.nine,"远路","[4G在线]身残志不残……")); f1.addChild(new Bean(R.drawable.ten,"雨欣","[手机在线]使我有身后名,不如即时一杯酒。")); firstBeanList.add(f1); FirstBean f2=new FirstBean("朋友"); f2.addChild(new Bean(R.drawable.one,"钟玲","[TIM在线]生活就像一场戏,我希望每天都好。")); f2.addChild(new Bean(R.drawable.two,"若原","[wifi在线]不愿种花就是不愿意开见花凋零。")); f2.addChild(new Bean(R.drawable.three,"王欢","[手机在线]我们需要完美!")); f2.addChild(new Bean(R.drawable.four,"李明","[wifi在线]事无巨细,用心就好。")); f2.addChild(new Bean(R.drawable.five,"姜晴","[4G在线]早安~ 凉凉的天气~")); f2.addChild(new Bean(R.drawable.six,"刘艺莲","[wifi在线]徒步,去探寻人生的意义。")); f2.addChild(new Bean(R.drawable.seven,"欧舒云","[wifi在线]种好你的红豆,别管他人的花生。")); f2.addChild(new Bean(R.drawable.eight,"海萌","[wifi在线]我有所念人,隔在远远乡。")); f2.addChild(new Bean(R.drawable.nine,"远路","[4G在线]身残志不残……")); f2.addChild(new Bean(R.drawable.ten,"雨欣","[手机在线]使我有身后名,不如即时一杯酒。")); firstBeanList.add(f2); FirstBean f3=new FirstBean("家人"); f3.addChild(new Bean(R.drawable.one,"钟玲","[TIM在线]生活就像一场戏,我希望每天都好。")); f3.addChild(new Bean(R.drawable.two,"若原","[wifi在线]不愿种花就是不愿意开见花凋零。")); f3.addChild(new Bean(R.drawable.three,"王欢","[手机在线]我们需要完美!")); f3.addChild(new Bean(R.drawable.four,"李明","[wifi在线]事无巨细,用心就好。")); f3.addChild(new Bean(R.drawable.five,"姜晴","[4G在线]早安~ 凉凉的天气~")); f3.addChild(new Bean(R.drawable.six,"刘艺莲","[wifi在线]徒步,去探寻人生的意义。")); f3.addChild(new Bean(R.drawable.seven,"欧舒云","[wifi在线]种好你的红豆,别管他人的花生。")); f3.addChild(new Bean(R.drawable.eight,"海萌","[wifi在线]我有所念人,隔在远远乡。")); f3.addChild(new Bean(R.drawable.nine,"远路","[4G在线]身残志不残……")); f3.addChild(new Bean(R.drawable.ten,"雨欣","[手机在线]使我有身后名,不如即时一杯酒。")); firstBeanList.add(f3); } }下面是用到的图片名称资源
相关文章推荐
- android开发之ExpandableListView的使用,实现类似QQ好友列表
- android端获取网络数据添加到二级列表ExpandableListView
- Android ExpandableListView使用(1):使用二维数组封装数据
- 具体评论ExpandableListView显示和查询模仿QQ组列表用户信息
- Android 仿QQ好友分组列表、ExpandableListView的使用详解
- 详细讲解ExpandableListView显示和查询仿QQ分组列表用户信息
- ExpandableListView万能adapter封装轻松实现分组列表及单选效果
- android ListView数据变动后保持屏幕位置不变(类似QQ对话列表)
- 05.实例篇:仿QQ好友列表——ExpandableListView可扩展列表的使用(上篇)
- Android UI视图效果篇之仿QQ好友列表分组悬浮PinnedHeaderExpandableListView
- ExpandableListView的使用,实现类似QQ好友列表
- Android UI视图效果篇之仿QQ好友列表分组悬浮PinnedHeaderExpandableListView
- 仿QQ列表--Android控件ExpandableListView的使用
- [Android实例] Expandablelistview实现,仿QQ人员列表,自制Shape
- Android 仿QQ好友列表分组悬浮PinnedHeaderExpandableListView
- QQ好友列表数据模型封装
- Android ExpandableListView 仿QQ列表页面,title永远在上面,带下拉刷新,上拉加载。
- Android开发系列(九) ExpandableListView进阶使用——QQ联系人列表(升级版)
- 自定义ExpandableListView 实现像QQ好友列表一样的功能
- Android学习第五天————ExpandableListView组件通过适配器BaseExpandableListAdapter实现两层列表项