什么是RecyclerView
RecyclerView是当前主流用于显示列表的UI控件.
基础样例
效果图
方案简要介绍
在app模块build.gradle文件中增加如下依赖
implementation 'androidx.recyclerview:recyclerview:1.1.0'
在activity对应的布局文件中增加
RecyclerView
<androidx.recyclerview.widget.RecyclerViewandroid:id="@+id/recyclerView"android:layout_width="match_parent"android:layout_height="match_parent" />
新增一个Adapter用于展示列表中每一行内容(含对应布局文件)
详见下面完整代码中的RvAdapter及其布局文件.在activity中实例化Adapter,设置数据,并将adapter设置给RecyclerView
private fun initRecyclerView() {
var dataList = getData()
val adapter = RvAdapter()
adapter.setData(dataList)
recyclerView.adapter = adapter
recyclerView.layoutManager = LinearLayoutManager(this)
}
private fun getData(): List {val dataList = ArrayList()for (index in 0 until 100) {val text = " 数据$index "
dataList.add(text)
}return dataList
}
完整代码
activity代码:MainActivity
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
initRecyclerView()
}
private fun initRecyclerView() {
var dataList = getData()
val adapter = RvAdapter()
adapter.setData(dataList)
recyclerView.adapter = adapter
recyclerView.layoutManager = LinearLayoutManager(this)
}
private fun getData(): List {val dataList = ArrayList()for (index in 0 until 100) {val text = " 数据$index "
dataList.add(text)
}return dataList
}
}
MainActivity对应布局文件: activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent">
<androidx.recyclerview.widget.RecyclerViewandroid:id="@+id/recyclerView"android:layout_width="match_parent"android:layout_height="match_parent" />
LinearLayout>
RecyclerView对应Adapter: RvAdapter
class RvAdapter : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
private var mDataList = mutableListOf()private lateinit var mContext: Contextoverride fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
mContext = parent.contextval view = LayoutInflater.from(mContext).inflate(R.layout.item_layout, parent, false)return ViewHolder(view)
}override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {val data = mDataList[position]//更新UI上nameTv展示内容
holder.itemView.nameTv.text = data//设置点击事件
holder.itemView.setOnClickListener {
Toast.makeText(mContext, data, Toast.LENGTH_SHORT).show()
}
}fun setData(dataList: List<String>) {
mDataList.clear()
mDataList.addAll(dataList)
notifyDataSetChanged()
}override fun getItemCount(): Int = mDataList.sizeclass ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView)
}
RvAdapter对应布局文件: item_layout.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="vertical">
<TextViewandroid:id="@+id/nameTv"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginStart="20dp"tools:text="姓名" />
<Viewandroid:layout_width="match_parent"android:layout_height="1dp"android:layout_marginStart="15dp"android:layout_marginEnd="15dp"android:layout_marginTop="5dp"android:background="#E7E7E7" />
LinearLayout>
基础样例完整源代码
https://gitee.com/cxyzy1/recyclerView_Demo
关注头条号,第一时间获取最新文章: