RecycleView适配器

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()
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值