我正在尝试处理我在RecycleView中动态添加的cardviews中的按钮的点击事件,我为每次点击按钮获得相同的ID。
Adapter.Java
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
if (viewType==0) {
View itemView1 = LayoutInflater.
from(parent.getContext()).
inflate(R.layout.room_detail_card_view, parent, false);
return new ViewHolder(itemView1);
}
else if (viewType==1){
View itemView = LayoutInflater.
from(parent.getContext()).
inflate(R.layout.room_detail_card_view, parent, false);
return new ViewHolderType3(itemView);}
else {
View itemView = LayoutInflater.
from(parent.getContext()).
inflate(R.layout.module_type_2, parent, false);
return new ViewHolderType2(itemView);}
}
@Override
public void onBindViewHolder(final ViewHolder holder, final int position) {
RoomDetails details =detailsList.get(position);
holder.Name1.setText(details.getName());
holder.layout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String str= String.valueOf(v.getId());
String ids= String.valueOf(R.id.switch_1_5);
if (v.getId()==R.id.switch_1_5) {
if (status[position] == true) {
Toast.makeText(context, "Done:)", Toast.LENGTH_SHORT).show();
holder.buttonId1.setImageResource(R.mipmap.ic_launcher);
} else if (v.getId() == R.id.switch_2_5) {
Toast.makeText(context, "button2", Toast.LENGTH_SHORT).show();
holder.buttonId2.setImageResource(R.mipmap.ic_launcher);
} else if (v.getId() == R.id.switch_3_5)
Toast.makeText(context, "button3", Toast.LENGTH_SHORT).show();
else if (v.getId() == R.id.switch_4_5)
Toast.makeText(context, "button4", Toast.LENGTH_SHORT).show();
else if (v.getId() == R.id.switch_5_5)
Toast.makeText(context, "button5", Toast.LENGTH_SHORT).show();
else
Toast.makeText(context, "Nothing", Toast.LENGTH_SHORT).show();//i always get else condition after button clicks
}}
});
if(position==0)
holder.layout.addView(View.inflate(context,R.layout.module_type_3,null));
//here i am unable to add onClick for dynamically inflated xmls
if (position==1)
holder.layout.addView(View.inflate(context,R.layout.module_type_1,null));
}
@Override
public int getItemViewType(int position) {
return position;
}
@Override
public int getItemCount() {
return detailsList.size();
}
public static class ViewHolder extends RecyclerView.ViewHolder {
private LinearLayout layout;
private final TextView Name1;
private ImageView buttonId1,buttonId2,buttonId3,buttonId4,buttonId5;
public ViewHolder(final View itemView) {
super(itemView);
Name1= (TextView) itemView.findViewById(R.id.room_name);
buttonId1= (ImageView) itemView.findViewById(R.id.switch_1_5);
buttonId2= (ImageView) itemView.findViewById(R.id.switch_1_5);
buttonId3= (ImageView) itemView.findViewById(R.id.switch_3_5);
buttonId4= (ImageView) itemView.findViewById(R.id.switch_4_5);
buttonId5= (ImageView) itemView.findViewById(R.id.switch_5_5);
layout= (LinearLayout) itemView.findViewById(R.id.module_layout_5);
}
public class ViewHolderType5 extends ViewHolder{
public ViewHolderType5(View itemView) {
super(itemView);
}
}
public class ViewHolderType3 extends ViewHolder{
public ViewHolderType3(View itemView) {
super(itemView);
}
}
public class ViewHolderType2 extends ViewHolder{
public ViewHolderType2(View itemView) {
super(itemView);
}
}
}
如何处理这些按钮的onclicks?
答案 0 :(得分:3)
private View.OnClickListener clickListener=new View.OnClickListener() {
@Override
public void onClick(View v) {
RecyclerViewHolder1 vholder = (RecyclerViewHolder1) v.getTag();
int position = vholder.getPosition();
switch (position) {
case 0:
final Intent intent = new Intent(context, Jazba.class);
context.startActivity(intent);
break;
case 1:
final Intent intent1 = new Intent(context, RatriB4Navratri.class);
context.startActivity(intent1);
break;
case 2:
final Intent intent2 = new Intent(context, RatriB4Navratri.class);
context.startActivity(intent2);
break;
case 3:
final Intent intent3 = new Intent(context, AnnualSports.class);
context.startActivity(intent3);
break;
}
}
};
试试..
答案 1 :(得分:-1)
你不能只按一个按钮并在里面设置开关盒! 如下所示:
holder.buttonId1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (status[position] == true) {
Toast.makeText(context, "Done:)", Toast.LENGTH_SHORT).show();
holder.buttonId1.setImageResource(R.mipmap.ic_launcher);
}
}
});
holder.buttonId2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(context, "button2:)", Toast.LENGTH_SHORT).show();
holder.buttonId2.setImageResource(R.mipmap.ic_launcher);
}
});
holder.buttonId3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(context, "button3:)", Toast.LENGTH_SHORT).show();
}
});
holder.buttonId4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(context, "button4:)", Toast.LENGTH_SHORT).show();
}
});
holder.buttonId5.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(context, "button5:)", Toast.LENGTH_SHORT).show();
}
});
//////Add View
if(position==0)
View vTemp1=View.inflate(context,R.layout.module_type_3,null);
vTemp1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(context, "vTemp1:)", Toast.LENGTH_SHORT).show();
}
});
holder.layout.addView(vTemp1);
//here i am unable to add onClick for dynamically inflated xmls
if (position==1)
View vTemp2=View.inflate(context, R.layout.module_type_1,null);
vTemp2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(context, "vTemp2:)", Toast.LENGTH_SHORT).show();
}
});
holder.layout.addView(vTemp2);