如何从firebase永久重新订购recyclerview?

qacovj5a  于 2021-06-30  发布在  Java
关注(0)|答案(0)|浏览(168)

我在做一个firebase项目。我使用itemtouchhelper从firebase数据库中拖动并重新排序recyclerview,但如果我输入另一个活动并恢复或重新启动应用程序,它会重置为原来的状态。有没有办法使对recyclerview订单的更改永久化?

ItemTouchHelper.SimpleCallback simpleCallback = new ItemTouchHelper.SimpleCallback(ItemTouchHelper.UP | ItemTouchHelper.DOWN | ItemTouchHelper.START | ItemTouchHelper.END, 0) {
@Override
public boolean onMove(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, @NonNull RecyclerView.ViewHolder target) {
    int fromPosition = viewHolder.getAdapterPosition();
    int toPosition = target.getAdapterPosition();

    Collections.swap(List, fromPosition, toPosition);

    adapter.notifyItemMoved(fromPosition, toPosition);
    return false;
}

@Override
public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int direction) {

}};
private void loadList() {
List = new ArrayList<>();
DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference("List");
databaseReference.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(@NonNull DataSnapshot snapshot) {
        List.clear();
        for(DataSnapshot ss: snapshot.getChildren()){
            if(ss.child("moreList").child(ID).exists()){
                ListModel model = ss.getValue(ListModel.class);
                List.add(model);
            }
        }
        adapter = new ListAdapter(getActivity(), List);
        RecyclerView.setAdapter(adapter);
        ItemTouchHelper itemTouchHelper = new ItemTouchHelper(simpleCallback);
        itemTouchHelper.attachToRecyclerView(RecyclerView);

    }

    @Override
    public void onCancelled(@NonNull DatabaseError error) {

    }
});

}

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题