重新启动时保持拖放项目位置

Jae*_*ger 10 android drag-and-drop android-recyclerview

我有一个RecyclerView - Grid,拖放,使用这个代码我已经设法实现了,并做了很多改变,只有一个问题,我无法保存拖动项目位置重新启动(应用程序不是电话 ).我想到的是将"int position"添加到我的item.java构造函数中,但我不能做的是获取更改的位置.

我正在使用链接中提供的相同拖放代码.

    ItemTouchHelper.Callback _ithCallback = new ItemTouchHelper.Callback() {
    //and in your imlpementaion of
    public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
        // get the viewHolder's and target's positions in your adapter data, swap them
        Collections.swap(AllItems, viewHolder.getAdapterPosition(), target.getAdapterPosition());
        // and notify the adapter that its dataset has changed

        rcAdapter.notifyItemMoved(viewHolder.getAdapterPosition(), target.getAdapterPosition());
        return true;
    }

    @Override
    public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
        //TODO
    }

    //defines the enabled move directions in each state (idle, swiping, dragging).
    @Override
    public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
        return makeFlag(ItemTouchHelper.ACTION_STATE_DRAG,
                ItemTouchHelper.DOWN | ItemTouchHelper.UP | ItemTouchHelper.START | ItemTouchHelper.END);
    }
};
Run Code Online (Sandbox Code Playgroud)

这是onCreate中的代码:

    ItemTouchHelper ith = new ItemTouchHelper(_ithCallback);
    ith.attachToRecyclerView(RcView);
Run Code Online (Sandbox Code Playgroud)

更改位置后获取重复项目,代码:

    @Override
public void onStop()
{
    super.onStop();
    SharedPreferencesTools.setOrderedItems(this, AllItems);
}
Run Code Online (Sandbox Code Playgroud)

getAllItemList:

private List<Item> getAllItemList(){
AllItems = SharedPreferencesTools.getOrderedItems(this);
//Add item .. etc 
//return items 
Run Code Online (Sandbox Code Playgroud)

}

Kon*_*nov 7

只要保持你的修改集合AllItemsSharedPreferences并加载它应用开始与它存回,一旦你退出应用.

要执行此操作,您需要将集合序列化为json by Gson并存储为String.然后将其反序列化:

public final class SharedPreferencesTools {
    private static final String USER_SETTINGS_PREFERENCES_NAME = "UserSettings";
    private static final String ALL_ITEMS_LIST = "AllItemsList";
    private static Gson gson = new GsonBuilder().create();

    public static List<Item> getOrderedItems(Context context) {
        String stringValue = getUserSettings(context).getString(ALL_ITEMS_LIST, "");
        Type collectionType = new TypeToken<List<Item>>() {
        }.getType();
        List<Item> result = gson.fromJson(stringValue, collectionType);
        return (result == null) ? new ArrayList<Item>() : result;
    }

    public static void setOrderedItems(Context context, List<Item> items) {
        String stringValue = gson.toJson(items);

        SharedPreferences sharedPreferences = getUserSettings(context);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putString(ALL_ITEMS_LIST, stringValue);
        editor.apply();        
    }

    static SharedPreferences getUserSettings(Context context) {
        return context.getSharedPreferences(USER_SETTINGS_PREFERENCES_NAME, Context.MODE_PRIVATE);
    }
}
Run Code Online (Sandbox Code Playgroud)

这两种方法的用法:

 SharedPreferencesTools.setOrderedItems(getActivity(), AllItems);
 ...
 List<Item> AllItems = SharedPreferencesTools.getOrderedItems(getActivity());
Run Code Online (Sandbox Code Playgroud)