【发布时间】: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