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

边栏推荐
- 学生成绩管理系统(C语言版)
- MySQL数据库之JDBC编程
- Obtain geographic location and coordinates according to ip address (offline method)
- Function 函数式接口及应用
- Understand JDBC in one article
- [Mozhe Academy] Identity Authentication Failure Vulnerability Actual Combat
- TDengineGUI无法连接TDengine
- Go简单实现协程池
- SQL Server 数据库之生成与执行 SQL 脚本
- Powerhouse Cup Preliminary WP
猜你喜欢
随机推荐
2022CISCNmisc
[PASECA2019]honey_shop
MySQL - Multi-table query and case detailed explanation
Bypassing the file upload vulnerability
JDBC programming of MySQL database
Jackson serialization failure problem - oracle data return type can't find the corresponding Serializer
Jdbc & Mysql timeout分析
C#预定义数据类型简介
3分钟告诉你如何成为一名黑客|零基础到黑客入门指南,你只需要掌握这五点能力
使用PyQt5为YoloV5添加界面(一)
Detailed MySQL-Explain
awd --waf deployment
Offensive and defensive world easy_web
Misc of CTF-image steganography
[MATLAB] Image Processing - Recognition of Traffic Signs
SSTI range
【SQL】SQL 高频面试题英语版(1)
C#利用开源NPlot实现K线图(蜡烛图)
C#下利用开源NPlot绘制股票十字交叉线
mysql is not an internal or external command, nor is it a runnable program or batch file to resolve








