【问题标题】:How to handle ImageView click inside a row in RecyclerView?如何处理 RecyclerView 中一行内的 ImageView 点击?
【发布时间】:2017-07-30 03:16:21
【问题描述】:

当我单击 ImageView 时,它会转到带有位置的 GridView 活动。当我单击卡片视图内的其他区域时,它会转到带有位置的 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));
        }
    }
}

【问题讨论】:

  • 尝试使用ButterKnife。它将减少样板代码
  • 你有什么错误吗?
  • @Champandorid 没有错误,我想使用更少的编码

标签: android gridview android-recyclerview cardview


【解决方案1】:

你可以在一行中完成!

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

虽然您需要以其他方式检索该值,而不是额外捆绑。

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

【讨论】:

    猜你喜欢
    • 2020-03-18
    • 1970-01-01
    • 2023-03-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多