简体   繁体   English

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

[英]How to handle ImageView click inside a row in RecyclerView?

When I click ImageView, it go to GridView activity with position. 当我单击ImageView时,它将转到带有位置的GridView活动。 When I click others area inside a cardview, it go to ItemDetailActivtiy with position. 当我单击卡片视图内的其他区域时,它将转到带位置的ItemDetailActivtiy。 How to do one line or less than coding when click. 单击时如何做一行或少于一行编码。

    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));
        }
    }
}

you can do it in one line! 您可以一行完成!

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

Though you need to retrieve the value in other way than bundle extra. 尽管您需要通过捆绑其他方式来检索值。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM