开发语言 Android实战开发-Kotlin教程(入门篇-RecyclerView数据列表的实现) - initData()

📅 2026-11-30 👤 拼多多助力营销技巧 📂 赚钱项目 👁️ 378 次阅读

在移动应用开发中,很多时候我们需要去展示数据记录,如电商应用中的商品列表、订单列表等等一系列的数据记录的展示时,我们需要用到Android的列表组件展示这些数据,列表组件应该是我们遇到到比较常见也是必须要掌握的基础界面布局实现。常见的列表组件有ListView、RecyclerView等,现在实现列表数据展示比较灵活也比较主流的组件是RecyclerView组件,本篇我们用一个完整的示例来实现RecyclerView的数据展示。

创建列表界面

新建一个ListActivity并创建对应的布局文件,在布局文件里添加RecyclerView组件,布局文件如图所示

xmlns:app="http://schemas.android.com/apk/res-auto"

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:padding="10dp"

android:background="@color/white"

android:orientation="vertical">

android:layout_width="match_parent"

android:layout_height="match_parent"

android:layout_weight="1"

android:id="@+id/rv_list"

app:layout_constraintTop_toBottomOf="@+id/text_home"

app:layout_constraintStart_toStartOf="parent"

app:layout_constraintEnd_toEndOf="parent"/>

新建列表适配器

新建一个MyAdapter的类继承RecyclerView.Adapter,具体实现代码如下

class MyAdapter (var context: Context?, var dataList: MutableList): RecyclerView.Adapter() {

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {

val view: View = LayoutInflater.from(context).inflate(R.layout.adapter_view,parent,false)

return MyViewHolder(view)

}

override fun getItemCount(): Int {

return dataList.size

}

override fun onBindViewHolder(holder: MyViewHolder, position: Int) {

holder.tv_name?.text = "姓名:"+dataList[position].name

holder.tv_age?.text = "年龄:"+dataList[position].age

}

class MyViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {

var tv_name: TextView? = null

var tv_age: TextView? = null

init {

tv_name = itemView.findViewById(R.id.tv_name)

tv_age = itemView.findViewById(R.id.tv_age)

}

}

}

创建适配器单条数据布局文件adapter_view,代码如下

android:layout_width="match_parent"

android:layout_height="50dp"

android:orientation="vertical">

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="horizontal"

android:layout_weight="1"

android:gravity="center_vertical">

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text=""

android:id="@+id/tv_name"

android:textSize="18sp"

android:layout_marginLeft="20dp"

android:textColor="@color/black"/>

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text=""

android:id="@+id/tv_age"

android:textSize="14sp"

android:layout_marginLeft="20dp"

android:textColor="@color/black"/>

android:layout_width="match_parent"

android:layout_height="1dp"

android:background="@color/black"/>

方法说明 onCreateViewHolder :引入布局视图,也就是单条数据显示的布局 getItemCount :当前列表数据记录总条数 onBindViewHolder :界面绑定数据

模板类 MyViewHolder 这里我们简单讲一下这个是什么意思,因为列表数据量大,在上下拉动的过程中需要不停的去复用视图,这样才能更高效的展示数据,MyViewHolder 实际上就是为了辅助视图复用的模型类。这里现在不作深入的讲解,目前只需要按照这样的规则去搭建就可以。

实现列表数据展示

在ListActivity中实现如下代码

class ListActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {

super.onCreate(savedInstanceState)

setContentView(R.layout.activity_list)

initData()

}

fun initData()

{

var adapter= MyAdapter(this,getListData())

rv_list.layoutManager = LinearLayoutManager(this)

rv_list.adapter = adapter

} 拼多多帮助力入口

private fun getListData():MutableList

{

var listData:MutableList= ArrayList()

for (i in 0..100)

{

var dataEntity=DataEntity()

dataEntity.name="张三"+i

dataEntity.age=10+i

listData.add(dataEntity)

}

return listData

}

}

编译后即可实现RecyclerView数据列表的基础展示 我们这里只是简单的介绍了RecyclerView的基础使用方法和步骤,熟悉了RecyclerView之后,我们可以利用它实现各种各样复杂的列表界面。

标签: 拼多多助力网站平台 赚钱项目 拼多多助力网 首码项目 手赚APP
拼多多助力营销技巧