Android学习笔记:用ExpandableListView实现qq好友列表

这篇博客介绍了如何在Android环境下,利用ExpandableListView实现类似QQ好友列表的功能。通过实现适配器的getGroupView和getChildView方法,以及自定义布局文件,详细展示了在Linux下的Android Studio中,针对Android 5.1 API 22的开发过程。
摘要由CSDN通过智能技术生成

ExpandableListView是Listview的一个父类,在实现ExpandableListView中,最终要的就是实现它的适配器。适配器中的主要实现getGroupView和getChildView两个方法,这两个方法的最要是完成ExpandableListView和ListView的视图功能。
下面是实现的效果:
这里写图片描述
这里写图片描述

实现环境

  1. Linux下Android Studio
  2. Android 5.1 API 22

实现代码

布局文件:fragment_account.xml:

<LinearLayout 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"
    android:orientation="vertical"
    tools:context="com.example.steam.app_mydemon_qq.AccountFragment">

    <include layout="@layout/edit_layout" />

    <include layout="@layout/account_menu_layout" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="2dp"
        android:text="   好友分组"
        android:textSize="16dp" />

    <com.example.steam.app_mydemon_qq.Mypeoplelist
        android:id="@+id/ealistview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="2dp"
        android:groupIndicator="@null"
        android:background="@color/colorwhite">

    </com.example.steam.app_mydemon_qq.Mypeoplelist>


</LinearLayout>

Mypeoplelist 是继承自ExpandableListView的一个自定义控件。
代码如下
Mypeoplelist .class:

package com.example.steam.app_mydemon_qq;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by steam on 15-12-8.
 */
public class Mypeoplelist extends ExpandableListView {
   

    public List<String> groupList = new ArrayList<String>();
    public List<List<String>> subListName=new ArrayList<List<String>>();
    public List<List<Integer>> subListIcon = new ArrayList<List<Integer>>();

    public Mypeoplelist(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        Initdata();
    }


    public Mypeople
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
使用ExpandableListView可以实现QQ好友分组列表。首先需要创建一个ExpandableListView控件,并为其设置适配器。适配器需要继承BaseExpandableListAdapter,并实现以下方法: 1. getGroupCount():获取分组的数量。 2. getChildrenCount():获取某个分组下子项的数量。 3. getGroup():获取某个分组的数据。 4. getChild():获取某个分组下某个子项的数据。 5. getGroupId():获取某个分组的ID。 6. getChildId():获取某个分组下某个子项的ID。 7. hasStableIds():判断分组和子项的ID是否稳定。 8. getGroupView():获取分组的视图。 9. getChildView():获取子项的视图。 10. isChildSelectable():判断子项是否可选中。 在实现适配器的过程中,需要根据数据源的结构来设置分组和子项的数据。例如,可以使用一个List<List<String>>来存储分组和子项的数据,其中外层List表示分组,内层List表示子项。在getGroup()和getChild()方法中,需要根据groupPosition和childPosition来获取对应的数据。 最后,需要为ExpandableListView设置分组的展开和收起事件。可以通过设置OnGroupClickListener和OnChildClickListener来实现。在OnGroupClickListener中,需要根据groupPosition来判断当前分组是否已经展开,如果已经展开则返回false,否则返回true。在OnChildClickListener中,可以根据childPosition来获取对应的数据,并进行相应的操作。 通过以上步骤,就可以实现一个简单的QQ好友分组列表
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值