Kotlin实践之多类型RecyclerView及列表单选效果

一、初衷

近期项目需要实现类型列表,使用java编写想必大家都已经熟悉,今天使用kotlin实践一把,顺便熟悉下kotlin使用

二、需求

两种条目类型一种是普通的文字类型,另外一种是带有单选框的条目类型

三、分析

类型一使用一个textView布局显示,类型二使用一个textView+imageView布局显示,单选可以通过recyclerView的条目点击事件监听实现,选择一个的同时将其他设置为未选择状态

四、代码实现

1、主activity

使用kotlin结合kotlin-android-extensions插件,连findViewById都不用写,开心~~~

class DiscountListActivity : BaseActivity(), BaseQuickAdapter.OnItemClickListener {

    private var discountList: MutableList<Bean> = ArrayList<Bean>()
    private var adapter: DiscountAdapter? = null
    override fun getLayout(): Int {
        return R.layout.activity_discount_list
    }

     override fun loadData() {
        for (i in 1..10){
            var  bean=Bean()
            if (i%2==1){
                bean.type=Bean.TYPE_ONE
                bean.content="有图片类型"
            }else{
                bean.type=Bean.TYPE_TWO
                bean.content="有radioButton类型$i"
            }

            discountList!!.add(bean)
        }
         rv_item_discount.layoutManager=LinearLayoutManager(context)
         adapter=DiscountAdapter(discountList)
         rv_item_discount?.adapter=adapter
         adapter?.setNewData(discountList)
         adapter?.onItemClickListener = this

    }
    override fun onItemClick(holdr: BaseQuickAdapter<*, *>?, p1: View?, index: Int) {
        if (discountList[index].type==2){
            discountList[index].isChecked=!discountList[index].isChecked
            // setUnChecked
            for ( i in discountList.indices){
                if (i!=index){
                    discountList[i].isChecked=false
                }
            }
            adapter?.notifyDataSetChanged()
            if (discountList[index].isChecked){
                LogUtils.debug(TAG,"选择的内容是"+discountList[index].content)
            }
        }
    }

}

2、adapter

继承BaseMultiItemQuickAdapter实现,节省代码

class DiscountAdapter(data: MutableList<Bean>?) : BaseMultiItemQuickAdapter<Bean, BaseViewHolder>(data) {
// 不同于java使用在构造方法中添加item类型,kotlin要在init方法中添加item类型
    init {
        addItemType(Bean.TYPE_ONE, R.layout.discount_item)
        addItemType(Bean.TYPE_TWO, R.layout.discount_item_two)
    }
    override fun convert(baseViewHolder: BaseViewHolder?, item: Bean?) {
        when (baseViewHolder?.itemViewType) {
            Bean.TYPE_ONE -> baseViewHolder.setText(R.id.tv_discount_item, item?.content)
            Bean.TYPE_TWO -> {
                if (item?.isChecked!!) {
                    baseViewHolder.getView<ImageView>(R.id.iv_item).setBackgroundResource(R.drawable.ic_radio_check)
                } else {
                    baseViewHolder.getView<ImageView>(R.id.iv_item).setBackgroundResource(R.drawable.ic_radio_uncheck)
                }
                baseViewHolder.setText(R.id.tv_discount_item_two, item.content)
            }
            else -> {
            }
        }
    }
}

3、bean类

bean类中要定义item的类型变量方便adapter中使用,注意bean类要实现MultiItemEntity才能使用BaseMultiItemQuickAdapter。

不用写get和set方法,开心~~~


class Bean : MultiItemEntity, Serializable {
    var type: Int = 0
    var content: String? = null
    var isChecked: Boolean = false
    override fun getItemType(): Int {
        return type
    }
    
// kotlin中不能声明静态类变量,可以通过companion来实现

    companion object {
        val TYPE_ONE = 1
        val TYPE_TWO = 2
    }
}

4、布局文件

activity布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >
<android.support.v7.widget.RecyclerView
    android:id="@+id/rv_item_discount"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginTop="50dp"/>
</LinearLayout>

item类型1布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#ff00ff">
<TextView
    android:id="@+id/tv_discount_item"
    android:layout_width="match_parent"
    android:layout_height="40dp"
    android:textSize="20sp"
    android:layout_margin="10dp"
    />
</LinearLayout>

item类型二布局文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="10dp">

    <TextView
        android:id="@+id/tv_discount_item_two"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:paddingLeft="10dp"
        android:text="这是优惠券啦啦啦"
        android:textSize="20sp" />

    <ImageView
        android:id="@+id/iv_item"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:background="@drawable/ic_radio_uncheck"
        android:paddingRight="10dp" />
</RelativeLayout>

效果图
在这里插入图片描述

五:总结

1、使用kotlin结合kotlin-android-extensions插件,省去findViewById,节约代码

2、bean类中不用声明get和set方法,更加简单

3、使用?进行非空判断

4、相比于java使用kotlin语法还是有很多不同,需要一定的时间进行实践来熟悉

5、对于不着急的项目可以采用kotlin进行实践

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值