赞
踩
在app
的build.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 | 是 |
这依赖包还需要得到要添加,在Project
的build.gradle
在添加以下代码,不添加就不行
allprojects {
repositories {
...
maven { url "https://jitpack.io" }//加上
}
}
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
<?xml version="1.0" encoding="utf-8"?> <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"> <!--<color name="white">#FFFFFFFF</color>--> <TextView android:id="@+id/tv_content" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="菜单" android:textColor="@color/bg"/> </LinearLayout>
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)) //<color name="bg">#E18D12</color> xxx.setTextColor(resources.getColor(R.color.white)) //<color name="white">#FFFFFFFF</color> }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
<?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"
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>
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。