简体   繁体   English

使用 ItemTouchHelper 从 RecyclerView 中删除项目时引发 UnsupportedOperationException

[英]UnsupportedOperationException is thrown when removing item from RecyclerView using ItemTouchHelper

When item is removed from studentList throws:当从 studentList 中删除项目时,抛出:

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

I implemented ItemTouchHelper.Callback as will but leads to same exception.我按照意愿实现ItemTouchHelper.Callback但导致相同的异常。 I can swap studentList items using onMove( ) without any problem why can't delete item using onSwiped() ?我可以毫无问题地使用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());
            }
        };
    }

The fragment initializes RecyclerView:片段初始化 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();
}

Student class:学生 class:

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);
    }
}

I solved the problem.我解决了这个问题。 The Arrays.asList() return a fixed-size list. Arrays.asList() 返回一个固定大小的列表。 This list allow you to do operations like get and set, but the add and remove operations will throw UnsupportedOperationException.此列表允许您执行 get 和 set 等操作,但添加和删除操作会抛出 UnsupportedOperationException。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM