expandableListview的使用,模仿qq好友分组点击收缩扩展

我主要讲述的是用listview实现、模仿qq好友分组点击收缩、扩展功能

这个是对listview的拓展,用法比较相似,还是需要一个适配器

MainActivity


public class MainActivity extends Activity {
    private ExpandableListView ex;
    //声明一个ExpandableListView 用的数据源
    //组
    private List<ExpandInfo> list=new ArrayList<ExpandInfo>();
    private MyAdapter adapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ex=(ExpandableListView) findViewById(R.id.expand);
        //初始化数据源
        initList();
        adapter=new MyAdapter(MainActivity.this, list);
        ex.setAdapter(adapter);
        //ExpandableListView子条目点击事件
        ex.setOnChildClickListener(new OnChildClickListener() {

            @SuppressLint("WrongConstant")
            @Override
            public boolean onChildClick(ExpandableListView parent, View v,
                                        int groupPosition, int childPosition, long id) {
                String str=((ChildInfo)adapter.getChild(groupPosition, childPosition)).NickName;
                Toast.makeText(MainActivity.this, str, 0).show();
                return false;
            }
        });
    }
    //初始化数据源
    private void initList() {
        ArrayList<String> arrayList = new ArrayList<String>(Arrays.asList("杨波", "鲁育铭", "梁天勉","刘康","赵明","杨波", "鲁育铭", "梁天勉","刘康","赵明"));
        ArrayList<String> arrayList_friend = new ArrayList<String>(Arrays.asList("我的好友", "我的家人", "特别关心","惑","师大径,亲友熟","同行 追随类"));

        for(int i=0;i<6;i++){
            //创建组对象
            ExpandInfo info=new ExpandInfo();
            //循环添加组名
            info.title=arrayList_friend.get(i);
            //创建子条目数据源
            List<ChildInfo> clist=new ArrayList<ChildInfo>();
            for(int j=0;j<10;j++){
                //创建子对象
                ChildInfo childinfo=new ChildInfo();
                //循环添加用户头像和昵称
                childinfo.headID=R.drawable.headimage;
                childinfo.NickName=arrayList.get(j);
                //将子对象添加到子数据源
                clist.add(childinfo);
            }
            //将子数据源赋值给组对象
            info.childList=clist;
            //将组对象添加到总数据源
            list.add(info);
        }
    }
}

public class MyAdapter extends BaseExpandableListAdapter{
    private List<ExpandInfo> list;
    private Context ctx;

    public MyAdapter(Context ctx,List<ExpandInfo> list) {
        this.ctx=ctx;
        this.list=list;
    }
    //组数
    @Override
    public int getGroupCount() {
        return list.size();
    }
    //子数
    @Override
    public int getChildrenCount(int groupPosition) {
        return list.get(groupPosition).childList.size();
    }
    //组的对象
    @Override
    public Object getGroup(int groupPosition) {
        return list.get(groupPosition);
    }
    //获得子的对象
    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return list.get(groupPosition).childList.get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        // TODO Auto-generated method stub
        return childPosition;
    }
    //当子条目ID相同时是否复用
    @Override
    public boolean hasStableIds() {
        return true;
    }
    //isExpanded:展开
    @Override
    public View getGroupView(int groupPosition, boolean isExpanded,
                             View convertView, ViewGroup parent) {
        if(convertView==null)
            convertView=View.inflate(ctx,R.layout.group_layout, null);
        TextView groupTv=(TextView) convertView.findViewById(R.id.group_tv);
        groupTv.setText(list.get(groupPosition).title);
        //组是否展开   如果展开,组变颜色
        if(isExpanded){
            groupTv.setTextColor(Color.BLUE);
        }else{
            groupTv.setTextColor(Color.BLACK);
        }
        return convertView;
    }
    //isLastChild:是否是该组最后子条目
    @Override
    public View getChildView(int groupPosition, int childPosition,
                             boolean isLastChild, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if(convertView==null){
            holder=new ViewHolder();
            convertView=View.inflate(ctx, R.layout.child_layout, null);
            holder.img=(ImageView) convertView.findViewById(R.id.img);
            holder.child_tv=(TextView) convertView.findViewById(R.id.child_tv);
            convertView.setTag(holder);
        }else{
            holder=(ViewHolder) convertView.getTag();
        }
        holder.img.setImageResource(list.get(groupPosition).childList.get(childPosition).headID);
        holder.child_tv.setText(list.get(groupPosition).childList.get(childPosition).NickName);
        //如果是最后一条,最后最后一条变色
        if(isLastChild){
            holder.child_tv.setTextColor(Color.GREEN);
        }else{
            holder.child_tv.setTextColor(Color.BLACK);
        }
        return convertView;
    }
    //子条目是否可以被点击/选中/选择
    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        // TODO Auto-generated method stub
        return true;
    }
    private class ViewHolder{
        private ImageView img;
        private TextView child_tv;
    }
}

然后需要一个组类和child类

public class ChildInfo {
    public int headID;//每个用户的头像
    public String NickName;//用户的昵称
}
``
`//组信息
public class ExpandInfo {
    public String title;//组的名字
    public List<ChildInfo> childList;//组下面对应的子数据源
}`在这里插入代码片`
child_layout

<?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:layout_width="match_parent"
        android:layout_height="45dp"
        android:layout_marginLeft="10dp"
        >
        <ImageView
            android:id="@+id/img"
            android:layout_width="45dp"
            android:layout_height="match_parent"
            android:src="@drawable/headimage"
            />
        <TextView
            android:id="@+id/child_tv"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:textSize="20sp"
            android:text="aaaaaa"
            android:layout_toRightOf="@id/img"
            android:gravity="center_vertical"
            android:layout_marginLeft="5dp"
            />
    </RelativeLayout>

</LinearLayout>
group_layout

<?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/group_tv"
        android:layout_width="wrap_content"
        android:layout_height="50dp"
        android:gravity="center_vertical"
        android:text="   aaaaa"
        android:textSize="25sp" />

</LinearLayout>
发布了46 篇原创文章 · 获赞 12 · 访问量 1591

猜你喜欢

转载自blog.csdn.net/weixin_43605701/article/details/103753648