holder.itemView.rowLayout.setOnClickListener
holder.itemView.rowLayout.setOnClickListener
我已经为 recyclerview 创建了以下适配器 class 但是在布局部分构建时出现编译错误 "holder.itemView.rowLayout.setOnClickListener" 未解决的参考:rowLayout ,我也将布局更改为 LinearLayout 但仍然存在编译错误
下面分别是Adapterclass和customRow.XML,
package com.example.roomapp.fragments.list
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.navigation.findNavController
import androidx.recyclerview.widget.RecyclerView
import com.example.roomapp.R
import com.example.roomapp.model.User
import kotlinx.android.synthetic.main.custom_row.view.*
class ListAdapter: RecyclerView.Adapter<ListAdapter.MyViewHolder>() {
private var userList = emptyList<User>()
class MyViewHolder(itemView: View): RecyclerView.ViewHolder(itemView) {}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
return MyViewHolder(LayoutInflater.from(parent.context).inflate(R.layout.custom_row, parent, false))
}
override fun getItemCount(): Int {
return userList.size
}
override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
val currentItem = userList[position]
holder.itemView.id_txt.text = currentItem.id.toString()
holder.itemView.txfirstName.text = currentItem.firstName
holder.itemView.txlastName.text = currentItem.lastName
holder.itemView.txContactNo.text = currentItem.contactNumber.toString()
holder.itemView.txEmailAddress.text = currentItem.email.toString()
holder.itemView.rowLayout.setOnClickListener {
val action = ListFragmentDirections.actionListFragmentToUpdateFragment(currentItem)
holder.itemView.findNavController().navigate(action)
}
}
fun setData(user: List<User>){
this.userList = user
notifyDataSetChanged()
}
}
customRow.XML
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:elevation="5dp"
android:padding="10dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:orientation="vertical"
android:padding="10dp">
<TextView
android:id="@+id/id_txt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1"
android:textSize="40sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="@+id/imProd"
android:layout_width="50dp"
android:layout_height="50dp"
android:src="@mipmap/ic_launcher" />
<TextView
android:id="@+id/txfirstName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingVertical="5dp"
android:text="Samsung Note 9"
android:textColor="@color/purple_700"
android:textSize="16dp"
android:textStyle="bold" />
<TextView
android:id="@+id/txlastName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingVertical="5dp"
android:text="Rs. 98,000"
android:textColor="@color/purple_700"
android:textSize="14dp" />
<TextView
android:id="@+id/txContactNo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingVertical="5dp"
android:text="446661122"
android:textColor="@color/purple_700"
android:textSize="13dp" />
<TextView
android:id="@+id/txEmailAddress"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingVertical="5dp"
android:text="test5@gmail.com"
android:textColor="@color/purple_700"
android:textSize="13dp" />
</LinearLayout>
</androidx.cardview.widget.CardView>
xml 中没有 rowLayout。
但是如果你想点击整行,你可以改变
holder.itemView.rowLayout.setOnClickListener {
val action = ListFragmentDirections.actionListFragmentToUpdateFragment(currentItem)
holder.itemView.findNavController().navigate(action)
}
至
holder.itemView.setOnClickListener {
val action = ListFragmentDirections.actionListFragmentToUpdateFragment(currentItem)
holder.itemView.findNavController().navigate(action)
}
我已经为 recyclerview 创建了以下适配器 class 但是在布局部分构建时出现编译错误 "holder.itemView.rowLayout.setOnClickListener" 未解决的参考:rowLayout ,我也将布局更改为 LinearLayout 但仍然存在编译错误 下面分别是Adapterclass和customRow.XML,
package com.example.roomapp.fragments.list
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.navigation.findNavController
import androidx.recyclerview.widget.RecyclerView
import com.example.roomapp.R
import com.example.roomapp.model.User
import kotlinx.android.synthetic.main.custom_row.view.*
class ListAdapter: RecyclerView.Adapter<ListAdapter.MyViewHolder>() {
private var userList = emptyList<User>()
class MyViewHolder(itemView: View): RecyclerView.ViewHolder(itemView) {}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
return MyViewHolder(LayoutInflater.from(parent.context).inflate(R.layout.custom_row, parent, false))
}
override fun getItemCount(): Int {
return userList.size
}
override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
val currentItem = userList[position]
holder.itemView.id_txt.text = currentItem.id.toString()
holder.itemView.txfirstName.text = currentItem.firstName
holder.itemView.txlastName.text = currentItem.lastName
holder.itemView.txContactNo.text = currentItem.contactNumber.toString()
holder.itemView.txEmailAddress.text = currentItem.email.toString()
holder.itemView.rowLayout.setOnClickListener {
val action = ListFragmentDirections.actionListFragmentToUpdateFragment(currentItem)
holder.itemView.findNavController().navigate(action)
}
}
fun setData(user: List<User>){
this.userList = user
notifyDataSetChanged()
}
}
customRow.XML
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:elevation="5dp"
android:padding="10dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:orientation="vertical"
android:padding="10dp">
<TextView
android:id="@+id/id_txt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1"
android:textSize="40sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="@+id/imProd"
android:layout_width="50dp"
android:layout_height="50dp"
android:src="@mipmap/ic_launcher" />
<TextView
android:id="@+id/txfirstName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingVertical="5dp"
android:text="Samsung Note 9"
android:textColor="@color/purple_700"
android:textSize="16dp"
android:textStyle="bold" />
<TextView
android:id="@+id/txlastName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingVertical="5dp"
android:text="Rs. 98,000"
android:textColor="@color/purple_700"
android:textSize="14dp" />
<TextView
android:id="@+id/txContactNo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingVertical="5dp"
android:text="446661122"
android:textColor="@color/purple_700"
android:textSize="13dp" />
<TextView
android:id="@+id/txEmailAddress"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingVertical="5dp"
android:text="test5@gmail.com"
android:textColor="@color/purple_700"
android:textSize="13dp" />
</LinearLayout>
</androidx.cardview.widget.CardView>
xml 中没有 rowLayout。
但是如果你想点击整行,你可以改变
holder.itemView.rowLayout.setOnClickListener {
val action = ListFragmentDirections.actionListFragmentToUpdateFragment(currentItem)
holder.itemView.findNavController().navigate(action)
}
至
holder.itemView.setOnClickListener {
val action = ListFragmentDirections.actionListFragmentToUpdateFragment(currentItem)
holder.itemView.findNavController().navigate(action)
}