[英]How to get Selected CardView position on OnClickListener android?
I have list of contacts and i have show this list on RecyclerView using CardView. 我有联系人列表,并且使用CardView在RecyclerView上显示了此列表。
List is showing perfectly But now I want to make clickable Card View. 列表显示完美,但是现在我要创建可点击的卡片视图。 So that When I click on particular contact i will show details of particular contact.
这样,当我单击特定联系人时,我将显示特定联系人的详细信息。
When I click on particular CardView I Toast a message but can't find position of selected cardview and want to pass details of selected contact to another activity using intent.putExtra Method. 当我单击特定的CardView时,我会举杯祝词,但找不到所选Cardview的位置,并希望使用intent.putExtra方法将所选联系人的详细信息传递给另一个活动。
Code : 代码:
@Override
public ContactViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_view_layout, parent, false);
ContactViewHolder contactViewHolder = new ContactViewHolder(view,ctx,contacts);
return contactViewHolder;
}
@Override
public void onBindViewHolder(ContactViewHolder holder, int position) {
Contact CON = contacts.get(position);
holder.person_name.setText(CON.getName());
holder.person_email.setText(CON.getEmail());
holder.card_view.setOnClickListener(this);
}
@Override
public void onClick(View v) {
Toast.makeText(this.ctx, "Hello", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(this.ctx,task_detail.class);
this.ctx.startActivity(intent);
}
@Override
public int getItemCount() {
return contacts.size();
}
public static class ContactViewHolder extends RecyclerView.ViewHolder {
TextView person_name, person_email;
ArrayList<Contact> contacts = new ArrayList<Contact>();
Context ctx;
CardView card_view;
public ContactViewHolder(View view, Context ctx ,ArrayList<Contact> contacts) {
super(view);
this.contacts=contacts;
this.ctx = ctx;
person_name = (TextView) view.findViewById(R.id.person_name);
person_email = (TextView) view.findViewById(R.id.person_email);
card_view = (CardView) view.findViewById(R.id.card_view);
}
}
Use the below code . 使用以下代码。 Instead of using
holder.card_view.setOnClickListener(this);
而不是使用
holder.card_view.setOnClickListener(this);
inside onBindViewHolder
, use it in ContactViewHolder
like this , card_view.setOnClickListener(this);
在
onBindViewHolder
,像这样在ContactViewHolder
使用它, card_view.setOnClickListener(this);
so that you can get the position using getPosition()
method. 这样您就可以使用
getPosition()
方法获取位置。
public static class ContactViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
TextView person_name, person_email;
ArrayList<Contact> contacts = new ArrayList<Contact>();
Context ctx;
CardView card_view;
public ContactViewHolder(View view, Context ctx ,ArrayList<Contact> contacts) {
super(view);
this.contacts=contacts;
this.ctx = ctx;
person_name = (TextView) view.findViewById(R.id.person_name);
person_email = (TextView) view.findViewById(R.id.person_email);
card_view = (CardView) view.findViewById(R.id.card_view);
card_view.setOnClickListener(this);
}
@Override
public void onClick(View v)
{
Toast.makeText(this.ctx, "position "+getPosition(), Toast.LENGTH_SHORT).show();
Intent intent = new Intent(this.ctx,task_detail.class);
this.ctx.startActivity(intent);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.