我已经在Medium上阅读了关于Recyclerview
的文章,并指出将听众置于onBindViewHolder
内是不好的做法。
就我而言,我在其中声明了一些听众,只是为了转到下一个Activity
holder.readMoreBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//INTENT BUNDLE HERE
}
});
是否有其他方法可以从持有者那里获取设定数据并通过意图传递?
holder.textViewTitle.setText(doctrines.get(position).doctrineTitle);
holder.textViewDesc.setText(doctrines.get(position).doctrineSummary);
.
.
.
.
编辑:提供修改后的完整代码
public class AdapterDoctrines扩展了RecyclerView.Adapter {
private Context mContext;
public List<ModelGreatDoctrines> doctrines;
public Intent bundleData;
public AdapterDoctrines(Context mContext, List<ModelGreatDoctrines> doctrines) {
this.mContext = mContext;
this.doctrines = doctrines;
}
@NonNull
@Override
public DoctrinesHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View viewDoctrineCard;
LayoutInflater layoutInflaterCard;
layoutInflaterCard = LayoutInflater.from(mContext);
viewDoctrineCard = layoutInflaterCard.inflate(R.layout.card_doctrine,parent,false);
return new DoctrinesHolder(viewDoctrineCard);
}
@Override
public void onBindViewHolder(@NonNull DoctrinesHolder holder, int position) {
//ModelGreatDoctrines greatDoctrines = doctrines.get(position);
}
@Override
public int getItemCount() {
return doctrines.size();
}
class DoctrinesHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
CardView mCardView;
AppCompatTextView textViewTitle;
AppCompatTextView textViewDesc;
AppCompatButton buttonRead;
AppCompatButton buttonDownload;
public DoctrinesHolder(View itemView) {
super(itemView);
mCardView = itemView.findViewById(R.id.card_great_doctrine);
mCardView.setOnClickListener(this);
textViewTitle = itemView.findViewById(R.id.card_doctrine_title);
textViewDesc = itemView.findViewById(R.id.card_doctrine_desc);
buttonRead = itemView.findViewById(R.id.card_doctrine_read);
buttonRead.setOnClickListener(this);
buttonDownload = itemView.findViewById(R.id.card_doctrine_download);
buttonDownload.setOnClickListener(this);
}
@Override
public void onClick(View view) {
int id = view.getId();
switch (id){
case R.id.card_great_doctrine:
break;
case R.id.card_doctrine_read:
//INTENT BUNDLE EXTRA - NEXT ACTIVITY
break;
case R.id.card_doctrine_download:
//PERFORM DOWNLOAD USING DOWNLOAD MANAGER
break;
}
}
//BUNDLE INTENT
public void intentBundle(){
bundleData = new Intent(mContext,ViewerDoctrineActivity.class);
// GET data from Holder
bundleData.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TOP);
mContext.startActivity(bundleData);
}
}
答案 0 :(得分:1)
您可以使用此方法从ViewHolder
课程中获取数据包中的数据。
首先,创建自己的onClickListener类(可以在ViewHolder
类中执行此操作):
public class RecyclerViewItemClickListener implements View.OnClickListener{
private ModelGreatDoctrines doctrine;
RecyclerViewItemClickListener(ModelGreatDoctrines doctrine){
this.doctrine = doctrine;
}
@Override
public void onClick(View view) {
int id = view.getId();
switch (id){
case R.id.card_great_doctrine:
break;
case R.id.card_doctrine_read:
intentBundle(doctrine);
break;
case R.id.card_doctrine_download:
//PERFORM DOWNLOAD USING DOWNLOAD MANAGER
break;
}
}
}
其次,更新ViewHolder
类的代码:
class DoctrinesHolder extends RecyclerView.ViewHolder{
CardView mCardView;
AppCompatTextView textViewTitle;
AppCompatTextView textViewDesc;
AppCompatButton buttonRead;
AppCompatButton buttonDownload;
public DoctrinesHolder(View itemView) {
super(itemView);
ModelGreatDoctrines greatDoctrines = doctrines.get(getAdapterPosition());
RecyclerViewItemClickListener itemClickListener = new RecyclerViewItemClickListener(greatDoctrines)
mCardView = itemView.findViewById(R.id.card_great_doctrine);
mCardView.setOnClickListener(itemClickListener);
textViewTitle = itemView.findViewById(R.id.card_doctrine_title);
textViewDesc = itemView.findViewById(R.id.card_doctrine_desc);
buttonRead = itemView.findViewById(R.id.card_doctrine_read);
buttonRead.setOnClickListener(itemClickListener);
buttonDownload = itemView.findViewById(R.id.card_doctrine_download);
buttonDownload.setOnClickListener(itemClickListener);
}
}
第三,将方法intentBundle
更改为接受ModelGreatDoctrines
作为参数:
//BUNDLE INTENT
public void intentBundle(ModelGreatDoctrines doctrine){
bundleData = new Intent(mContext,ViewerDoctrineActivity.class);
// GET data from Holder
bundleData.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TOP);
mContext.startActivity(bundleData);
}
现在你的intentBundle
方法中有你当前的学说,所以你可以用它来获取数据
答案 1 :(得分:0)
首先创建一个界面 -
public interface ItemClickListener {
void onItemClick(int pos);
}
现在,在你的适配器类中声明一个实现interface-
的内部公共类MyViewHolderpublic class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
private ItemClickListener itemClickListener;
public MyViewHolder(View itemView) {
super(itemView);
}
public void setItemClickListener(ItemClickListener itemClickListener) {
this.itemClickListener = itemClickListener;
itemView.setOnClickListener(this);
}
@Override
public void onClick(View view) {
this.itemClickListener.onItemClick(this.getLayoutPosition());
}
}
然后,在你的onBindViewHolder中,你可以像下面一样实现setItemClickListener方法并传递数据 -
holder.setItemClickListener(new ItemClickListener() {
@Override
public void onItemClick(int pos) {
Intent intent = new Intent(context, NewActivity.class);
intent.putExtra("data_name", "your data");
context.startActivity(intent);
}
});