android手风琴特效(二)

本文详细介绍如何使用Android的ExpandableListView组件构建可展开列表,包括创建适配器、定义实体类和布局,以及实现数据填充的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1 页面中写一个ExpandableListView

<ExpandableListView
    android:id="@+id/id_expandableLi
    android:layout_width="match_pare
    android:layout_height="match_par
    android:groupIndicator="@drawabl
    android:childIndicator="@drawabl
    android:childDivider="@color/col
    ></ExpandableListView>

2 写一个类继承  BaseExpandableListAdapter

public class ChapterAdapter extends BaseExpandableListAdapter {
    private List<Chapter> mDatas;

    public ChapterAdapter(){}


    @Override
    public int getGroupCount() {
        return mDatas.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return mDatas.get(groupPosition).getChildren().size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return mDatas.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return mDatas.get(groupPosition).getChildren().get(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) {
        return null;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        return null;
    }

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

 

3  两个实体类  (一个父实体类包含多个子实体类)

a 父实体类 

public class Chapter {
    private int id;
    private String name;
    private List<ChapterItem>  children = new ArrayList<>();

    public Chapter(){}
    public Chapter(int id, String name) {
        this.id = id;
        this.name = name;
    }



    public void addChild(ChapterItem chapterItem){
        chapterItem.setPid(getId());
        children.add(chapterItem);
    }

    public void addChild(int cid,String cname){
        ChapterItem chapterItem = new ChapterItem(cid,cname);
        chapterItem.setPid(getId());
        children.add(chapterItem);
    }


    public int getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public List<ChapterItem> getChildren() {
        return children;
    }

    public void setChildren(List<ChapterItem> children) {
        this.children = children;
    }




}

b 子实体类

public class ChapterItem {
    private int id;
    private String name;

    public ChapterItem(){}

    public ChapterItem(int id, String name) {
        this.id = id;
        this.name = name;
    }

    private int pid;



    public int getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getPid() {
        return pid;
    }

    public void setPid(int pid) {
        this.pid = pid;
    }
}

4 写两个布局Layout  (父Layout) (子Layout)

a 父Layout

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    xmlns:tools="http://schemas.android.com/tools"
    android:background="@color/color">
    <TextView
        android:id="@+id/id_tv_name"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center_vertical"
        tools:text="android"
        android:textSize="24dp"
        android:textStyle="bold"
        />



</LinearLayout>

 

b子Layout

<TextView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="40dp"
    android:gravity="center_vertical"
    android:textSize="16dp"
    android:id="@+id/text_tv_name"
    >


</TextView>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值