`public class CardAdapter扩展了RecyclerView.Adapter {
String game_name[];
private static final int TYPE_HEADER = 0;
private static final int TYPE_ITEM = 1;
//private RecyclerViewClickListener itemListener;
card_header header;
public CardAdapter(card_header header, String[] game_name) {
this.game_name = game_name;
this.header = header;
}
@Override
public int getItemCount() {
return game_name.length + 1;
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
if (viewType == TYPE_HEADER) {
View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_view_header, parent, false);
return new ViewHolderHeader(itemView);
} else if (viewType == TYPE_ITEM) {
View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_view, parent, false);
return new ViewHolderItem(itemView);
}
throw new RuntimeException("there is no type that matches the type " + viewType + " + make sure your using types correctly");
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
if (holder instanceof ViewHolderHeader) {
ViewHolderHeader VHheader = (ViewHolderHeader) holder;
VHheader.tv_heading_slot_time.setText(getResources().getString(R.string.slot_time));
} else if (holder instanceof ViewHolderItem) {
ViewHolderItem VHitem = (ViewHolderItem) holder;
VHitem.tv_time_slot.setText(game_name[position - 1]);
}
}
@Override
public int getItemViewType(int position) {
if (isPositionHeader(position))
return TYPE_HEADER;
return TYPE_ITEM;
}
private boolean isPositionHeader(int position) {
return position == 0;
}
@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
}
public class ViewHolderHeader extends RecyclerView.ViewHolder {
TextView tv_heading_game_name, tv_heading_1, tv_heading_2, tv_heading_3,
tv_heading_4, tv_heading_5, tv_heading_6;
public ViewHolderHeader(View itemView) {
super(itemView);
tv_heading_slot_time = (TextView) itemView.findViewById(R.id.tv_heading_slot_time);
tv_heading_1 = (TextView) itemView.findViewById(R.id.tv_heading_1);
tv_heading_2 = (TextView) itemView.findViewById(R.id.tv_heading_2);
tv_heading_3 = (TextView) itemView.findViewById(R.id.tv_heading_3);
tv_heading_4 = (TextView) itemView.findViewById(R.id.tv_heading_4);
tv_heading_5 = (TextView) itemView.findViewById(R.id.tv_heading_5);
tv_heading_6 = (TextView) itemView.findViewById(R.id.tv_heading_6);
switch (numberOfCheckBoxes){
case 1:
tv_heading_2.setVisibility(View.GONE);
tv_heading_3.setVisibility(View.GONE);
tv_heading_4.setVisibility(View.GONE);
tv_heading_5.setVisibility(View.GONE);
tv_heading_6.setVisibility(View.GONE);
break;
case 2:
tv_heading_2.setVisibility(View.VISIBLE);
tv_heading_3.setVisibility(View.GONE);
tv_heading_4.setVisibility(View.GONE);
tv_heading_5.setVisibility(View.GONE);
tv_heading_6.setVisibility(View.GONE);
break;
case 3:
tv_heading_2.setVisibility(View.VISIBLE);
tv_heading_3.setVisibility(View.VISIBLE);
tv_heading_4.setVisibility(View.GONE);
tv_heading_5.setVisibility(View.GONE);
tv_heading_6.setVisibility(View.GONE);
break;
case 4:
tv_heading_2.setVisibility(View.VISIBLE);
tv_heading_3.setVisibility(View.VISIBLE);
tv_heading_4.setVisibility(View.VISIBLE);
tv_heading_5.setVisibility(View.GONE);
tv_heading_6.setVisibility(View.GONE);
break;
case 5:
tv_heading_2.setVisibility(View.VISIBLE);
tv_heading_3.setVisibility(View.VISIBLE);
tv_heading_4.setVisibility(View.VISIBLE);
tv_heading_5.setVisibility(View.VISIBLE);
tv_heading_6.setVisibility(View.GONE);
break;
case 6:
tv_heading_2.setVisibility(View.VISIBLE);
tv_heading_3.setVisibility(View.VISIBLE);
tv_heading_4.setVisibility(View.VISIBLE);
tv_heading_5.setVisibility(View.VISIBLE);
tv_heading_6.setVisibility(View.VISIBLE);
break;
}
}
}
public class ViewHolderItem extends RecyclerView.ViewHolder implements View.OnClickListener {
TableLayout tl;
TextView tv_time_slot;
CheckBox cb_1, cb_2, cb_3, cb_4, cb_5, cb_6;
public ViewHolderItem(View itemView) {
super(itemView);
cb_1 = (CheckBox) itemView.findViewById(R.id.cb_1);
cb_2 = (CheckBox) itemView.findViewById(R.id.cb_2);
cb_3 = (CheckBox) itemView.findViewById(R.id.cb_3);
cb_4 = (CheckBox) itemView.findViewById(R.id.cb_4);
cb_5 = (CheckBox) itemView.findViewById(R.id.cb_5);
cb_6 = (CheckBox) itemView.findViewById(R.id.cb_6);
tv_time_slot = (TextView) itemView.findViewById(R.id.tv_time_slot);
tl = (TableLayout) itemView.findViewById(R.id.tl);
switch (numberOfCheckBoxes){
case 1:
tl.setColumnCollapsed(1,false);
tl.setColumnCollapsed(2,true);
tl.setColumnCollapsed(3,true);
tl.setColumnCollapsed(4,true);
tl.setColumnCollapsed(5,true);
tl.setColumnCollapsed(6,true);
break;
case 2:
tl.setColumnCollapsed(1,false);
tl.setColumnCollapsed(2,false);
tl.setColumnCollapsed(3,true);
tl.setColumnCollapsed(4,true);
tl.setColumnCollapsed(5,true);
tl.setColumnCollapsed(6,true);
break;
case 3:
tl.setColumnCollapsed(1,false);
tl.setColumnCollapsed(2,false);
tl.setColumnCollapsed(3,false);
tl.setColumnCollapsed(4,true);
tl.setColumnCollapsed(5,true);
tl.setColumnCollapsed(6,true);
break;
case 4:
tl.setColumnCollapsed(1,false);
tl.setColumnCollapsed(2,false);
tl.setColumnCollapsed(3,false);
tl.setColumnCollapsed(4,false);
tl.setColumnCollapsed(5,true);
tl.setColumnCollapsed(6,true);
break;
case 5:
tl.setColumnCollapsed(1,false);
tl.setColumnCollapsed(2,false);
tl.setColumnCollapsed(3,false);
tl.setColumnCollapsed(4,false);
tl.setColumnCollapsed(5,false);
tl.setColumnCollapsed(6,true);
break;
case 6:
tl.setColumnCollapsed(1,false);
tl.setColumnCollapsed(2,false);
tl.setColumnCollapsed(3,false);
tl.setColumnCollapsed(4,false);
tl.setColumnCollapsed(5,false);
tl.setColumnCollapsed(6,false);
break;
}
cb_1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
switch (getLayoutPosition()) {
case 1:
if (cb_1.isChecked()) {
}
else {
}
break;
case 2:
if (cb_1.isChecked()) {
}
else {
}
break;
case 3:
if (cb_1.isChecked()) {
}
else {
}
break;
case 4:
if (cb_1.isChecked()) {
}
else {
}
break;
case 5:
if (cb_1.isChecked()) {
}
else {
}
break;
case 6:
if (cb_1.isChecked()) {
}
else {
}
break;
}
}
});
cb_2.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "cb2 " + getLayoutPosition(),
Toast.LENGTH_SHORT).show();
}
});
cb_3.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "cb3 " + getLayoutPosition(),
Toast.LENGTH_SHORT).show();
}
});
cb_4.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "cb4 " + getLayoutPosition(),
Toast.LENGTH_SHORT).show();
}
});
cb_5.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "cb5 " + getLayoutPosition(),
Toast.LENGTH_SHORT).show();
}
});
cb_6.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "cb6 " + getLayoutPosition(),
Toast.LENGTH_SHORT).show();
}
});
}
}
}
`我有一个recyclerview,其中包含从列表中提取的文本项和每个项中的几个复选框。当我按完成按钮时,我想要检查复选框的状态。我想识别每个复选框。有人可以帮忙吗?
列中的复选框具有ID CB1,CB2等等。
答案 0 :(得分:1)
Malav Shah分享的上述链接的摘要,有一个标志代表模型类中的每个CheckBox
。当View
被回收时,这些标志不会被重置。在您设置onBindViewHolder
属性的View
方法中,从相应的标记中设置每个CheckBox
的值。希望这会有所帮助。
答案 1 :(得分:1)
在recyclerview上处理选择状态的正确“官方认可”方法是使用androidx.recyclerview.selection
库。
有关如何使用该库的分步指南:https://proandroiddev.com/a-guide-to-recyclerview-selection-3ed9f2381504