• 结合viewBinding实现RecyclerView组件的滚动列表显示


    RecyclerView是一个列表显示的工具,可以将内容以列表的方式进行显示。在本文中将结合viewBinding来实现数据的绑定。
    一、定义实体类
    data class Robot(val imageId:Int,val title:String,val message:String)

    二、定义单项数据的布局
    单项数据的布局item_robot.xml嵌入在CardView中,代码如下所示:

    <?xml version="1.0" encoding="utf-8"?>
    <androidx.cardview.widget.CardView
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tool"
    
        android:id="@+id/cardView"
        android:layout_margin="5dp"
        app:cardBackgroundColor="@android:color/holo_blue_bright"
        app:cardCornerRadius="5dp"
        app:cardElevation="3dp"
        app:contentPadding="4dp"
    
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
            <ImageView
                android:id="@+id/imageView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                app:srcCompat="@android:mipmap/sym_def_app_icon" />
            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="vertical">
                <TextView
                    android:id="@+id/nameTxt"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Robot"
                    android:textSize="20sp" />
                <TextView
                    android:id="@+id/descTxt"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="安卓机器人"
                    android:textSize="24sp" />
            </LinearLayout>
    
        </LinearLayout>
    
    </androidx.cardview.widget.CardView>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46

    三、定义适配器
    适配器实现将数据和视图的绑定。

    class RobotAdapter(val robots:List<Robot>):
        RecyclerView.Adapter<RobotAdapter.ViewHolder>() {
            inner class ViewHolder(val binding: ItemRobotBinding):
                RecyclerView.ViewHolder(binding.root){
                fun bindData(robot:Robot){
                    binding.imageView.setImageResource(robot.imageId)
                    binding.nameTxt.text = robot.title
                    binding.descTxt.text = robot.message
                    binding.imageView.setOnClickListener {
                        Snackbar.make(binding.root,"显示${robot.toString()}",Snackbar.LENGTH_LONG).show()
                    }
                 
                }
            }
        /**创建视图*/
        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
            val binding = ItemRobotBinding.inflate(LayoutInflater.from(parent.context),
                parent,
                false)//视图绑定item_robot.xml
            return ViewHolder(binding)
        }
    
        /**绑定数据*/
        override fun onBindViewHolder(holder: ViewHolder, position: Int) {
            val robot = robots[position]
            holder.bindData(robot)
        
        }
    
        override fun getItemCount(): Int=robots.size
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31

    四、在碎片FirstFragment中加载RecyclerView
    修改上一篇"Fragment碎片切换"的FirstFragment的内容
    (1)FirstFragment对应的布局fragment_first.xml

    <?xml version="1.0" encoding="utf-8"?>
    <androidx.recyclerview.widget.RecyclerView
        android:background="@android:color/holo_green_light"
        android:id="@+id/recyclerView"
        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"
        tools:context=".AboutFragment"/>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    (2)FirstFragment显示滚动列表的处理

    object FirstFragment : Fragment() {
        lateinit var binding:FragmentAboutBinding
        lateinit var robots:ArrayList<Robot>
    
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            robots = ArrayList<Robot>()
       
            for(i in 0..10){
                robots.add(Robot(R.mipmap.ic_launcher,"机器人","编号:${i+1}"))
            }
        }
    
        override fun onCreateView(
            inflater: LayoutInflater, container: ViewGroup?,
            savedInstanceState: Bundle?
        ): View? {
            binding = FragmentAboutBinding.inflate(inflater,container,false)
            val adapter = RobotAdapter(robots)
            binding.recyclerView.layoutManager =
                StaggeredGridLayoutManager(1,StaggeredGridLayoutManager.VERTICAL)//瀑布布局显示
            binding.recyclerView.adapter = adapter
    
            return binding.root
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26

    运行结果如下
    滚动列表显示的效果

    参考文献
    《Android移动应用开发(微课版)》 陈轶 清华大学出版社 
     ISBN:978-7-302-59734-6

  • 相关阅读:
    【Linux-Day10-信号量,共享内存,消息队列】
    初识webGL
    (JavaSE)抽象类和接口
    Linux下Jenkins服务器安装与使用
    【力扣每日一题】2023.10.7 股票价格跨度
    Scroll L2 rollup提交到L1的全流程解析
    取产品之道、赚效率的钱,锅圈万店背后的赢家法则
    Cesium3Dtilesets 使用customShader的解读以及泛光效果示例
    谈谈对跨域(跨源)的一些理解
    操作系统微内核和宏内核
  • 原文地址:https://blog.csdn.net/userhu2012/article/details/127550258