首先要在fragment上拖出一个RecyclerView控件
然后更改其属性在其对应的XML文件中添加属性 app:layoutManager=”LinearLayoutManager”
见示例的最后一行
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toTopOf="@+id/guideline54"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/guideline51"
app:layoutManager="LinearLayoutManager"/>
创建一个小的布局文件 显示在recycleView中显示的每一行的内容
cell_sysytem.xml
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/cell_system"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/selectableItemBackground"
android:clickable="true"
android:focusable="true">
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline55"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.2" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline56"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.86" />
<TextView
android:id="@+id/systemTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:layout_marginEnd="140dp"
android:layout_marginBottom="15dp"
android:text="@string/sytemsetting"
android:textColor="@color/black"
android:textSize="24sp"
app:layout_constraintBottom_toTopOf="@+id/guideline57"
app:layout_constraintEnd_toStartOf="@+id/guideline56"
app:layout_constraintStart_toStartOf="@+id/guideline55"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="@+id/imageView2"
android:layout_width="59dp"
android:layout_height="47dp"
android:contentDescription="@string/sytemsetting"
app:layout_constraintBottom_toTopOf="@+id/guideline57"
app:layout_constraintEnd_toStartOf="@+id/guideline55"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/ic_baseline_settings_24" />
<ImageView
android:id="@+id/imageView3"
android:layout_width="50dp"
android:layout_height="32dp"
android:contentDescription="@string/systemSettingsFragment"
app:layout_constraintBottom_toTopOf="@+id/guideline57"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="@+id/guideline56"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/ic_baseline_arrow_forward_ios_24" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline57"
android:layout_width="0dp"
android:layout_height="0dp"
android:orientation="horizontal"
app:layout_constraintGuide_percent="1.0" />
<View
android:id="@+id/divider2"
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@color/black"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/guideline57" />
</androidx.constraintlayout.widget.ConstraintLayout>
显示效果如下:
新建一个Adapter,示例如下:
SettingADapter.kt
package com.cslc.minipos.adapter.systemsettings
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import android.widget.Toast
import androidx.navigation.NavController
import androidx.navigation.Navigation
import androidx.recyclerview.widget.RecyclerView
import com.cslc.minipos.R
/**
* description: 设置的适配器
* author: cenkun
* create time: 2021/3/1 15:06
**/
class SettingAdapter (private val dataSet: Array<String>):
RecyclerView.Adapter<SettingAdapter.ViewHolder>(){
private lateinit var controller: NavController
/**
* Provide a reference to the type of views that you are using
* (custom ViewHolder).
*/
class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
val textView: TextView
init {
// Define click listener for the ViewHolder's View.
textView = view.findViewById(R.id.systemTextView)
}
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
// Create a new view, which defines the UI of the list item
val view = LayoutInflater.from(parent.context)
.inflate(R.layout.cell_system, parent, false)
return ViewHolder(view)
}
/**
* description: 返回条目数
* author: cenkun
* params:
* return
* create time: 2021/3/1 15:15
**/
override fun getItemCount(): Int {
return dataSet.size
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
holder.textView.text = dataSet[position ]
//整个布局的点击事件
holder.itemView.setOnClickListener{
Toast.makeText(it.context,dataSet[position],Toast.LENGTH_SHORT).show()
controller = Navigation.findNavController(it)
val bundle = Bundle()
bundle.putString("my_name",dataSet[position])
when(position){
0 -> controller.navigate(R.id.networkSettingsFragment ,bundle)
1 -> controller.navigate(R.id.applicationSettingsFragment,bundle)
2 -> controller.navigate(R.id.aboutApplicationFragment,bundle)
3 -> controller.navigate(R.id.aboutApplicationFragment,bundle)
4 -> controller.navigate(R.id.aboutApplicationFragment,bundle)
5 -> controller.navigate(R.id.aboutApplicationFragment,bundle)
6 -> controller.navigate(R.id.aboutMachineFragment,bundle)
}
}
}
}
适配器就是一个控制在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布局中的组件显示的内容