当前位置:网站首页>二十二、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、运行效果;

边栏推荐
- Briefly describe SSRF
- Misc of CTF-Memory Analysis (Volatility)
- 在不同的服务器上基于docker部署redis主从同步
- MySQL存储引擎
- 2022CISCNmisc
- uncategorized SQLException; SQL state [null]; error code [0]; sql injection violation, syntax error
- MySQL 索引的数据结构及类型
- Obtain geographic location and coordinates according to ip address (offline method)
- JVM Learning (2) Garbage Collector
- Calendar类的习题
猜你喜欢

Defense Ideas for a Type of SMS Vulnerability

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

线程的5种状态

使用PyQt5为YoloV5添加界面(一)

Servlet basic principles and application of common API methods

uni-app installs components using npm commands

Online sql editing query tool sql-editor

Arrays工具类的使用

MySQL - 函数及约束命令

Invalid bound statement (not found)出现的原因和解决方法
随机推荐
Misc of CTF - other types of steganography
在不同的服务器上基于docker部署redis主从同步
DVWA安装教程(懂你的不懂·详细)
mysql is not an internal or external command, nor is it a runnable program or batch file to resolve
Servlet基本原理与常见API方法的应用
Misc-traffic analysis of CTF
C# WPF中监听窗口大小变化事件
学生管理系统
Go简单实现协程池
mysql delete duplicate data in the table, (retain only one row)
国内数字藏品交易平台开发市场会开放二级市场吗
Remember a traffic analysis practice - Anheng Technology (August ctf)
运维工程师面试经历
【OS】操作系统高频面试题英文版(1)
A Spark task tuning 】 【 one day suddenly slow down how to solve
sql concat() function
misc-file steganography of CTF
POI工具类
Arthas 命令解析(watch/tt/sc)
[Net Ding Cup 2020 Qinglong Group] AreUSerialz