您好我有一个RecycleView
和一个CheckBox
的适配器,允许用户在Recycle视图中选择多个项目(选中的项目将存储在ArrayList中)现在我想添加另一个函数允许用户仅在“回收”视图中选择1项(仅选中1个复选框)(用户可以自由更改选择)。如何实现这一点。任何帮助都非常感谢
这是我的apdater
(对于选择多项)(我想修改或添加另一个adpater以允许用户只选择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);
}
}
这是我的数据模型(Event)类
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];
}
};
}
答案 0 :(得分:1)
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);
}
}
答案 1 :(得分:0)
试试这个
FragmentPagerAdapter