我们有一个RecyLeadPter,它有一个onClickListener,用于包含ImageView的RelativeLayout。当点击视图时,数据会被传输到另一个活动中进行编辑,这非常有效。我们遇到的问题是,在导航到新活动之前单击相应活动中的ImageView时,id将替换要编辑的文本。我们的问题是如何在不刷新id的情况下获取id?我们的膨胀视图XML和RecyclerAdapter代码发布在下面
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp">
<TextView
android:id="@+id/tvName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:textColor="@color/color_Black"
android:textSize="18sp"
android:textStyle="bold" />
</RelativeLayout>
<RelativeLayout
android:id="@+id/click"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_marginLeft="370dp"
android:paddingLeft="6dp"
android:paddingRight="6dp"
android:paddingTop="12dp">
<ImageView
android:id="@+id/ivEdit"
android:layout_width="30dp"
android:layout_height="30dp"
android:src="@drawable/ic_edit" />
</RelativeLayout>
class PersonRecyclerAdapter(contactList: List<Contact>, internal var context: Context) : RecyclerView.Adapter<PersonRecyclerAdapter.ViewHolder>() {
internal var contactList: List<Contact> = ArrayList()
init { this.contactList = contactList }
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val view = LayoutInflater.from(context).inflate(R.layout.new_single_card,parent,false)
return ViewHolder(view)
}
override fun getItemCount(): Int {
return contactList.size
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val items = contactList[position]
holder.item.text = items.name
holder.click.setOnClickListener {
val i = Intent(context, MainActivity::class.java)
holder.item.text = items.id.toString()
i.putExtra("Mode", "E")
i.putExtra("MainActId",items.id)
i.putExtra("ET",items.name)
i.flags = Intent.FLAG_ACTIVITY_NEW_TASK
context.startActivity(i)
}
}