android - Maintain Drag & Dropped items position when restarting -
i have recyclerview - grid, drag & drop, using code i've manage achieve that, , made lot of changes, 1 problem, can't save dragged items position on restarting ( app not phone ). thought adding " int position " item.java constructor, can't getting changed position .
i'm using same drag & drop codes provided in link.
itemtouchhelper.callback _ithcallback = new itemtouchhelper.callback() { //and in imlpementaion of public boolean onmove(recyclerview recyclerview, recyclerview.viewholder viewholder, recyclerview.viewholder target) { // viewholder's , target's positions in adapter data, swap them collections.swap(allitems, viewholder.getadapterposition(), target.getadapterposition()); // , notify adapter dataset has changed rcadapter.notifyitemmoved(viewholder.getadapterposition(), target.getadapterposition()); return true; } @override public void onswiped(recyclerview.viewholder viewholder, int direction) { //todo } //defines 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); } };
here's code in oncreate :
itemtouchhelper ith = new itemtouchhelper(_ithcallback); ith.attachtorecyclerview(rcview);
getting duplicated items after position changing, code :
@override public void onstop() { super.onstop(); sharedpreferencestools.setordereditems(this, allitems); }
getallitemlist :
private list<item> getallitemlist(){ allitems = sharedpreferencestools.getordereditems(this); //add item .. etc //return items
}
just keep modified collection allitems
in sharedpreferences
, load on app start & store back, once out of app.
to this, need serialize collection json gson
, store string
. , deserialize afterwards:
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); } }
the usage of these 2 methods:
sharedpreferencestools.setordereditems(getactivity(), allitems); ... list<item> allitems = sharedpreferencestools.getordereditems(getactivity());
Comments
Post a Comment