如何在RecyclerView中的行内单击ImageView?

时间:2017-03-09 08:27:01

标签: android gridview android-recyclerview cardview

当我单击ImageView时,它会转到带有位置的GridView活动。当我点击cardview中的其他区域时,它会转到具有位置的ItemDetailActivtiy。单击时如何进行一行或少于编码。

    public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        TextView tv_restaurant_name, tv_restaurant_address, tv_restaurant_phone;
        ImageView imgview;

    public ViewHolder(View itemView) {
        super(itemView);
        tv_restaurant_name = (TextView) itemView.findViewById(R.id.tv_restaurant_name);
        tv_restaurant_address = (TextView) itemView.findViewById(R.id.tv_restaurant_address);
        tv_restaurant_phone = (TextView) itemView.findViewById(R.id.tv_restaurant_phone);
        imgview = (ImageView) itemView.findViewById(R.id.imgview);
        tv_restaurant_name.setOnClickListener(this);
        tv_restaurant_address.setOnClickListener(this);
        tv_restaurant_phone.setOnClickListener(this);
        imgview.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        int position = getAdapterPosition();
        if (v.getId() == imgview.getId()) {
            Intent intent = new Intent(context, GridView.class);
            Bundle bundle = new Bundle();
            bundle.putInt("ID", Integer.valueOf(mRestaurantList.get(position).getRestaurantId()));
            intent.putExtras(bundle);
            context.startActivity(intent);
        } else {
            context.startActivity(new Intent(context, ItemDetailActivtiy.class));
        }
    }
}

1 个答案:

答案 0 :(得分:0)

你可以在一行中完成!

context.startActivity(new Intent(context, GridView.class).putExtra("ID",Integer.valueOf(mRestaurantList.get(position).getRestaurantId())));

虽然您需要以除extra bundle之外的其他方式检索值。

getIntent().getIntExtra("ID",0);