【发布时间】:2023-03-13 09:05:01
【问题描述】:
如何使用 DiffUtil 回调处理 RecyclerView 的 onClick?以及如何更改 recyclerview 中所选项目的背景颜色?我在一个活动中有两个 RecyclerViews。当用户点击 RecyclerView A 中的项目时,RecyclerView B 中会发生一些事情。
这是课
import androidx.room.ColumnInfo
data class SkladTuple(
@ColumnInfo(name = "sklad") val sklad: Int?,
@ColumnInfo(name = "reg") val reg: Int?
)
这是适配器:
class SkladAdapter: ListAdapter<SkladTuple, SkladAdapter.PolozkaViewHolder>(DiffCallback())
{
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): PolozkaViewHolder {
val binding = SkladyItemBinding.inflate(LayoutInflater.from(parent.context), parent, false)
return PolozkaViewHolder(binding)
}
override fun onBindViewHolder(holder: PolozkaViewHolder, position: Int) {
val currentItem = getItem(position)
holder.bind(currentItem)
}
class PolozkaViewHolder(private val binding: SkladyItemBinding): RecyclerView.ViewHolder(binding.root){
fun bind(polozkaSklad: SkladTuple){
binding.apply {
tvSklad.text = polozkaSklad.sklad.toString()
tvRegal.text = polozkaSklad.reg.toString()
}
}
}
class DiffCallback: DiffUtil.ItemCallback<SkladTuple>(){
override fun areItemsTheSame(oldItem: SkladTuple, newItem: SkladTuple) =
oldItem.sklad == newItem.sklad
override fun areContentsTheSame(oldItem: SkladTuple, newItem: SkladTuple) =
oldItem == newItem
}
}
这是活动
@AndroidEntryPoint
class DokladActivity : AppCompatActivity() {
private val skladViewModel: SkladViewModel by viewModels()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val binding = ActivityDokladBinding.inflate(layoutInflater)
setContentView(binding.root)
binding.btVybratDoklad.setOnClickListener{
openActivity(binding.root)
}
val skladAdapter = SkladAdapter()
val dokladAdapter = DokladAdapter()
binding.apply {
recyclerViewSklady.apply {
adapter = skladAdapter
layoutManager = LinearLayoutManager(this@DokladActivity)
}
skladViewModel.skladyPolozky.observe(this@DokladActivity) {
skladAdapter.submitList(it)
Log.d("Doklad", skladAdapter.currentList.toString())
}
recyclerViewDoklady.apply {
adapter = dokladAdapter
layoutManager = LinearLayoutManager(this@DokladActivity)
}
skladViewModel.dokladyPolozky.observe(this@DokladActivity){
dokladAdapter.submitList(it)
Log.d("Doklad", dokladAdapter.currentList.toString())
}
}
}
fun openActivity(view: View){
val intent = Intent(this,PolozkaActivity::class.java )
startActivity(intent)
}
}
这是视图模型
@HiltViewModel
class SkladViewModel @Inject constructor(
repository: SybaseRepository
): ViewModel(){
val skladyPolozky = repository.getAllSkladFromPolozka().asLiveData()
val dokladyPolozky = repository.getAllHlavickyToDoklad().asLiveData()
}
这是布局 - 我有两个回收站视图。当用户点击 recyclerview A 中的项目时,recyclerview B 中会发生一些事情
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.02" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.7" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline10"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.25" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline11"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.75" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline12"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.9" />
<TableLayout
android:stretchColumns="1,2"
android:layout_margin="8dp"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toTopOf="@+id/guideline10"
app:layout_constraintEnd_toStartOf="@+id/guideline9"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/guideline8">
<TableRow>
<TextView
android:text="SKLAD"
android:textSize="16dp"
android:textStyle="bold"
android:padding="10dp"
android:layout_gravity="center"
android:layout_column="1"/>
<TextView
android:text="REGAL"
android:textSize="16dp"
android:textStyle="bold"
android:padding="10dp"
android:layout_gravity="center"
android:layout_column="1"/>
</TableRow>
<androidx.recyclerview.widget.RecyclerView
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/recycler_view_sklady"
android:scrollbars="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:listitem="@layout/sklady_item"
/>
</TableLayout>
<TableLayout
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="16dp"
android:layout_marginBottom="8dp"
android:stretchColumns="1,2,3,4,5"
app:layout_constraintBottom_toTopOf="@+id/guideline11"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/guideline10"
>
<TableRow>
<TextView
android:layout_column="1"
android:layout_gravity="center"
android:padding="10dp"
android:text="U"
android:textSize="16dp"
android:textStyle="bold" />
<TextView
android:layout_column="1"
android:layout_gravity="center"
android:padding="10dp"
android:text="DOKL"
android:textSize="16dp"
android:textStyle="bold" />
<TextView
android:layout_column="1"
android:layout_gravity="center"
android:padding="10dp"
android:text="ODB"
android:textSize="16dp"
android:textStyle="bold" />
<TextView
android:layout_column="1"
android:layout_gravity="center"
android:padding="10dp"
android:text="ORG"
android:textSize="16dp"
android:textStyle="bold" />
<TextView
android:layout_column="1"
android:layout_gravity="center"
android:padding="10dp"
android:text="DATUM"
android:textSize="16dp"
android:textStyle="bold" />
</TableRow>
<androidx.recyclerview.widget.RecyclerView
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/recycler_view_doklady"
android:scrollbars="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:listitem="@layout/doklady_item"/>
</TableLayout>
<Button
android:id="@+id/bt_back"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="24dp"
android:text="Zpět"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/guideline12" />
<Button
android:id="@+id/bt_vybrat_doklad"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="24dp"
android:text="Vybrat doklad"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="@+id/guideline12" />
<TextView
android:id="@+id/textView2"
android:layout_width="209dp"
android:layout_height="50dp"
android:layout_marginStart="24dp"
android:layout_marginTop="16dp"
android:text="TextView"
app:layout_constraintBottom_toTopOf="@+id/guideline12"
app:layout_constraintEnd_toStartOf="@+id/guideline9"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/guideline11" />
</androidx.constraintlayout.widget.ConstraintLayout>
【问题讨论】:
标签: android kotlin android-recyclerview android-adapter