Android RecyclerView 效果之树形折叠效果

效果.png

项目中遇到类似,省/市/县/乡/村 这种多层级数据展示的效果.原先处理过两层或者多层数据嵌套展示,没有处理过这种多层数据嵌套展示的效果所以在此记录一下,增强点记忆.

需求:省/市/县/乡/村 多层展示单选最小单位选中

思路:

  • 数据整理,数据处理(list 无线嵌套list)
  • 用RecyclerView 子条目也是RecyclerView 承载数据

难点:

  • 底层数据获取上层数据的属性(必要属性建议拼接携带)
  • 不同层级点击事件处理(建议只处理需要点击的和展开缩放的)
  • RecyclerView 数据刷新闪烁问题(局部刷新,不刷新整条或者整个RecycerView)

功能实现

1. 数据类 NodeInfo.class

package com.wu.material.activity.rv.model


/**
 * @author wkq
 *
 * @date 2022年03月15日 9:35
 *
 *@des
 *
 */

data class NodeInfo(
        var isExpen: Boolean,
        val description: String,
        val domainid: Int,
        val full_name: String,
        val iCode: String,
        val id: Int,
        val label_icon: String,
        val name: String,

        val orderindex: Int,
        val parentName: String,
        val parentid: Int,
        val regtype: Int,
        val sCode: String,
        val sn: String,
        val usecode: Boolean,
        //嵌套的list(重要)
        val nodes: List<NodeInfo>
)

2. Adapter 数据

2.1 DemoTreeAdapter.class

package com.wu.material.activity.rv.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.SimpleItemAnimator
import com.wu.material.R
import com.wu.material.activity.rv.model.NodeInfo
import com.wu.material.activity.rv.model.TreeInfo


/**
 * @author wkq
 *
 * @date 2022年03月14日 13:28
 *
 *@des
 *
 */

class DemoTreeAdapter(mContext: Context) : RecyclerView.Adapter<KtViewHolder>() {
    var mContext: Context? = null
    var listData = ArrayList<NodeInfo>()

    init {
        this.mContext = mContext
    }

    var listener: ItemClickListener? = null
    fun setOnItemClickListener(listener: ItemClickListener) {
        this.listener = listener
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): KtViewHolder {

        var view =
            LayoutInflater.from(mContext).inflate(R.layout.item_rv_tree_content, parent, false)
        var viewHolder = KtViewHolder(view)
        return viewHolder
    }

    fun getItem(position: Int): NodeInfo {
        return listData.get(position)
    }

    // 局部刷新
    override fun onBindViewHolder(holder: KtViewHolder, position: Int, payloads: MutableList<Any>) {
        super.onBindViewHolder(holder, position, payloads)
        var info = listData.get(position)
        var iconImageView = holder.getAddressTypeImageView()
        var recycleView = holder.getRecyclerView()
        if (payloads != null && payloads!!.size > 0 && "1".equals(payloads.get(0))) {
            if (info.nodes.size == 0) {
                iconImageView.visibility = View.GONE
            } else {
                iconImageView.visibility = View.VISIBLE
                if (info.isExpen) {
                    recycleView.visibility = View.VISIBLE
                } else {
                    recycleView.visibility = View.GONE
                }
            }
            if (info.isExpen) {
                iconImageView.setBackgroundResource(R.mipmap.iv_address_delete)
            } else {
                iconImageView.setBackgroundResource(R.mipmap.iv_address_add)
            }
        } else {
            super.onBindViewHolder(holder, position, payloads);
        }

    }

    override fun onBindViewHolder(holder: KtViewHolder, position: Int) {
        var mHolder = holder
        var info = listData.get(position)
        var titleTextView = mHolder.getTextView()
        titleTextView.text = listData.get(position).name
        var recycleView = mHolder.getRecyclerView()
        var iconImageView = mHolder.getAddressTypeImageView()
        recycleView.layoutManager = LinearLayoutManager(mContext)
        var contentAdapter = DemoTreeAdapter(mContext!!)
        recycleView.adapter = contentAdapter
        if (info.nodes.size == 0) {
            iconImageView.visibility = View.GONE
        } else {
            iconImageView.visibility = View.VISIBLE
            if (info.isExpen) {
                recycleView.visibility = View.VISIBLE
            } else {
                recycleView.visibility = View.GONE
            }
            contentAdapter.addItems(info.nodes)
        }

        if (info.isExpen) {
            iconImageView.setBackgroundResource(R.mipmap.iv_address_delete)
        } else {
            iconImageView.setBackgroundResource(R.mipmap.iv_address_add)
        }

        contentAdapter.setOnItemClickListener(object : DemoTreeAdapter.ItemClickListener {
            override fun onItemClick(position: Int) {
                var info = contentAdapter.getItem(position)
                if (info.nodes.size == 0) {
                    Toast.makeText(mContext, info.full_name, Toast.LENGTH_SHORT).show()
                } else {
                    info.isExpen = !info.isExpen
                    contentAdapter.notifyItemChanged(position, "1")
                }

            }
        })

        titleTextView!!.setOnClickListener {
            if (listener != null) {
                listener!!.onItemClick(position)
            }
        }


    }

    override fun getItemCount(): Int {
        return listData.size
    }

    override fun getItemViewType(position: Int): Int {
        return super.getItemViewType(position)
    }

    fun addItems(items: List<NodeInfo>) {
        listData.addAll(items)
        //刷新数据
        notifyDataSetChanged()
    }

    interface ItemClickListener {
        fun onItemClick(position: Int)
    }

}

2.2 adapter子条目的布局

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="10dp"
    android:layout_marginTop="10dp"
    android:layout_marginRight="10dp">

    <LinearLayout
        android:id="@+id/ll_top"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/white"
        android:paddingLeft="10dp"
        android:orientation="horizontal"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <ImageView

            android:id="@+id/iv_icon_type"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:background="@mipmap/iv_address_add" />

        <ImageView
            android:id="@+id/iv_icon"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_marginLeft="10dp"
            android:background="@mipmap/iv_address_icon" />

        <TextView
            android:id="@+id/tv_content"
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:layout_gravity="center_vertical"
            android:layout_marginLeft="10dp"
            android:ellipsize="end"
            android:gravity="center_vertical"
            android:lines="1"
            android:text="这是测试Item"
            android:textColor="#333333" />
    </LinearLayout>


    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/rv_content"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/ll_top" />


</androidx.constraintlayout.widget.ConstraintLayout>

2.3 holder类

package com.wu.material.activity.rv.adapter

import android.view.View
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.wu.material.R


/**
 * @author wkq
 *
 * @date 2022年03月14日 13:29
 *
 *@des
 *
 */

class KtViewHolder(view: View) : RecyclerView.ViewHolder(view) {
    var rootView: View? = null

    init {
        rootView = view
    }

    fun getTextView(): TextView {
        var tvContent = rootView!!.findViewById<TextView>(R.id.tv_content)
        return tvContent
    }
    fun getImageView(): ImageView {
        return rootView!!.findViewById<ImageView>(R.id.iv_icon)
    }

    fun getAddressTypeImageView(): ImageView {
            return rootView!!.findViewById<ImageView>(R.id.iv_icon_type)
    }

    fun getRecyclerView(): RecyclerView {
        return rootView!!.findViewById<RecyclerView>(R.id.rv_content)
    }
}

3. 主要实现

package com.wu.material.activity.rv

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.DataBindingUtil
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.SimpleItemAnimator
import com.google.gson.Gson
import com.wu.material.R
import com.wu.material.activity.rv.adapter.DemoTreeAdapter
import com.wu.material.activity.rv.model.NodeInfo
import com.wu.material.activity.rv.model.TreeContentInfo
import com.wu.material.activity.rv.model.TreeInfo
import com.wu.material.databinding.ActivityRecyclerviewBinding


/**
 * @author wkq
 *
 * @date 2022年02月17日 10:43
 *
 *@des
 *
 */

class RecyclerTreeViewActivity : AppCompatActivity() {

    var datas="{\n" +
            "    \"id\": 46,\n" +
            "    \"name\": \"沧县\",\n" +
            "    \"description\": \"0\",\n" +
            "    \"domainid\": 0,\n" +
            "    \"sCode\": \"1\",\n" +
            "    \"iCode\": null,\n" +
            "    \"orderindex\": 0,\n" +
            "    \"parentid\": 40,\n" +
            "    \"parentName\": null,\n" +
            "    \"regtype\": 0,\n" +
            "    \"sn\": null,\n" +
            "    \"usecode\": false,\n" +
            "    \"full_name\": \"沧县\",\n" +
            "    \"label_icon\": \"rule_icon_region\",\n" +
            "    \"nodes\": [\n" +
            "      {\n" +
            "        \"id\": 49,\n" +
            "        \"name\": \"保定县\",\n" +
            "        \"description\": null,\n" +
            "        \"domainid\": 0,\n" +
            "        \"sCode\": \"1\",\n" +
            "        \"iCode\": null,\n" +
            "        \"orderindex\": 0,\n" +
            "        \"parentid\": 42,\n" +
            "        \"parentName\": null,\n" +
            "        \"regtype\": 0,\n" +
            "        \"sn\": null,\n" +
            "        \"usecode\": false,\n" +
            "        \"full_name\": \"沧县\\\\保定县\",\n" +
            "        \"label_icon\": \"rule_icon_region\",\n" +
            "        \"nodes\": [\n" +
            "          {\n" +
            "            \"id\": 50,\n" +
            "            \"name\": \"保定乡\",\n" +
            "            \"description\": null,\n" +
            "            \"domainid\": 0,\n" +
            "            \"sCode\": \"1\",\n" +
            "            \"iCode\": null,\n" +
            "            \"orderindex\": 0,\n" +
            "            \"parentid\": 49,\n" +
            "            \"parentName\": null,\n" +
            "            \"regtype\": 0,\n" +
            "            \"sn\": null,\n" +
            "            \"usecode\": false,\n" +
            "            \"full_name\": \"沧县\\\\保定县\\\\保定乡\",\n" +
            "            \"label_icon\": \"rule_icon_region\",\n" +
            "            \"nodes\": [\n" +
            "              {\n" +
            "                \"id\": 52,\n" +
            "                \"name\": \"保定村村村村村村\",\n" +
            "                \"description\": null,\n" +
            "                \"domainid\": 0,\n" +
            "                \"sCode\": \"1\",\n" +
            "                \"iCode\": null,\n" +
            "                \"orderindex\": 0,\n" +
            "                \"parentid\": 50,\n" +
            "                \"parentName\": null,\n" +
            "                \"regtype\": 0,\n" +
            "                \"sn\": null,\n" +
            "                \"usecode\": false,\n" +
            "                \"full_name\": \"沧县\\\\保定县\\\\保定乡\\\\保定村村村村村村\",\n" +
            "                \"label_icon\": \"rule_icon_region\",\n" +
            "                \"nodes\": []\n" +
            "              }\n" +
            "            ]\n" +
            "          }\n" +
            "        ]\n" +
            "      },\n" +
            "      {\n" +
            "        \"id\": 53,\n" +
            "        \"name\": \"灵寿乡\",\n" +
            "        \"description\": null,\n" +
            "        \"domainid\": 0,\n" +
            "        \"sCode\": \"1\",\n" +
            "        \"iCode\": null,\n" +
            "        \"orderindex\": 0,\n" +
            "        \"parentid\": 44,\n" +
            "        \"parentName\": null,\n" +
            "        \"regtype\": 0,\n" +
            "        \"sn\": null,\n" +
            "        \"usecode\": false,\n" +
            "        \"full_name\": \"沧县\\\\灵寿乡\",\n" +
            "        \"label_icon\": \"rule_icon_region\",\n" +
            "        \"nodes\": []\n" +
            "      },\n" +
            "      {\n" +
            "        \"id\": 54,\n" +
            "        \"name\": \"柏乡3\",\n" +
            "        \"description\": null,\n" +
            "        \"domainid\": 0,\n" +
            "        \"sCode\": \"1\",\n" +
            "        \"iCode\": null,\n" +
            "        \"orderindex\": 0,\n" +
            "        \"parentid\": 46,\n" +
            "        \"parentName\": null,\n" +
            "        \"regtype\": 0,\n" +
            "        \"sn\": null,\n" +
            "        \"usecode\": false,\n" +
            "        \"full_name\": \"沧县\\\\柏乡3\",\n" +
            "        \"label_icon\": \"rule_icon_region\",\n" +
            "        \"nodes\": []\n" +
            "      },\n" +
            "      {\n" +
            "        \"id\": 48,\n" +
            "        \"name\": \"柏乡2\",\n" +
            "        \"description\": \"0\",\n" +
            "        \"domainid\": 0,\n" +
            "        \"sCode\": \"123\",\n" +
            "        \"iCode\": null,\n" +
            "        \"orderindex\": 0,\n" +
            "        \"parentid\": 46,\n" +
            "        \"parentName\": null,\n" +
            "        \"regtype\": 0,\n" +
            "        \"sn\": null,\n" +
            "        \"usecode\": false,\n" +
            "        \"full_name\": \"沧县\\\\柏乡2\",\n" +
            "        \"label_icon\": \"rule_icon_region\",\n" +
            "        \"nodes\": []\n" +
            "      },\n" +
            "      {\n" +
            "        \"id\": 47,\n" +
            "        \"name\": \"柏乡1\",\n" +
            "        \"description\": \"1234\",\n" +
            "        \"domainid\": 0,\n" +
            "        \"sCode\": \"1234\",\n" +
            "        \"iCode\": null,\n" +
            "        \"orderindex\": 0,\n" +
            "        \"parentid\": 46,\n" +
            "        \"parentName\": null,\n" +
            "        \"regtype\": 0,\n" +
            "        \"sn\": null,\n" +
            "        \"usecode\": false,\n" +
            "        \"full_name\": \"沧县\\\\柏乡1\",\n" +
            "        \"label_icon\": \"rule_icon_region\",\n" +
            "        \"nodes\": []\n" +
            "      }\n" +
            "    ]\n" +
            "  }"
    var binding: ActivityRecyclerviewBinding? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = DataBindingUtil.setContentView<ActivityRecyclerviewBinding>(this, R.layout.activity_recyclerview)


        initView()


    }

    private fun initView() {
        var mAdapter = DemoTreeAdapter(this)
        //设置LayoutManager
        var linearLayoutManager = LinearLayoutManager(this)
        binding!!.rvContent.layoutManager = linearLayoutManager
        //设置 Adapter
        binding!!.rvContent.adapter = mAdapter

        var listData = ArrayList<NodeInfo>()
        //刷新数据
        var gson= Gson();

        var info= gson.fromJson(datas,NodeInfo::class.java)
        listData.add(info)
        mAdapter.addItems(listData)

        mAdapter.setOnItemClickListener(object : DemoTreeAdapter.ItemClickListener {
            override fun onItemClick(position: Int) {
                var info = mAdapter.getItem(position)
                info.isExpen = !info.isExpen
                mAdapter.notifyItemChanged(position, "1")
            }
        })

    }

}

总结

这里简单的运用RecyclerView 实现了简单的树形结构以及处理了经常遇到的问题,一般需求能满足,要是复杂页面需要定制处理

看都看了点个赞再走吧

源码

1.项目源码

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 213,417评论 6 492
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 90,921评论 3 387
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 158,850评论 0 349
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 56,945评论 1 285
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 66,069评论 6 385
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 50,188评论 1 291
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,239评论 3 412
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 37,994评论 0 268
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,409评论 1 304
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,735评论 2 327
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,898评论 1 341
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,578评论 4 336
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,205评论 3 317
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,916评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,156评论 1 267
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 46,722评论 2 363
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 43,781评论 2 351

推荐阅读更多精彩内容