您的位置:首页 > 其它

仿QQ好友列表分组折叠效果

2015-12-04 00:07 309 查看
[code]最近要一个类似QQ好友列表分组折叠效果,经过网友提醒应该使用ExpandableListView,因为其就集成了这个功能,我到网上随便找了文章一看,果然如此,因为工作需要和兴趣的推动,下班做完事后决定做一个小的demo,一来是为了更好地熟练记忆,二是如果能够帮助有需要的人更好!
先来一个简单的效果图,没错我们现在要做的就是这么简单,重在知识。
![效果一](http://img.blog.csdn.net/20151203235520538)
![效果二](http://img.blog.csdn.net/20151203235623308)
废话就不多说了,直接开始。
其实很简单了,首先写一个布局文件activity_main.xml


[code]<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
   >
    <TextView
        android:id="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world"
        android:textSize="20sp" />
    <ExpandableListView
        android:id="@+id/lv_expand"
        android:layout_below="@+id/tv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    </ExpandableListView>
</RelativeLayout>


然后是一个简单的好友bean文件

[code]public class Bean {
    private int id;
    private String userName;
    private String grouName;

    public Bean(int id, String userName, String grouName) {
        this.id = id;
        this.userName = userName;
        this.grouName = grouName;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getGrouName() {
        return grouName;
    }

    public void setGrouName(String grouName) {
        this.grouName = grouName;
    }
}


重点就在这个适配器ExandAdapters,重写相应的功能

[code]public class ExandAdapters extends BaseExpandableListAdapter {

    private List<String> groupList;
    private List<List<Bean>> childList;
    private LayoutInflater inflater;

    public ExandAdapters(Context context,List<String> groupList,List<List<Bean>> childList) {
        this.inflater = LayoutInflater.from(context);
        this.groupList =groupList;
        this.childList =childList;
    }

    //分组总数
    @Override
    public int getGroupCount() {
        return groupList.size();
    }

    //某分组下子属性数
    @Override
    public int getChildrenCount(int groupPosition) {
        return childList.get(groupPosition).size();
    }

    //分组对象
    @Override
    public Object getGroup(int groupPosition) {
        return groupList.get(groupPosition);
    }

    //子属性对象
    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return childList.get(groupPosition).get(childPosition);
    }

    //分组的id
    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    //子item的id
    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public View getGroupView(int groupPosition, boolean b, View view, ViewGroup viewGroup) {
        ViweHolder holder = null;
        if (view == null) {
            holder = new ViweHolder();
            view = inflater.inflate(R.layout.item_list_expand_group, null);
            holder.groupName = (TextView) view.findViewById(R.id.group);

            view.setTag(holder);
        } else {
            holder = (ViweHolder) view.getTag();
        }
        holder.groupName.setText(groupList.get(groupPosition).toString());
        return view;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean b, View view, ViewGroup viewGroup) {
        ViweHolder holder = null;
        if (view == null) {
            holder = new ViweHolder();
            view = inflater.inflate(R.layout.item_list_expand_child, null);
            holder.childName = (TextView) view.findViewById(R.id.info);
            holder.image = (ImageView) view.findViewById(R.id.image);

            view.setTag(holder);
        } else {
            holder = (ViweHolder) view.getTag();
        }
        holder.childName.setText(childList.get(groupPosition).get(childPosition).getUserName());
        holder.image.setImageResource(R.drawable.ww);//图片还是使用默认图片
        return view;
    }

    @Override
    public boolean isChildSelectable(int i, int i1) {
        return true;//子item是否响应
    }

    class ViweHolder {
        TextView groupName;
        TextView childName;
        ImageView image;
    }
}


最后在MainActivity.java文件中和匹配其他适配器一样适配就好。

[code]public class MainActivity extends Activity {

    private ExpandableListView exLv;
    private List<String> groupList;
    private List<List<Bean>> childList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // 初始化ArrayList;
        groupList = new ArrayList<String>();
        childList = new ArrayList<List<Bean>>();

        //伪造数据
        // 初始化一个数组;
        String[] title = new String[]{"我的好友", "死党", "蟑螂", "同学", "公司"};
        // 为ArrayList添加数据;
        for (int i = 0; i < title.length; i++) {
            groupList.add(title[i]);
            List<Bean> list = new ArrayList<Bean>();
            for (int j = 0; j < 12; j++) {
                list.add(new Bean(j, "小明" + j, title[i]));
            }
            // 与父亲同一层;
            childList.add(list);
        }

        exLv = (ExpandableListView) findViewById(lv_expand);
        final ExandAdapters adapter = new ExandAdapters(this, groupList, childList);
        exLv.setAdapter(adapter);

        exLv.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
            @Override
            public boolean onChildClick(ExpandableListView expandableListView, View view, int groupPosition, int childPosition, long l) {

                Toast.makeText(
                        MainActivity.this,
                        "你点击了" + childList.get(groupPosition).get(childPosition).getGrouName() + "\n" + childList.get(groupPosition).get(childPosition).getUserName(),
                        Toast.LENGTH_SHORT).show();
                return false;
            }
        });
    }
}


到这里主要的代码就基本完成了,不过还有两个简单的适配布局文件没有写出,因为比较简单,所以就没有写了,如果有兴趣的可以去下载我的小demo。

源码下载
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: