Android中RecyclerView点击item展开二级列表的简单实现(代码详细讲解)

Android中RecyclerView可展开二级列表及非展开二级列表(代码详细讲解)

一、大体介绍

  最近在学安卓的基础知识recylcerview,然后去网上搜了很多代码,发现写的比较复杂,于是自己在leader的指导下写了一个简单的代码,并且有完整的代码实现以及详细代码讲解。想要学习的小伙伴可不要错过了!所有的完整代码都标注了!
  最开始学习此view的时候是由浅入深的,因此如果一开始就上来做可展开二级列表会有点难度,因此我们可以从最简单的非展开二级列表来完成,然后循序渐进,做出来可折叠的二级列表。
  首先看一下主界面(对应的是activity_main.xml和MainActivity),也就是如下图所示:

  非折叠菜单的图如下所示,也就是说一级标题和二级标题并没有区分,只是布局有所不同,既一级标题不能展开二级标题:

  折叠版本的如下所示,一级标题可以进行展开和关闭(动图不会做,请见谅):

  同时,可以点击界面:如果点击一级标题则会出现toast响应事件:

  如果点击二级标题,则会直接跳转到新的界面:

  整体文件如下所示:

二、XML界面介绍

1.activity_main.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="horizontal">

    <Button
        android:id="@+id/button1"
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginLeft="60dp"
        android:text="非折叠菜单" />

    <Button
        android:id="@+id/button2"
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginLeft="60dp"
        android:text="折叠菜单"/>
</LinearLayout>

2. nofold_layout.xml

  这个界面主要是用来存放一个recyclerview布局的,对应的是非折叠界面,和fold_layout.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">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/nofold_recyclerview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
</LinearLayout>

3. fold_layout.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">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/fold_recyclerview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
</LinearLayout>

4. parent_caption.xml

  一级标题和二级标题的布局格式是不一样的,因此我采取了两个xml界面来进行布局管理,parent_caption.xml管理的是一级标题,用的都是LinearLayout线性布局:

<?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="wrap_content"
    android:orientation="vertical"
    android:background="#eff7e9">

    <TextView
        android:id="@+id/parent_textview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:padding="15dp"
        android:textSize="16sp"
        android:textStyle="bold" />

    <View
        android:layout_width="match_parent"
        android:layout_height="1px"
        android:background="@color/gray" />

</LinearLayout>

5. child_caption.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="wrap_content"
    android:orientation="vertical">

    <TextView
        android:id="@+id/child_textview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="30dp"
        android:layout_marginLeft="30dp"
        android:padding="20dp"
        android:textSize="16sp"
        android:textStyle="bold" />

    <View
        android:layout_width="match_parent"
        android:layout_height="1px"
        android:background="@color/gray" />

</LinearLayout>

三、MapBean介绍

  我创建了一个MapBean类来封装我需要的变量,参数和方法,整体代码如下所示:

package com.shuting.myapplication.bean;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class MapBean implements Serializable {
   

    public static final int TYPE_PARENT=0;//父类标题,即一级标题
    public static final int TYPE_CHILD=1;//子类,即二级标题

    private int viewType;   //判断标题类型是TYPE_PARENT还是TYPE_CHILD来对应不同的布局
    private String caption; //标题的内容

	//表示获得的标题内容以及标题类型(父标题还是子标题)
    public MapBean(String caption,int viewType){
   
        this.caption=caption;
        this.viewType=viewType;
    }
	
    public int getViewType(){
   return viewType;}//获得对应的标题类型

    public String getCaption(){
    //获得标题内容
        return caption;
    }

    //折叠展开列表
    private List<MapBean> childList;//定义一个装载多个子类的列表
    private boolean expand=false; //是否展开子项

    public void setChildList(List<MapBean> childList){
    //构建子项列表
        this.childList=childList;
    }
    public List<MapBean> getChildList(){
   //获得存放子标题的列表
        return childList;
    }
评论 18
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值