1.XML
<?xml version="1.0" encoding="utf-8"?>
<layout 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">
<data>
</data>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/purple_200"
android:gravity="center"
android:padding="20dp"
android:text="111111" />
<androidx.recyclerview.widget.RecyclerView
android:overScrollMode="never"
android:id="@+id/viewPage"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</androidx.core.widget.NestedScrollView>
</LinearLayout>
</layout>
2.使用
private val binding: ActivityMainBinding by lazy {
DataBindingUtil.setContentView(this, R.layout.activity_main)
}
private val mAdapter by lazy { MyAdapter() }
@SuppressLint("UseCompatLoadingForDrawables")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding.viewPage.apply {
layoutManager = LinearLayoutManager(this@MainActivity)
val decoration = DividerItemDecoration(context, DividerItemDecoration.VERTICAL)
decoration.setDrawable(resources.getDrawable(R.drawable.divider, null))
addItemDecoration(decoration)
adapter = mAdapter
}
val list = ArrayList<String>()
for (i in 1..50) {
list.add("$i")
}
mAdapter.setDataList(list)
}
3.子条目横线
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="@color/black"></solid>
<size
android:width="4dp"
android:height="1dp" />
</shape>
4.适配器
class MyAdapter : RecyclerView.Adapter<MyAdapter.InnerHolder>() {
private val list = ArrayList<String>()
class InnerHolder(item: View, val binding: ListItemBinding) : RecyclerView.ViewHolder(item)
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): InnerHolder {
// val itemView =
// LayoutInflater.from(parent.context).inflate(R.layout.list_item, parent, false)
val binding: ListItemBinding = DataBindingUtil.inflate(
LayoutInflater.from(parent.context), R.layout.list_item, parent, false
)
return InnerHolder(binding.root, binding)
}
override fun onBindViewHolder(holder: InnerHolder, position: Int) {
holder.binding.tvListItem.text = list[position]
}
override fun getItemCount(): Int = list.size
@SuppressLint("NotifyDataSetChanged")
fun setDataList(dataList: ArrayList<String>) {
list.clear()
list.addAll(dataList)
notifyDataSetChanged()
}
}