我想在cardview的onclicklistener上实现,每张卡都将我带到不同的活动

时间:2018-12-16 12:20:42

标签: android android-recyclerview interstitial cardview

我想替换烤面包,并且希望单击后的每张卡片视图都将用户带到新的不同活动中。当用户点击卡片视图时,我还想实施非页内广告。

这是我的适配器类

public class MyAdapter extends RecyclerView.Adapter< LessonViewHolder > {

private Context mContext;
private List< LessonData > mLessonList;

MyAdapter(Context mContext, List< LessonData > mLessonList) {
    this.mContext = mContext;
    this.mLessonList = mLessonList;
}

@Override
public LessonViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View mView = LayoutInflater.from(parent.getContext()).inflate(R.layout.recyclerview_item_row,  parent, false);
    return new LessonViewHolder(mView);
}

@Override
public void onBindViewHolder(final LessonViewHolder holder, int position) {
    holder.mImage.setImageResource(mLessonList.get(position).getLessonImage());
    holder.mTitle.setText(mLessonList.get(position).getLessonName());

    holder.mCardView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

         // I want every single card takes the user to a new different activity, and also implement interstitial ads when the user clicks
        }
    });
}




@Override
public int getItemCount() {
    return mLessonList.size();
}
}

class LessonViewHolder extends RecyclerView.ViewHolder {

CardView mCardView;
ImageView mImage;
TextView mTitle;

LessonViewHolder(View itemView) {
    super(itemView);

    mImage = itemView.findViewById(R.id.ivImage);
    mTitle = itemView.findViewById(R.id.tvTitle);
    CardView mCardView = itemView.findViewById(R.id.cardview);
}
}

我还想在用户点击这些插卡时对非页内广告进行广告。

1 个答案:

答案 0 :(得分:1)

您将要在列表中的每个元素上设置一个点击侦听器。

在您的onBindViewHolder内编写如下内容:

 holder.itemView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if(pos == 0){              
               view.getContext().startActivity(new Intent(view.getContext(),Activity1.class));
           }else if(pos == 1){
               view.getContext().startActivity(new Intent(view.getContext(),Activity2.class));
          }
        }
    });
}

快乐编码;)