关注 安卓007 ,免费获取全套安卓开发学习资料
RecyclerView是当前主流用于显示列表的UI控件.
implementation 'androidx.recyclerview:recyclerview:1.1.0'
RecyclerView
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
private fun initRecyclerView() {
var dataList = getData()
val adapter = RvAdapter()
adapter.setData(dataList)
recyclerView.adapter = adapter
recyclerView.layoutManager = LinearLayoutManager(this)
}
private fun getData(): List<String> {
val dataList = ArrayList<String>()
for (index in 0 until 100) {
val text = " 数据$index "
dataList.add(text)
}
return dataList
}
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<String> {
val dataList = ArrayList<String>()
for (index in 0 until 100) {
val text = " 数据$index "
dataList.add(text)
}
return dataList
}
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
LinearLayout>
class RvAdapter : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
private var mDataList = mutableListOf<String>()
private lateinit var mContext: Context
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
mContext = parent.context
val 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.size
class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView)
}
<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">
<TextView
android:id="@+id/nameTv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
tools:text="姓名" />
<View
android: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
Kotlin语言基础
UI控件_TextView
UI控件_EditText
UI控件_Button
UI控件_ImageView
UI控件_RadioButton
UI控件_CheckBox
UI控件_ProgressBar
关注头条号,第一时间获取最新文章: