首先要在fragment上拖出一个RecyclerView控件
    然后更改其属性在其对应的XML文件中添加属性 app:layoutManager=”LinearLayoutManager”
    见示例的最后一行

    1. <androidx.recyclerview.widget.RecyclerView
    2. android:id="@+id/recyclerView"
    3. android:layout_width="0dp"
    4. android:layout_height="0dp"
    5. app:layout_constraintBottom_toTopOf="@+id/guideline54"
    6. app:layout_constraintEnd_toEndOf="parent"
    7. app:layout_constraintStart_toStartOf="parent"
    8. app:layout_constraintTop_toTopOf="@+id/guideline51"
    9. app:layoutManager="LinearLayoutManager"/>

    创建一个小的布局文件 显示在recycleView中显示的每一行的内容
    cell_sysytem.xml

    1. <?xml version="1.0" encoding="utf-8"?>
    2. <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    3. xmlns:app="http://schemas.android.com/apk/res-auto"
    4. xmlns:tools="http://schemas.android.com/tools"
    5. android:id="@+id/cell_system"
    6. android:layout_width="match_parent"
    7. android:layout_height="wrap_content"
    8. android:background="?attr/selectableItemBackground"
    9. android:clickable="true"
    10. android:focusable="true">
    11. <androidx.constraintlayout.widget.Guideline
    12. android:id="@+id/guideline55"
    13. android:layout_width="wrap_content"
    14. android:layout_height="0dp"
    15. android:orientation="vertical"
    16. app:layout_constraintGuide_percent="0.2" />
    17. <androidx.constraintlayout.widget.Guideline
    18. android:id="@+id/guideline56"
    19. android:layout_width="wrap_content"
    20. android:layout_height="0dp"
    21. android:orientation="vertical"
    22. app:layout_constraintGuide_percent="0.86" />
    23. <TextView
    24. android:id="@+id/systemTextView"
    25. android:layout_width="wrap_content"
    26. android:layout_height="wrap_content"
    27. android:layout_marginTop="30dp"
    28. android:layout_marginEnd="140dp"
    29. android:layout_marginBottom="15dp"
    30. android:text="@string/sytemsetting"
    31. android:textColor="@color/black"
    32. android:textSize="24sp"
    33. app:layout_constraintBottom_toTopOf="@+id/guideline57"
    34. app:layout_constraintEnd_toStartOf="@+id/guideline56"
    35. app:layout_constraintStart_toStartOf="@+id/guideline55"
    36. app:layout_constraintTop_toTopOf="parent" />
    37. <ImageView
    38. android:id="@+id/imageView2"
    39. android:layout_width="59dp"
    40. android:layout_height="47dp"
    41. android:contentDescription="@string/sytemsetting"
    42. app:layout_constraintBottom_toTopOf="@+id/guideline57"
    43. app:layout_constraintEnd_toStartOf="@+id/guideline55"
    44. app:layout_constraintStart_toStartOf="parent"
    45. app:layout_constraintTop_toTopOf="parent"
    46. app:srcCompat="@drawable/ic_baseline_settings_24" />
    47. <ImageView
    48. android:id="@+id/imageView3"
    49. android:layout_width="50dp"
    50. android:layout_height="32dp"
    51. android:contentDescription="@string/systemSettingsFragment"
    52. app:layout_constraintBottom_toTopOf="@+id/guideline57"
    53. app:layout_constraintEnd_toEndOf="parent"
    54. app:layout_constraintStart_toStartOf="@+id/guideline56"
    55. app:layout_constraintTop_toTopOf="parent"
    56. app:srcCompat="@drawable/ic_baseline_arrow_forward_ios_24" />
    57. <androidx.constraintlayout.widget.Guideline
    58. android:id="@+id/guideline57"
    59. android:layout_width="0dp"
    60. android:layout_height="0dp"
    61. android:orientation="horizontal"
    62. app:layout_constraintGuide_percent="1.0" />
    63. <View
    64. android:id="@+id/divider2"
    65. android:layout_width="match_parent"
    66. android:layout_height="1dp"
    67. android:background="@color/black"
    68. app:layout_constraintBottom_toBottomOf="parent"
    69. app:layout_constraintEnd_toEndOf="parent"
    70. app:layout_constraintStart_toStartOf="parent"
    71. app:layout_constraintTop_toTopOf="@+id/guideline57" />
    72. </androidx.constraintlayout.widget.ConstraintLayout>

    显示效果如下:
    image.png

    新建一个Adapter,示例如下:
    SettingADapter.kt

    1. package com.cslc.minipos.adapter.systemsettings
    2. import android.os.Bundle
    3. import android.view.LayoutInflater
    4. import android.view.View
    5. import android.view.ViewGroup
    6. import android.widget.TextView
    7. import android.widget.Toast
    8. import androidx.navigation.NavController
    9. import androidx.navigation.Navigation
    10. import androidx.recyclerview.widget.RecyclerView
    11. import com.cslc.minipos.R
    12. /**
    13. * description: 设置的适配器
    14. * author: cenkun
    15. * create time: 2021/3/1 15:06
    16. **/
    17. class SettingAdapter (private val dataSet: Array<String>):
    18. RecyclerView.Adapter<SettingAdapter.ViewHolder>(){
    19. private lateinit var controller: NavController
    20. /**
    21. * Provide a reference to the type of views that you are using
    22. * (custom ViewHolder).
    23. */
    24. class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
    25. val textView: TextView
    26. init {
    27. // Define click listener for the ViewHolder's View.
    28. textView = view.findViewById(R.id.systemTextView)
    29. }
    30. }
    31. override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
    32. // Create a new view, which defines the UI of the list item
    33. val view = LayoutInflater.from(parent.context)
    34. .inflate(R.layout.cell_system, parent, false)
    35. return ViewHolder(view)
    36. }
    37. /**
    38. * description: 返回条目数
    39. * author: cenkun
    40. * params:
    41. * return
    42. * create time: 2021/3/1 15:15
    43. **/
    44. override fun getItemCount(): Int {
    45. return dataSet.size
    46. }
    47. override fun onBindViewHolder(holder: ViewHolder, position: Int) {
    48. holder.textView.text = dataSet[position ]
    49. //整个布局的点击事件
    50. holder.itemView.setOnClickListener{
    51. Toast.makeText(it.context,dataSet[position],Toast.LENGTH_SHORT).show()
    52. controller = Navigation.findNavController(it)
    53. val bundle = Bundle()
    54. bundle.putString("my_name",dataSet[position])
    55. when(position){
    56. 0 -> controller.navigate(R.id.networkSettingsFragment ,bundle)
    57. 1 -> controller.navigate(R.id.applicationSettingsFragment,bundle)
    58. 2 -> controller.navigate(R.id.aboutApplicationFragment,bundle)
    59. 3 -> controller.navigate(R.id.aboutApplicationFragment,bundle)
    60. 4 -> controller.navigate(R.id.aboutApplicationFragment,bundle)
    61. 5 -> controller.navigate(R.id.aboutApplicationFragment,bundle)
    62. 6 -> controller.navigate(R.id.aboutMachineFragment,bundle)
    63. }
    64. }
    65. }
    66. }

    适配器就是一个控制在RecyclerView组件上面什么时候显示 显示多少东西的控制器
    他需要重写几个方法

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder
    创建一个ViewHolder 将上面的cell_sysytem.xml新建的布局加载进来

    override fun getItemCount(): Int
    返回需要显示的条目数

    override fun onBindViewHolder
    将新加载的ViewHolder和具体的数据加载进来 以及一些相应的控制功能

    以及重写了一个
    class ViewHolder(view: View) : RecyclerView.ViewHolder(view)
    ViewHolder 就是控制每个cell_layout布局中的组件显示的内容