如何使用recyclerview管理cardview中的多次点击

时间:2015-08-12 11:39:10

标签: android android-layout android-recyclerview android-cardview

我在Android中使用cardview和recyclerview。

在一张卡内查看我正在实现多个按钮......

所以, 如何为每个按钮实现不同的点击事件

例如: enter image description here

  

我无法使用Intent为不同的活动再调用一个活动   点击选项

     

如果我点击图片,则必须转到一个活动

     

如果我点击描述,则必须转到另一个活动

MainActivity

public class MainActivity extends ActionBarActivity {
RecyclerView mRecyclerView;
RecyclerView.LayoutManager mLayoutManager;
RecyclerView.Adapter mAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view);
    mRecyclerView.setHasFixedSize(true);
    mLayoutManager = new LinearLayoutManager(this);
    mRecyclerView.setLayoutManager(mLayoutManager);
    mAdapter = new CardAdapter();
    mRecyclerView.setAdapter(mAdapter);
}
}

CardAdapter

public class CardAdapter extends RecyclerView.Adapter<CardAdapter.ViewHolder> {

List<Items> mItems;

public CardAdapter() {
    super();
    mItems = new ArrayList<Items>();
    Items nature = new Items();
    nature.setName("The Great Barrier Reef");
    nature.setDes("Lorem ipsum dolor sit amet");
    nature.setThumbnail(R.drawable.great_barrier_reef);
    mItems.add(nature);

    nature = new Items();
    nature.setName("Grand Canyon");
    nature.setDes("Lorem ipsum dolor sit amet");
    nature.setThumbnail(R.drawable.grand_canyon);
    mItems.add(nature);

    nature = new Items();
    nature.setName("Baltoro Glacier");
    nature.setDes("Lorem ipsum dolor sit amet");
    nature.setThumbnail(R.drawable.baltoro_glacier);
    mItems.add(nature);

}

@Override
public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
    View v = LayoutInflater.from(viewGroup.getContext())
            .inflate(R.layout.recycler_view_card_item, viewGroup, false);
    ViewHolder viewHolder = new ViewHolder(v);
    return viewHolder;
}

@Override
public void onBindViewHolder(ViewHolder viewHolder, int i) {
    Items nature = mItems.get(i);
    viewHolder.tvNature.setText(nature.getName());
    viewHolder.tvDesNature.setText(nature.getDes());
    viewHolder.imgThumbnail.setImageResource(nature.getThumbnail());
}

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

class ViewHolder extends RecyclerView.ViewHolder{

    public ImageView imgThumbnail;
    public TextView tvNature;
    public ViewHolder(View itemView) {
        super(itemView);
        imgThumbnail = (ImageView)itemView.findViewById(R.id.img_thumbnail);
        tvNature = (TextView)itemView.findViewById(R.id.tv_nature);

    }
}
}

public class Items {
private String mName;
private String mDes;
private int mThumbnail;

public String getName() {
    return mName;
}

public void setName(String name) {
    this.mName = name;
}

public String getDes() {
    return mDes;
}

public void setDes(String des) {
    this.mDes = des;
}

public int getThumbnail() {
    return mThumbnail;
}

public void setThumbnail(int thumbnail) {
    this.mThumbnail = thumbnail;
}

}

2 个答案:

答案 0 :(得分:1)

您需要定义自定义单击侦听器,该侦听器能够确定单击了哪个视图(或者几个不同的视图,每个视图用于不同视图),将类实现到适配器的转发类(例如包含片段/活动)然后在onClickListener方法在您需要的视图上将此引用设置为revids

答案 1 :(得分:1)

您必须将Context传递给适配器类。然后你可以使用它来调用startActivity()

1.更改你的Adapter构造函数

private Context context
public CardAdapter(Context context) {
  this.context = context;
  ....//your remaining code
}

2.像这样改变你的onBindViewHolder

@Override
public void onBindViewHolder(ViewHolder viewHolder, int i) {
    Items nature = mItems.get(i);
    viewHolder.tvNature.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent = new Intent(context,TargetActivity.class)
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);)
            context.startActivity(intent);
        }
    });
    ...//remaining code
}

3.现在在onCreate()

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ....
    mAdapter = new CardAdapter(this); //change this line
    mRecyclerView.setAdapter(mAdapter);
}