当前位置:网站首页>二十二、Kotlin进阶学习:简单学习RecyclerView实现列表展示;
二十二、Kotlin进阶学习:简单学习RecyclerView实现列表展示;
2022-07-30 05:43:00 【¥伊人独醉】
1、首先加入RecyclerView依赖包
implementation 'androidx.recyclerview:recyclerview:1.1.0'2、项目列表

3、activity_main文件;

<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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>4、添加列表子布局文件 list_view;

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="a"
android:padding="10dp"
android:textSize="20dp"/>
</LinearLayout>5、添加适配器类 MyAdaptec.kt;
class MyAdapter(var context: Context,var items:List<String>) : RecyclerView.Adapter<MyAdapter.ViewHolder>() {
// 在此进行布局id的绑定
class ViewHolder(view: View): RecyclerView.ViewHolder(view) {
val tv=view.findViewById<TextView>(R.id.tv)
}
// 绑定 子布局
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyAdapter.ViewHolder {
val inflate = LayoutInflater.from(context).inflate(R.layout.list_view, parent, false)
return ViewHolder(inflate)
}
// 列表展示列数
override fun getItemCount(): Int =items.size
// 进行数据赋值
override fun onBindViewHolder(holder: MyAdapter.ViewHolder, position: Int) {
holder.tv.text=items.get(position)
}
}6、MainActivity.kt
class MainActivity : AppCompatActivity() {
lateinit var recyclerView: RecyclerView
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
var items=ArrayList<String>()
for (item in 1..20){
items.add("Kotlin学习"+item)
}
recyclerView=findViewById(R.id.recyclerView)
recyclerView.adapter=MyAdapter(this,items)
recyclerView.layoutManager=LinearLayoutManager(this)
}
}7、运行效果;

边栏推荐
- Function 函数式接口及应用
- sqli-labs shooting range SQL injection learning Less-1
- npm安装和npm安装——保存
- 史上超强最常用SQL语句大全
- Connect to Mysql in the cloud server Docker detailed graphic and text operations (full)
- uni-app installs components using npm commands
- Volatility memory forensics - command shows
- SQL Server安装教程
- 批量自动归集
- oracle row to column, column to row summary
猜你喜欢

Solution to TypeError The view function did not return a valid response. The function either returned None

mysql删除表中重复数据,(只保留一行)

MySQL 5.7 安装教程(全步骤、保姆级教程)

MySQL 数据类型及占用空间

c#下Web3合约空投、转账调用代码

Oracle数据库SQL优化详解
awd --waf deployment

3分钟告诉你如何成为一名黑客|零基础到黑客入门指南,你只需要掌握这五点能力
Go简单实现协程池

DVWA installation tutorial (understand what you don't understand · in detail)
随机推荐
Powerhouse Cup Preliminary WP
oracle row to column, column to row summary
FastAPI 快速入门
C# WPF下限制TextBox只输入数字、小数点、删除等键
[MATLAB]图像处理——交通标志的识别
Servlet基本原理与常见API方法的应用
Bypassing the file upload vulnerability
phpok website vulnerability exploitation analysis
【SQL】SQL 高频面试题英语版(1)
2022CISCNmisc
awd --waf deployment
SQL Server安装教程
学生成绩管理系统(C语言版)
在线sql编辑查询工具sql-editor
【MySQL功法】第5话 · SQL单表查询
Volatility memory forensics - command shows
c#下Web3合约空投、转账调用代码
CTFSHOW command execution [web29-web124] unfinished to be continued
[MATLAB] Image Processing - Recognition of Traffic Signs
Mysql 客户端常见异常分析