Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maintain Drag & Dropped items position when restarting

I have a RecyclerView - Grid, with drag & drop, using this code I've manage to achieve that, And made a lot of changes, only one problem, i can't save the dragged items position on restarting ( the app not the phone ). What i thought about is adding " int position " to my item.java constructor, but what i can't do is getting the changed position .

I'm using the same drag & drop codes provided in the 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);
    }
};

Here's the 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 

}

like image 678
Jaeger Avatar asked Jan 27 '16 12:01

Jaeger


People also ask

When should you tighten your drag?

For best results, the drag setting should be able at the point where the line holds a third to half of its weight before moving (e.g., a 20-pound line should not move until the hook holds seven to ten pounds).

Do you lubricate drag washers?

Grease it and the max drag gets better. Go figure. In a saltwater setup, I wouldn't even hesitate to grease the carbon washers. This extra grease will aid in fighting off the ever corrosive saltwater and keep the washers from drying out and collecting salt air moisture in them, causing the drag to become jerky.


1 Answers

Just keep your modified collection AllItems in SharedPreferences and load it on the app start & store it back, once you get out of the app.

To do this, you need to serialize your collection to json by Gson and store as a String. And then deserialize it 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 two methods:

 SharedPreferencesTools.setOrderedItems(getActivity(), AllItems);
 ...
 List<Item> AllItems = SharedPreferencesTools.getOrderedItems(getActivity());
like image 51
Konstantin Loginov Avatar answered Oct 11 '22 14:10

Konstantin Loginov