业务代码中的选择工具类,一个简单的recyclerAdapter
Quick Setup
dependencies {implementation 'com.joketng:choseutil:1.0.1'}复制代码
Usage
Add RecyclerView to your layout
<android.support.v7.widget.RecyclerViewandroid:id="@+id/rv_chose"android:layout_width="match_parent"android:layout_height="match_parent"/>复制代码
Add LayoutManager
rv_chose.layoutManager = LinearLayoutManager(context)//or set other LayoutManager复制代码
then set recyclerAdapter
choseAdapter = object :ChoseAdapter<MyChoseBean>(this, listContent){override fun bindData(holder: MyViewHolder) {holder.itemView.tv_text.text = listContent[holder.layoutPosition].title}override fun createCustomView(layout: LinearLayout) {LayoutInflater.from(context).inflate(R.layout.item_custom, layout, true)}}.setOnNotSelectedListener {it.itemView.img_check.setImageDrawable(ContextCompat.getDrawable(context, R.drawable.vector_drawable_check_box_off))}.setOnIsSelectedListener {it.itemView.img_check.setImageDrawable(ContextCompat.getDrawable(context, R.drawable.vector_drawable_check_box_on))}.setOnClickListener {// Toast.makeText(context,"${it.adapterPosition}",Toast.LENGTH_SHORT).show()}.setSingleChose(false)复制代码
在createCustomView()方法中添加列表自定义布局文件,在bindData()方法中绑定数据到控件,setOnNotSelectedListener()方法控制没有选中时控件的显示方式,setOnIsSelectedListener()方法控制选中状态下控件的显示方式
传送门 Github Demo