100字范文,内容丰富有趣,生活中的好帮手!
100字范文 > recyclerview简单实现单选多选反选全选

recyclerview简单实现单选多选反选全选

时间:2018-11-05 04:34:45

相关推荐

recyclerview简单实现单选多选反选全选

业务代码中的选择工具类,一个简单的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

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。