保持拖拽和重新启动时丢弃的项目位置

时间:2016-01-27 12:32:22

标签: android drag-and-drop android-recyclerview

我有一个RecyclerView - Grid,带有拖放功能。 drop,使用这个code我已经设法实现了这一点,并做了很多改变,只有一个问题,我无法在重启时保存被拖动的项目位置(应用程序不是手机)。 我想到的是将“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);
    }
};

这是onCreate中的代码:

    ItemTouchHelper ith = new ItemTouchHelper(_ithCallback);
    ith.attachToRecyclerView(RcView);

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

    @Override
public void onStop()
{
    super.onStop();
    SharedPreferencesTools.setOrderedItems(this, AllItems);
}

getAllItemList:

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

}

1 个答案:

答案 0 :(得分:7)

只需将修改后的收藏集AllItems保留在SharedPreferences中,然后将其加载到应用开始时间&amp;一旦你离开应用程序,就把它存回来。

为此,您需要按Gson将集合序列化为json并存储为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);
    }
}

这两种方法的用法:

 SharedPreferencesTools.setOrderedItems(getActivity(), AllItems);
 ...
 List<Item> AllItems = SharedPreferencesTools.getOrderedItems(getActivity());