gpt4 book ai didi

android - 允许用户在 Recyclerview Android 中仅选择 1 项

转载 作者:行者123 更新时间:2023-11-30 01:41:09 25 4
gpt4 key购买 nike

您好,我有一个 RecycleView 和一个带有 CheckBox 的适配器,允许用户在 Recycle View 中选择多个项目(选中的项目将存储在 ArrayList 中)现在我想要添加另一个功能,允许用户在回收 View 中仅选择 1 个项目(仅选中 1 个复选框)(用户可以自由更改选择)。如何实现这一点。非常感谢任何帮助

这是我的 apdater(用于选择多个项目)(我想修改或添加另一个 adpater 以允许用户只选择 1 个项目,一次只选中 1 个复选框)

public class EventAdapterAssign extends RecyclerView.Adapter<EventAdapterAssign.ViewHolder> {

private Context context;
List <Event> events, eventSelected;

public EventAdapterAssign(Context context, List<Event> events, List<Event> eventSelected) {
this.context = context;
this.events = events;
this.eventSelected = eventSelected;
}


@Override
public EventAdapterAssign.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
Context context = parent.getContext();
LayoutInflater inflater = LayoutInflater.from(context);

// Inflate the custom layout
View eventView1 = inflater.inflate(R.layout.event_list_assign, parent, false);

// Return a new holder instance
ViewHolder viewHolder = new ViewHolder(eventView1);
return viewHolder;
}

@Override
public void onBindViewHolder(EventAdapterAssign.ViewHolder holder, int position) {
//Getting the particular item from the list
final Event event = events.get(position);

//Showing data on the views
holder.EventTitle.setHint(event.getEventTitle());
holder.EventDesc.setText(event.getEventDesc());
holder.EventStartTime.setText(event.getEventStartTime());
holder.EventEndTime.setText(event.getEventEndTime());
holder.EventID.setText(event.getEventID());

//in some case, it will prevent unwanted situations;
holder.selectCheckBox.setOnCheckedChangeListener(null);

//if true, your check box will be selected, else unselected
holder.selectCheckBox.setChecked(event.isSelected());

holder.selectCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

event.setIsSelected(isChecked);
if (isChecked) {
eventSelected.add(event);
} else {
eventSelected.remove(event);
}
}
});

}

// Return the total count of items
@Override
public int getItemCount() {
return events.size();
}

public class ViewHolder extends RecyclerView.ViewHolder {
//View
public EditText EventTitle;
public TextView EventDesc;
public TextView EventStartTime;
public TextView EventEndTime;
public TextView EventID;
public CheckBox selectCheckBox;

//initiating View
public ViewHolder(View itemView) {
super(itemView);

// display Check In event when beacon in range

EventTitle = (EditText)itemView.findViewById(R.id.EventTitle);
EventDesc = (TextView)itemView.findViewById(R.id.EventDesc);
EventStartTime = (TextView)itemView.findViewById(R.id.EventStartTime);
EventEndTime = (TextView)itemView.findViewById(R.id.EventEndTime);
EventID = (TextView)itemView.findViewById(R.id.EventID);

selectCheckBox = (CheckBox)itemView.findViewById(R.id.selectCheckBox);

}
}

这是我的数据模型(事件)类

public class Event implements Parcelable {
private String EventID;
private String EventDesc;
private String EventTitle;
private String EventStartTime;
private String EventEndTime;

private Context context;
private boolean isSelected;

public boolean isSelected() {
return isSelected;
}

public void setIsSelected(boolean isSelected) {
this.isSelected = isSelected;
}

public String getEventID() {
return EventID;
}

public void setEventID(String eventID) {
EventID = eventID;
}

public String getEventDesc() {
return EventDesc;
}

public void setEventDesc(String eventDesc) {
EventDesc = eventDesc;
}

public String getEventTitle() {
return EventTitle;
}

public void setEventTitle(String eventTitle) {
EventTitle = eventTitle;
}

public String getEventStartTime() {
return EventStartTime;
}

public void setEventStartTime(String eventStartTime) {
EventStartTime = eventStartTime;
}

public String getEventEndTime() {
return EventEndTime;
}

public void setEventEndTime(String eventEndTime) {
EventEndTime = eventEndTime;
}

@Override
public int describeContents() {
return 0;
}

@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(this.EventID);
dest.writeString(this.EventDesc);
dest.writeString(this.EventTitle);
dest.writeString(this.EventStartTime);
dest.writeString(this.EventEndTime);
}

public Event() {
}

protected Event(Parcel in) {
this.EventID = in.readString();
this.EventDesc = in.readString();
this.EventTitle = in.readString();
this.EventStartTime = in.readString();
this.EventEndTime = in.readString();
}

public static final Parcelable.Creator<Event> CREATOR = new Parcelable.Creator<Event>() {
public Event createFromParcel(Parcel source) {
return new Event(source);
}

public Event[] newArray(int size) {
return new Event[size];
}
};


}

最佳答案

    public class EventAdapterAssign extends RecyclerView.Adapter<EventAdapterAssign.ViewHolder> {

private Context context;
List <Event> events, eventSelected;
private SparseBooleanArray mSparseBooleanArray;
public int selectedPosition;
public EventAdapterAssign(Context context, List<Event> events, List<Event> eventSelected) {
this.context = context;
this.events = events;
this.eventSelected = eventSelected;
mSparseBooleanArray = new SparseBooleanArray();
}


@Override
public EventAdapterAssign.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
Context context = parent.getContext();
LayoutInflater inflater = LayoutInflater.from(context);

// Inflate the custom layout
View eventView1 = inflater.inflate(R.layout.event_list_assign, parent, false);

// Return a new holder instance
ViewHolder viewHolder = new ViewHolder(eventView1);
return viewHolder;
}

@Override
public void onBindViewHolder(EventAdapterAssign.ViewHolder holder, int position) {
//Getting the particular item from the list
Event event = events.get(position);

//Showing data on the views
holder.EventTitle.setHint(event.getEventTitle());
holder.EventDesc.setText(event.getEventDesc());
holder.EventStartTime.setText(event.getEventStartTime());
holder.EventEndTime.setText(event.getEventEndTime());
holder.EventID.setText(event.getEventID());

//in some case, it will prevent unwanted situations;
holder.selectCheckBox.setOnCheckedChangeListener(null);

//if true, your check box will be selected, else unselected

holder.selectCheckBox.setTag(position);

if(position==selectedPosition)
{
holder.selectCheckBox.setChecked(true);
}
else
{
holder.selectCheckBox.setChecked(false);
}
holder.selectCheckBox.setOnCheckedChangeListener(new CheckListener(holder.selectCheckBox,position));

}

// Return the total count of items
@Override
public int getItemCount() {
return events.size();
}

class CheckListener implements OnCheckedChangeListener {

private CheckBox checkbox;
Event data;
public CheckListener(CheckBox checkbox,int position) {

this.checkbox = checkbox;
this.position=position;

}

@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {

if (isChecked) {
checkbox.setChecked(true);
selectedPosition = position;
EventAdapterAssign.this.notifyDataSetChanged();
} else {
checkbox.setChecked(false);

}
buttonView.setChecked(isChecked);

}


}

public Event getCheckedItems() {


return event.get(selectedPosition);
}

public class ViewHolder extends RecyclerView.ViewHolder {
//View
public EditText EventTitle;
public TextView EventDesc;
public TextView EventStartTime;
public TextView EventEndTime;
public TextView EventID;
public CheckBox selectCheckBox;

//initiating View
public ViewHolder(View itemView) {
super(itemView);

// display Check In event when beacon in range

EventTitle = (EditText)itemView.findViewById(R.id.EventTitle);
EventDesc = (TextView)itemView.findViewById(R.id.EventDesc);
EventStartTime = (TextView)itemView.findViewById(R.id.EventStartTime);
EventEndTime = (TextView)itemView.findViewById(R.id.EventEndTime);
EventID = (TextView)itemView.findViewById(R.id.EventID);

selectCheckBox = (CheckBox)itemView.findViewById(R.id.selectCheckBox);

}
}

关于android - 允许用户在 Recyclerview Android 中仅选择 1 项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34488053/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com