gpt4 book ai didi

java - 在 Android 中保存 Drag 'n' Drop in RecyclerView 后所做的更改 - 我已阅读所有内容

转载 作者:行者123 更新时间:2023-12-02 02:27:25 25 4
gpt4 key购买 nike

我有一个具有拖放功能的 RecyclerView。 Drop/Drop 在自定义适配器中完成。使用数据库空间,实体将不会按“删除”顺序保存。

我尝试了大约十几种不同的方法/github:

...这样的例子还在继续...

我从许多不同的来源拼凑了代码:

我的自定义适配器:

public class SwipeAdapt extends OmegaRecyclerView.Adapter<SwipeAdapt.ViewHolder> {

public void onItemDismiss(int position) {
accountList.remove(position);
notifyItemRemoved(position);
}
public boolean onItemMove(int fromPosition, int toPosition) {
if (fromPosition < toPosition) {
for (int i = fromPosition; i < toPosition; i++) {
Collections.swap(accountList, i, i + 1);
}
} else {
for (int i = fromPosition; i > toPosition; i--) {
Collections.swap(accountList, i, i - 1);
}
}

notifyItemMoved(fromPosition, toPosition);

return true;
}

public void onItemClear() {
for (int count = 0; count < accountList.size(); count++) {
Account u = accountList.get(count);
u.setId(count);
upAccount(u);
}
notifyDataSetChanged();
}

我的主要 Activity (Java):

    OmegaRecyclerView RVMain;
Account crossOver;

private ItemTouchHelper itemTouchHelper;


RVMain = (OmegaRecyclerView) findViewById(R.id.theRV);
RVMain.setLayoutManager(new LinearLayoutManager(this));
getAccounts();
}

private void getAccounts() {
class GetAccounts extends AsyncTask<Void,Void,List<Account>> {

@Override
protected List<Account> doInBackground(Void... voids) {
List<Account> aList = DataClient.getInstance(getApplicationContext()).getAppDB().theDao().getAll();
return aList;
}

@Override
protected void onPostExecute(List<Account> accounts) {
super.onPostExecute(accounts);
SwipeAdapt adapter = new SwipeAdapt(MainActivity.this, accounts);
RVMain.setAdapter(adapter);
DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(RVMain.getContext(), new LinearLayoutManager(getApplicationContext()).getOrientation());
RVMain.addItemDecoration(dividerItemDecoration);
ItemTouchHelper.Callback callback = new SimpleItemTouchHelperCallback(adapter);
itemTouchHelper = new ItemTouchHelper(callback);
itemTouchHelper.attachToRecyclerView(RVMain);
}
}

GetAccounts ga = new GetAccounts();
ga.execute();
}
}

拖放回调

public class SimpleItemTouchHelperCallback extends ItemTouchHelper.Callback {
private final SwipeAdapt mAdapter;

@Override
public int getMovementFlags(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder) {
int dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN;
//int swipeFlags = ItemTouchHelper.START | ItemTouchHelper.END;
return makeMovementFlags(dragFlags, 0);
}

@Override
public boolean onMove(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, @NonNull RecyclerView.ViewHolder target) {
mAdapter.onItemMove(viewHolder.getAdapterPosition(), target.getAdapterPosition());
return true;
}

@Override
public void onSelectedChanged(RecyclerView.ViewHolder viewHolder, int actionState) {
if (actionState != ItemTouchHelper.ACTION_STATE_IDLE) {
if (viewHolder instanceof ItemTouchHelperViewHolder) {
ItemTouchHelperViewHolder itemViewHolder = (ItemTouchHelperViewHolder) viewHolder;
itemViewHolder.onItemSelected();
}
}

super.onSelectedChanged(viewHolder, actionState);
}
@Override
public void clearView(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
super.clearView(recyclerView, viewHolder);
mAdapter.onItemClear();
}
}

希望这还不算太多。如果有人愿意,我可以将整个内容放在 GitHub 上。我没有收到任何错误。一切看起来都很好。根据所有这些来源,我做得正确。我正在“通知”一切,等等。然而,每次 Activity 发生变化时,RecyclerView 顺序都会回到拖/放之前的状态。任何帮助是极大的赞赏。 TIA!

最佳答案

在 onItemMove 方法的底部,用新位置更新数据库中的所有数据。

关于java - 在 Android 中保存 Drag 'n' Drop in RecyclerView 后所做的更改 - 我已阅读所有内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57246034/

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