0
votes

I have application in which I am getting data from sqlite to recyclerview. features provided to recyclerview are, item of recyclerview can be drag and drop and deleting item at swap. After drag and drop I am I am deleting whole database table and storing new Array which is contain item in updated order after drag and drop. I am also doing same deleting and storing stuff after deleting item by swap. Problem was occurred when deleting item after swap, I solved that problem by reset adapter item(by using notifyDataSetChanged()) after drag and drop event so that at time of delete, new item position list is available.

Now comes to what is current problem is : For checking Drag and drop event is occurred or not I am using flag(boolean) inside one interfere, that Interface only called if drag and drop event is happened. So if that event is true then Deleting data and storing new list in database and calling notifyDataSetChanged(). Problem is if I drag and drop even single item that flag is return true because of that notifyDataSetChanged() is called so I can't drag item position more than one if user want to drag item after two or three item he can't. So is there anyway I can use notifyDataSetChanged after all drag and drop event is completed , currently only one position can be drag.

Code inside Adapter for drag and drop stuff.

  @Override
    public boolean onItemMove(int fromPosition, int toPosition) {
        if (fromPosition < toPosition) {
            for (int i = fromPosition; i < toPosition; i++) {
                Collections.swap(customDataListModels, i, i + 1);

            }
            Log.e("UpTODown", String.valueOf(toPosition));
        } else {
            for (int i = fromPosition; i > toPosition; i--) {
                Collections.swap(customDataListModels, i, i - 1);

            }
            Log.e("DownToUp", String.valueOf(toPosition));
        }
            Log.e("postion", "from pos : "+String.valueOf(fromPosition)+" : to Position :"+String.valueOf(toPosition));

        mListChangedListener.onNoteListChanged(customDataListModels);

        notifyItemMoved(fromPosition, toPosition);

        chCustom=customDataListModels;
        if (swapChecker == true) {
            tempSqliteDatabaseHelper.deleteall();
            for (int i = 0; i < chCustom.size(); i++) {
                Log.e("PositionInSwap", String.valueOf(chCustom.get(i).getName()));
                tempinsert(chCustom.get(i).getName(), chCustom.get(i).getNumber(), chCustom.get(i).getColor(), chCustom.get(i).getFSize(), 1);

            }
            notifyDataSetChanged();
        }
        return true;
    }      

In above code swapChecker == true is true at when drag and drop event happened.

  @Override
    public void onNoteListChanged(List<CustomDataListModel> customDataListModels) {
        swapChecker = true;
    }

tempinsert method is storing into database. which is not related to current problem at all still if u curious about code then:

 public void tempinsert(String name, String num, String color, int size, int index) {
        boolean inSamedata = tempSqliteDatabaseHelper.IsItemExist(name, num);
        if (inSamedata == false) {
            boolean indata = tempSqliteDatabaseHelper.insertData(new ContactModel(name, num, color, size, index));
            if (indata == true) {
                Log.e("DataSave", "DataSave");
            } else {
                Log.e("In_Data_not_True", "In Data not True");
            }
        } else if (inSamedata == true) {
            Log.e("Record_Exist", "Record Exist");
        }
    }

Interface which is Implemented in Adapter :

public class CustomRecycleAdapter extends RecyclerView.Adapter<CustomRecycleAdapter.MyContactHolder> implements ItemTouchHelperAdapter{

}


  public interface ItemTouchHelperAdapter {

}
        void onRowSelected(CustomRecycleAdapter.MyContactHolder myViewHolder);

        void onRowClear(CustomRecycleAdapter.MyContactHolder myViewHolder);

        boolean onItemMove(int fromPosition, int toPosition);
    }  

onItemMove called in onMove method of SimpleItemTouchHelperCallback class which is extends from ItemTouchHelper.Callback class

public class SimpleItemTouchHelperCallback extends ItemTouchHelper.Callback {

    private final ItemTouchHelperAdapter mAdapter;

    public SimpleItemTouchHelperCallback(ItemTouchHelperAdapter adapter) {
        mAdapter = adapter;
    }

    @Override
    public boolean isLongPressDragEnabled() {
        return true;
    }

    @Override
    public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
        int dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN;
        return makeMovementFlags(dragFlags, 0);
    }

    @Override
    public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder,
                          RecyclerView.ViewHolder target) {
        mAdapter.onItemMove(viewHolder.getAdapterPosition(), target.getAdapterPosition());
        return true;
    }

    @Override
    public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {

    }

}

I posted The code of interface and code below it because some user suggested me to use onRowSelected and onRowClear override methods. but I notices that I can't use those method directly.so I pull those method from Interface.

1

1 Answers

0
votes
**IN RECYCLER VIEW ADAPTER**

@Override
public void onRowSelected(MyViewHolder myViewHolder) {
    //called when user select item and start dragging.
    //So, here you can start timer

}

@Override
public void onRowClear(MyViewHolder myViewHolder) {
    // called when user drop the item
    // here stop timer and calculate the time
}