如何为适配器中的项目设置侦听器?
how to set listener for items in adapter?
适配器上有一个 lyser,可以在方法点击时改变一些东西。
我不知道我应该在 onClickListener 的主体上写什么。
和 listener.onClick();需要(View View,StructCard data,int posotion)
提前谢谢你我很抱歉我的英语不好
银行卡适配器:
package ir.acopal.android.test.adapter;
import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
import java.util.Random;
import ir.acopal.android.test.R;
import ir.acopal.android.test.classes.StructCard;
import ir.acopal.android.test.interfaces.setOnclickListenerBankCard;
import ir.acopal.android.test.view.CustomBankCard;
public class BankCardAdapter extends RecyclerView.Adapter<BankCardAdapter.ViewHolder> {
private ArrayList<StructCard> accountCards;
private Context context;
setOnclickListenerBankCard listener;
Boolean a = true;
View view;
public BankCardAdapter(ArrayList<StructCard> accountCards, Context context) {
this.accountCards = accountCards;
this.context = context;
}
public void setListener(setOnclickListenerBankCard listener) {
this.listener = listener;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.test, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
StructCard cur = accountCards.get(position);
if (a) {
int firstCard = Color.parseColor("#BDBAAB");
holder.card.setCardBackgroundColor(firstCard);
a = false;
} else {
Random rnd = new Random();
int color = Color.argb(255, rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256));
holder.card.setCardBackgroundColor(color);
}
holder.card.setCardNumber(cur.getAccountCardNumbers());
holder.card.setCustomCardExpireDateText(cur.getAccountExpirationDate());
holder.card.setNewCardText("");
// holder.txtExpirationDate.setText(number.getAccountExpirationDate());
}
@Override
public int getItemCount() {
return accountCards.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
CustomBankCard card;
public ViewHolder(@NonNull final View itemView) {
super(itemView);
findViews(itemView);
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
}
private void findViews(View itemView) {
card = itemView.findViewById(R.id.itemBankCard_cbcCard);
}
}
}
结构卡:
package ir.acopal.android.test.classes;
public class StructCard {
private String accountCardNumbers;
private String accountExpirationDate;
private boolean empty;
public String getAccountCardNumbers() {
return accountCardNumbers;
}
public void setAccountCardNumbers(String accountCardNumbers) {
this.accountCardNumbers = accountCardNumbers;
}
public String getAccountExpirationDate() {
return accountExpirationDate;
}
public void setAccountExpirationDate(String accountExpirationDate) {
this.accountExpirationDate = accountExpirationDate;
}
public boolean isEmpty() {
return empty;
}
public void setEmpty(boolean empty) {
this.empty = empty;
}
}
听众:
package ir.acopal.android.test.interfaces;
import android.view.View;
import ir.acopal.android.test.classes.StructCard;
public interface setOnclickListenerBankCard {
void onClick(View view, StructCard data, int posotion);
}
在您的 onCreateViewHolder 上添加以下代码。您将通过使用适配器 position
获得 structCard 值
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.test, parent, false);
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d(TAG, "position = " + holder.getAdapterPosition());
StructCard structCard = accountCards[holder.getAdapterPosition()]
}
});
return new ViewHolder(view);
}
像这样改变你的onBindViewHolder
。
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
StructCard cur = accountCards.get(position);
if (a) {
int firstCard = Color.parseColor("#BDBAAB");
holder.card.setCardBackgroundColor(firstCard);
a = false;
} else {
Random rnd = new Random();
int color = Color.argb(255, rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256));
holder.card.setCardBackgroundColor(color);
}
holder.card.setCardNumber(cur.getAccountCardNumbers());
holder.card.setCustomCardExpireDateText(cur.getAccountExpirationDate());
holder.card.setNewCardText("");
/*Add these lines for item click. After Click on item
call listener.onClick and pass requires parameters. */
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
listener.onClick(holder.itemView, cur, position)
}
});
}
适配器上有一个 lyser,可以在方法点击时改变一些东西。 我不知道我应该在 onClickListener 的主体上写什么。 和 listener.onClick();需要(View View,StructCard data,int posotion) 提前谢谢你我很抱歉我的英语不好
银行卡适配器:
package ir.acopal.android.test.adapter;
import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
import java.util.Random;
import ir.acopal.android.test.R;
import ir.acopal.android.test.classes.StructCard;
import ir.acopal.android.test.interfaces.setOnclickListenerBankCard;
import ir.acopal.android.test.view.CustomBankCard;
public class BankCardAdapter extends RecyclerView.Adapter<BankCardAdapter.ViewHolder> {
private ArrayList<StructCard> accountCards;
private Context context;
setOnclickListenerBankCard listener;
Boolean a = true;
View view;
public BankCardAdapter(ArrayList<StructCard> accountCards, Context context) {
this.accountCards = accountCards;
this.context = context;
}
public void setListener(setOnclickListenerBankCard listener) {
this.listener = listener;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.test, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
StructCard cur = accountCards.get(position);
if (a) {
int firstCard = Color.parseColor("#BDBAAB");
holder.card.setCardBackgroundColor(firstCard);
a = false;
} else {
Random rnd = new Random();
int color = Color.argb(255, rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256));
holder.card.setCardBackgroundColor(color);
}
holder.card.setCardNumber(cur.getAccountCardNumbers());
holder.card.setCustomCardExpireDateText(cur.getAccountExpirationDate());
holder.card.setNewCardText("");
// holder.txtExpirationDate.setText(number.getAccountExpirationDate());
}
@Override
public int getItemCount() {
return accountCards.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
CustomBankCard card;
public ViewHolder(@NonNull final View itemView) {
super(itemView);
findViews(itemView);
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
}
private void findViews(View itemView) {
card = itemView.findViewById(R.id.itemBankCard_cbcCard);
}
}
}
结构卡:
package ir.acopal.android.test.classes;
public class StructCard {
private String accountCardNumbers;
private String accountExpirationDate;
private boolean empty;
public String getAccountCardNumbers() {
return accountCardNumbers;
}
public void setAccountCardNumbers(String accountCardNumbers) {
this.accountCardNumbers = accountCardNumbers;
}
public String getAccountExpirationDate() {
return accountExpirationDate;
}
public void setAccountExpirationDate(String accountExpirationDate) {
this.accountExpirationDate = accountExpirationDate;
}
public boolean isEmpty() {
return empty;
}
public void setEmpty(boolean empty) {
this.empty = empty;
}
}
听众:
package ir.acopal.android.test.interfaces;
import android.view.View;
import ir.acopal.android.test.classes.StructCard;
public interface setOnclickListenerBankCard {
void onClick(View view, StructCard data, int posotion);
}
在您的 onCreateViewHolder 上添加以下代码。您将通过使用适配器 position
获得 structCard 值@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.test, parent, false);
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d(TAG, "position = " + holder.getAdapterPosition());
StructCard structCard = accountCards[holder.getAdapterPosition()]
}
});
return new ViewHolder(view);
}
像这样改变你的onBindViewHolder
。
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
StructCard cur = accountCards.get(position);
if (a) {
int firstCard = Color.parseColor("#BDBAAB");
holder.card.setCardBackgroundColor(firstCard);
a = false;
} else {
Random rnd = new Random();
int color = Color.argb(255, rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256));
holder.card.setCardBackgroundColor(color);
}
holder.card.setCardNumber(cur.getAccountCardNumbers());
holder.card.setCustomCardExpireDateText(cur.getAccountExpirationDate());
holder.card.setNewCardText("");
/*Add these lines for item click. After Click on item
call listener.onClick and pass requires parameters. */
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
listener.onClick(holder.itemView, cur, position)
}
});
}