实现二级列表(实现QQ的好友列表)

    实现QQ好友的列表功能,可以使用二级列表(ExpandableListView控件),显示标题,可将内容隐藏,同时点击标题,也可将内容展示出来,效果如图所示。


代码实现如下:

首先需要实现标题部分的布局,就是实现途中的分组名(同学,家人,朋友)。

 layout_parent.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:orientation="vertical" >

    <TextView
        android:id="@+id/main_name"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_gravity="center"
        android:gravity="center" />

</LinearLayout>
实现每组内部的布局,同样只需要一个TextView就可以,替换不同的数据即可。

 layout_children.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:orientation="vertical" >

    <TextView
        android:id="@+id/content_name"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:layout_gravity="center"
        android:gravity="center" />

</LinearLayout>
再来就是实现MainActivity中的代码,在这里添加数据,实现Adapter,设置ExpandableListView,代码如下:

public class MainActivity extends Activity {

    ExpandableListView mainlistview = null;
    List<String> mGroup = null;
    Map<String, List<String>> map = null;
    Adapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mainlistview = (ExpandableListView) findViewById(R.id.main_expandablelistview);
        initData();
        adapter=new Adapter();
        mainlistview.setAdapter(adapter);

    }

    private void initData() {
        mGroup = new ArrayList<String>();//向标题列表中添加数据
        mGroup.add("同学");
        mGroup.add("家人");
        mGroup.add("朋友");
        map = new HashMap<String, List<String>>();//每个标题组中的内容由向map中添加,每个key就是一个标题(key值与标题题目相同,方便寻找每个标题下的内容)
        List<String> list1 = new ArrayList<String>();
        list1.add("A");//每个标题中有几个好友,就添加几条数据
        list1.add("B");
        map.put("同学", list1);//将每个标题与每组数据进行绑定

        List<String> list2 = new ArrayList<>();
        list2.add("C");
        list2.add("D");
        map.put("家人", list2);

        List<String> list3 = new ArrayList<>();
        list3.add("E");
        list3.add("F");
        map.put("朋友", list3);
    }

    public class Adapter extends BaseExpandableListAdapter{
//集成BaseExpandableAdapter必须实现以下8个方法
        @Override
        public int getGroupCount() {
            return mGroup.size();//获取标题的个数,即有多少个分组
        }

        @Override
        public int getChildrenCount(int groupPosition) {
            String key=mGroup.get(groupPosition);
            return map.get(key).size();//获取每个小组有多少个好友
        }

        @Override
        public Object getGroup(int groupPosition) {
            return mGroup.get(groupPosition);//获取标题数据,即组名
        }

        @Override
        public Object getChild(int groupPosition, int childPosition) {
            String key=mGroup.get(groupPosition);//获取每组成员
            String child=map.get(key).get(childPosition);
            return child;
        }

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

        @Override
        public long getChildId(int groupPosition, int childPosition) {
            return childPosition;
        }

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

        @Override
        public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {//将分组组名显示出来
            if(convertView==null){
             convertView=LayoutInflater.from(MainActivity.this).inflate(R.layout.layout_parent,null);
            }
            TextView textView=(TextView)convertView.findViewById(R.id.main_name);
            textView.setText(MainActivity.this.mGroup.get(groupPosition));
            return textView;
        }

        @Override
        public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {//将好友列表显示出来
            if(convertView==null){
                convertView=LayoutInflater.from(MainActivity.this).inflate(R.layout.layout_children,null);
            }
            TextView textView=(TextView)convertView.findViewById(R.id.content_name);
            textView.setText(map.get(MainActivity.this.mGroup.get(groupPosition)).get(childPosition));
            return textView;
        }

        @Override
        public boolean isChildSelectable(int groupPosition, int childPosition) {
            return true;
        }
    }

    // 初始化数据


}

以上就是全部代码。

如果不需要图中的按钮(即图中标题旁边的展开按钮),则设置代码:

mainlistView.setGroupIndicator(null);即可去除按钮。

如果不想用Map实现,也可以使用在一个List中放一个List的方式实现对每个小组的数据存储。

List<List<String>> childContent=new ArrayList<List<String>>();
首先设置里面的那个List,用于存放数据。

List<String> content= new ArrayList<String>();

然后设置一个循环,每向这个content中添加一条数据,就把这个content(List对象)对象添加到childrenContent这个外部的List中,然后通过childrenContent.get(groupPosition)的方式与父List进行绑定。后面的设置就跟上面的代码相同了。只是添加数据的过程不同。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值