重新启动时保持拖放项目的位置
Maintain Drag & Dropped items position when restarting
我有一个 RecyclerView - 网格,可以拖放,使用这个 我已经设法实现了,并且做了很多更改,只有一个问题,我无法保存拖放重新启动时的项目位置(应用程序不是 phone )。
我想到的是在我的 item.java 构造函数中添加“ int position ”,但我做不到的是改变位置。
我正在使用 link 中提供的相同拖放代码。
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);
}
获取所有项目列表:
private List<Item> getAllItemList(){
AllItems = SharedPreferencesTools.getOrderedItems(this);
//Add item .. etc
//return items
}
只需将修改后的 collection AllItems
保存在 SharedPreferences
中,然后在应用程序启动时加载它,并在退出应用程序后将其存储回来。
为此,您需要通过 Gson
将 collection 序列化为 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());
我有一个 RecyclerView - 网格,可以拖放,使用这个
我正在使用 link 中提供的相同拖放代码。
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);
}
获取所有项目列表:
private List<Item> getAllItemList(){
AllItems = SharedPreferencesTools.getOrderedItems(this);
//Add item .. etc
//return items
}
只需将修改后的 collection AllItems
保存在 SharedPreferences
中,然后在应用程序启动时加载它,并在退出应用程序后将其存储回来。
为此,您需要通过 Gson
将 collection 序列化为 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());