Android kotlin 实现点击列表item改变颜色(RecyclerView+BRVAH3.0.6+androidx)

文章目录

  • 一、实现效果
  • 二、引入依赖
  • 四、实现源码
    • 1、适配器
    • 2、实现视图

一、实现效果

Android kotlin 实现点击列表item改变颜色(RecyclerView+BRVAH3.0.6+androidx)_第1张图片

二、引入依赖

appbuild.gradle在添加以下代码
1、implementation 'com.github.CymChad:BaseRecyclerViewAdapterHelper:3.0.6',这个里面带的适配器,直接调用就即可

BaseRecyclerViewAdapterHelper简称BRVAH

Android SDK 是否支持BaseRecyclerViewAdapterHelper:3.0.6
android compileSdkVersion 29
android compileSdkVersion 30
android compileSdkVersion 31
android compileSdkVersion 32
android compileSdkVersion 33

这依赖包还需要得到要添加,在Projectbuild.gradle在添加以下代码,不添加就不行

allprojects {
    repositories {
        ...
        maven { url "https://jitpack.io" }//加上
    }
}

四、实现源码

1、适配器

RvAdapter.kt

package com.example.myapplication3.adapter

import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.example.myapplication3.R
import kotlinx.android.synthetic.main.item.view.*


class RvAdapter(layoutResId: Int = R.layout.item) :
    BaseQuickAdapter<String, BaseViewHolder>(layoutResId) {

    override fun convert(holder: BaseViewHolder, item: String) {

        if (mCallBack != null) {
            mCallBack!!.convert(holder, holder.layoutPosition);
        }

        holder.itemView.run {
            tv_content.text = item
        }
    }

    //回调
    private var mCallBack: ItemSelectedCallBack? = null

    fun setItemSelectedCallBack(CallBack: ItemSelectedCallBack?) {
        mCallBack = CallBack
    }

    interface ItemSelectedCallBack {
        fun convert(holder: BaseViewHolder?, position: Int)
    }
}

item布局item.xml


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/item_layout"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:background="@color/white"
    android:gravity="center">
	

    <TextView
        android:id="@+id/tv_content"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="菜单"
        android:textColor="@color/bg"/>
LinearLayout>

2、实现视图

MainActivity.kt

package com.example.myapplication3

import android.os.Bundle
import android.view.View
import android.widget.LinearLayout
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.listener.OnItemClickListener
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.example.myapplication3.adapter.RvAdapter
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity(), OnItemClickListener{

    //记录当前位置
    private var currentPosition = 0

    private var mShowItems: MutableList<String> = ArrayList()

    private val mAdapter by lazy {
        RvAdapter().apply {
            setOnItemClickListener(this@MainActivity)
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        init()
    }

    private fun init() {
        for (i in 0..19) {
            mShowItems.add("菜单$i")
        }
        val layoutManager = LinearLayoutManager(this@MainActivity)
        layoutManager.orientation = LinearLayoutManager.VERTICAL
        recyclerView.layoutManager = layoutManager
        recyclerView.adapter = mAdapter
        mAdapter.setList(mShowItems)
//        mAdapter.setItemSelectedCallBack(this)

        mAdapter.setItemSelectedCallBack(object : RvAdapter.ItemSelectedCallBack {
            override fun convert(holder: BaseViewHolder?, position: Int) {

                //初始化组件
                val xx = holder!!.getView<LinearLayout>(R.id.item_layout)
                val xxx = holder!!.getView<TextView>(R.id.tv_content)

                //判断传入的position是否和当前一致
                if (position == currentPosition){
                    xx.setBackgroundColor(resources.getColor(R.color.bg)) //#E18D12
                    xxx.setTextColor(resources.getColor(R.color.white)) //#FFFFFFFF
                }else{
                    xx.setBackgroundColor(resources.getColor(R.color.white))
                    xxx.setTextColor(resources.getColor(R.color.bg))
                }
            }
        })
    }


    override fun onItemClick(adapter: BaseQuickAdapter<*, *>, view: View, position: Int) {

        //这里赋值
        currentPosition = position
        //每点击一次item就刷新适配器
        mAdapter.notifyDataSetChanged()
    }
}

activity_main.xml


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        tools:listitem="@layout/item"/>
LinearLayout>

你可能感兴趣的:(Android,kotlin开源项目-功能,kotlin,RecyclerView,BRVAH3.0.6,androidx,item)