所以我知道如何從一個正常的活動開始一個活動到另一個,但我不知道如何做到這一點。我對android幾乎一無所知,我的教授也沒有真正解釋任何事情,所以如果這個問題難以理解或其他什么,我深表歉意。請要求任何澄清。
package com.example.myrecyclerview
import android.content.Intent
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.example.myrecyclerview.databinding.CardLayoutBinding
class RecyclerAdapter : RecyclerView.Adapter<RecyclerAdapter.ViewHolder>() {
private var titles = arrayOf("Little Gooby Jr", "Nathaniel Scuttlebug", "Indigo Purple", "Eathan Fishtic",
"Malli mailman", "Cameron Silva", "Frozen Candy", "Gamer Jones", "Nuzz Lightyear", "Nueben Sandwich")
private var image = arrayOf(R.drawable.goob, R.drawable.psy, R.drawable.indigo, R.drawable.teo,
R.drawable.malli, R.drawable.cam, R.drawable.frozen, R.drawable.gamer, R.drawable.nazz, R.drawable.nueb)
inner class ViewHolder(val binding: CardLayoutBinding): RecyclerView.ViewHolder(binding.root){
init {
itemView.setOnClickListener{
val position: Int = bindingAdapterPosition
//
// code to start another activity and pass info like view holder position
//
}
}
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerAdapter.ViewHolder {
val binding = CardLayoutBinding.inflate(LayoutInflater.from(parent.context), parent, false)
return ViewHolder(binding)
}
override fun onBindViewHolder(holder: RecyclerAdapter.ViewHolder, position: Int) {
with(holder){
with(binding){
itemImage.setImageResource(image[position])
itemTitle.text = titles[position]
}
}
}
override fun getItemCount(): Int {
return titles.size
}
}
uj5u.com熱心網友回復:
首先,您需要在RecyclerAdapter中將背景關系作為引數傳遞。而且,您需要做的下一件事是為 CardLayoutBinding 的根視圖提供一個 ID。例如,在這里我認為您的 CardLayoutBinding XML 看起來像這樣。因此,我將 id 提供給根視圖作為“itemViewContainer”
card_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@ id/itemViewContainer"
android:layout_margin="10dp"
>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@ id/itemImage"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@id/itemImage"
android:id="@ id/itemTitle"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
現在,在您的配接器類中,您進行以下更改:
RecyclerAdapter.java
class RecyclerAdapter(val context:Context) : RecyclerView.Adapter<...>(){
.......
.....
inner class ViewHolder(val binding: ItemLayoutBinding): RecyclerView.ViewHolder(binding.root){}
.......
......
override fun onBindViewHolder(holder: RecyclerAdapter.ViewHolder, position: Int) {
with(holder){
with(binding){
itemImage.setImageResource(image[position])
itemTitle.text = titles[position]
//from here you can click on item to start new Activity and pass position.you can also use intent to pass position either.
itemViewContainer.setOnClickListener {
val bundle = Bundle()
bundle.putInt("position", position)
context.startActivity(Intent(context, YourDesiredActivity::class.java).putExtras(bundle))
}
}
}
}
.......
.......
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/435793.html
上一篇:在顫動中路由按鈕時不斷出錯
