当使用ItemTouchHelper从RecyclerView中移除项目时抛出UnsupportedOperationE



当item从studentList中删除时抛出:

**java.lang.UnsupportedOperationException
at java.util.AbstractList.remove(AbstractList.java:161)
at com.sayedy.naweed.test.newtest.TestCase$2.onSwiped**

我实现了ItemTouchHelper.Callback,但导致相同的异常。我可以用onMove(交换studentList,没有任何问题,为什么不能用onSwiped()删除项目?

RecyclerView:

public class TestCase extends RecyclerView.Adapter<TestCase.PassedViewHolder> {
private Context context;
private List<Student> studentList;
private ItemTouchHelper itemTouchHelper;

public TestCase(Context context, List<Student> studentList) {
this.context = context;
this.studentList = studentList;
}

public void setItemTouchHelper(ItemTouchHelper itemTouchHelper) {
this.itemTouchHelper = itemTouchHelper;
}

@NonNull
@Override
public PassedViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
StudentPassedRowBinding rowBinding = DataBindingUtil.inflate(
LayoutInflater.from(context),
R.layout.student_passed_row,
parent, false);
return new PassedViewHolder(rowBinding.getRoot());
}

@Override
public void onBindViewHolder(@NonNull PassedViewHolder holder, int position) {
holder.passedRowBinding.setStudent(studentList.get(position));
holder.passedRowBinding.getRoot().setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
itemTouchHelper.startDrag(holder);
return false;
}
});
}

@Override
public int getItemCount() {
return studentList.size();
}


class PassedViewHolder extends RecyclerView.ViewHolder {
public StudentPassedRowBinding passedRowBinding;

public PassedViewHolder(@NonNull View itemView) {
super(itemView);

passedRowBinding = DataBindingUtil.bind(itemView);
}
}


public ItemTouchHelper.SimpleCallback simpleCallback = new ItemTouchHelper.SimpleCallback(ItemTouchHelper.DOWN | ItemTouchHelper.UP, ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT) {
@Override
public boolean onMove(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, @NonNull RecyclerView.ViewHolder target) {
int currentPosition = viewHolder.getAdapterPosition();
int targetPosition = target.getAdapterPosition();
Collections.swap(studentList, currentPosition,targetPosition);

notifyItemMoved(currentPosition, targetPosition);
return true;
}

@Override
public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int direction) {
studentList.remove(viewHolder.getAdapterPosition());
notifyItemRemoved(viewHolder.getAdapterPosition());
}
};
}

片段初始化RecyclerView:

public View onCreateView(@NonNull LayoutInflater inflater, 
@Nullable ViewGroup container, 
@Nullable Bundle savedInstanceState) {
studentBinding = FragmentStudentBinding.inflate(inflater);
// Data
List<Student> students = Arrays.asList(
new Student("Ahmad", 1, R.drawable.profile, "PASS"),
new Student("Mahmood", 2, R.drawable.profile, "FAIL"),
new Student("Zakir", 3, R.drawable.profile, "PASS"),
new Student("Rezaq", 8, R.drawable.profile, "FAIL"),
new Student("Zeya", 9, R.drawable.profile, "FAIL"),
new Student("Rasool", 10, R.drawable.profile, "FAIL"),
new Student("Parwiz", 11, R.drawable.profile, "FAIL"));
TestCase testCase = new TestCase(getContext(), students);
studentBinding.studentRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
studentBinding.studentRecyclerView.setAdapter(testCase);
ItemTouchHelper itemTouchHelper = new ItemTouchHelper(testCase.simpleCallback);
testCase.setItemTouchHelper(itemTouchHelper);
itemTouchHelper.attachToRecyclerView(studentBinding.studentRecyclerView);
return studentBinding.getRoot();
}

学生类:

public class Student implements Parcelable {
private String name;
private int id;
private int studentProfile;
private String result;
public Student(String name, int id, int studentProfile, String result) {
this.name = name;
this.id = id;
this.studentProfile = studentProfile;
this.result = result;
}
protected Student(Parcel in) {
name = in.readString();
id = in.readInt();
studentProfile = in.readInt();
result = in.readString();
}
public static final Creator<Student> CREATOR = new Creator<Student>() {
@Override
public Student createFromParcel(Parcel in) {
return new Student(in);
}
@Override
public Student[] newArray(int size) {
return new Student[size];
}
};
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getStudentProfile() {
return studentProfile;
}
public void setStudentProfile(int studentProfile) {
this.studentProfile = studentProfile;
}
public String getResult() {
return result;
}
public void setResult(String result) {
this.result = result;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(name);
dest.writeInt(id);
dest.writeInt(studentProfile);
dest.writeString(result);
}
}

我解决了这个问题。数组。aslist()返回一个固定大小的列表。该列表允许您执行get和set之类的操作,但是添加和删除操作将抛出UnsupportedOperationException。

最新更新