我有一些CardView项目。我点击它时想要打开不同的活动。 这是我的CardView适配器类的一部分
@Override
public int getItemCount() {
return productGroups.size();
}
public class ViewHolder extends RecyclerView.ViewHolder{
private TextView txtGroupName;
public ViewHolder(final View view) {
super(view);
txtGroupName = (TextView)view.findViewById(R.id.txtNameOfGroup);
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
switch (getLayoutPosition()){
case 0: Intent intent = new Intent(view.getContext(),MyProductsActivity.class);
view.getContext().startActivity(intent);
case 1: Intent intent1 = new Intent(view.getContext(), VegetablesActivity.class);
view.getContext().startActivity(intent1);
}
}
});
}
}
我点击第一项(想要打开MyProductsActivity.class
),但打开VegetablesActivity.class
,只有当我点击手机上的后退按钮时才会打开MyProductsActivity.class
答案 0 :(得分:2)
你忘了休息一下:
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
switch (getLayoutPosition()){
case 0: Intent intent = new Intent(view.getContext(),MyProductsActivity.class);
view.getContext().startActivity(intent);
break; //Here
case 1: Intent intent1 = new Intent(view.getContext(), VegetablesActivity.class);
view.getContext().startActivity(intent1);
break; //Here
}
}
});