recyclerview点击事件_安卓开发入门教程UI控件_RecyclerView

本文介绍了Android开发中RecyclerView的基础用法,包括其作为主要列表控件的角色,以及如何实现点击事件。通过一个基础样例,展示了在build.gradle中添加依赖、创建Adapter和布局文件的步骤,并提供了完整代码示例链接。
摘要由CSDN通过智能技术生成

什么是RecyclerView

RecyclerView是当前主流用于显示列表的UI控件.

基础样例

效果图

58a069eae1ad980ba368a2f1fccb0ec8.png

方案简要介绍

  1. 在app模块build.gradle文件中增加如下依赖

implementation 'androidx.recyclerview:recyclerview:1.1.0'
  1. 在activity对应的布局文件中增加RecyclerView

<androidx.recyclerview.widget.RecyclerViewandroid:id="@+id/recyclerView"android:layout_width="match_parent"android:layout_height="match_parent" />
  1. 新增一个Adapter用于展示列表中每一行内容(含对应布局文件)
    详见下面完整代码中的RvAdapter及其布局文件.

  2. 在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
}

完整代码

  1. 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
}
}
  1. 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>
  1. 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)
}
  1. 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

关注头条号,第一时间获取最新文章:

ac390df3b721aba0741c1d1e580426a8.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值