我想到的是在我的item.java构造函数中添加“int position”,但我不能做的就是获得改变的位置.
我正在使用相同的拖放链接中提供的丢弃代码.
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);
更改位置后获取重复的项目,代码:
@OverrIDepublic voID onStop(){ super.onStop(); SharedPreferencesTools.setorderedItems(this,Allitems);}
getAllitemList:
private List<Item> getAllitemList(){Allitems = SharedPreferencesTools.getorderedItems(this);//Add item .. etc //return items
}
解决方法 只需将修改后的收藏Allitems保存在SharedPreferences中,并将其加载到应用程序开始&一旦您退出应用程序,就将其存储起来.为此,您需要将您的集合序列化为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());总结
以上是内存溢出为你收集整理的android – 重新启动时保持拖放项目的位置全部内容,希望文章能够帮你解决android – 重新启动时保持拖放项目的位置所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)