RecyclerView – Drag and Drop – move item over another item. (placeholder)

I have an Adapter, Recyclerview and ItemTouchHelper.
I want to make placeholders on which, as a result of moving, it will be possible to place items.
Placeholders are now made as empty elements. But I don’t know how I can put the element directly on this one.

enter image description here

My adapter:

public class DateTimeAdapter extends RecyclerView.Adapter<MainHolder> implements ItemTouchHelperAdapter {
    private List<MainItem> values;
    private RecyclerView.RecycledViewPool viewPool = new RecyclerView.RecycledViewPool();
    private Activity context;
    private final OnStartDragListener mDragStartListener;
    private boolean initEmpty = false;

    public DateTimeAdapter(Activity context, List<MainItem> values, OnStartDragListener dragListener) {
        this.values = values;
        this.context = context;
        mDragStartListener = dragListener;
    }


    @NonNull
    @Override
    public MainHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view;
        switch (viewType){
            case (Constants.ITEM_HEADER_TEXT_VIEWTYPE):
                view = LayoutInflater.from(context).inflate(R.layout.list_group_item, parent, false);
                return new HeaderHolder(view);
            case (Constants.ITEM_EVENT_TEXT_VIEWTYPE):
                view = LayoutInflater.from(context).inflate(R.layout.item_wo_items, parent, false);
                return new ItemHolder(view);
            case (Constants.ITEM_PLACEHOLDER_VIEWTYPE):
                view = LayoutInflater.from(context).inflate(R.layout.item_placeholder, parent, false);
                return new PlaceHolder(view);
            default: throw new IllegalArgumentException();
        }
    }

    @Override
    public void onBindViewHolder(@NonNull final MainHolder holder, int position) {
        holder.setData(values.get(position));

        if (holder.getItemViewType() == Constants.ITEM_PLACEHOLDER_VIEWTYPE){

        }
    }


    @Override
    public int getItemCount() {
        return values.size();
    }

    @Override
    public int getItemViewType(int position) {
        return values.get(position).getViewType();
    }

    protected LocalTime getStartTimebyHeader(int position){
        ListIterator<MainItem> listIterator = values.listIterator(position);
        while (listIterator.hasPrevious()){
            MainItem t = listIterator.previous();
            if (t.getHeaderItem() != null){
                DateTimeFormatter parse = new DateTimeFormatterBuilder().appendPattern("HH:mm").toFormatter();
                LocalTime localTime = LocalTime.parse(t.getHeaderItem().getHeaderText(), parse);
                return localTime;
            }
        }
        return null;
    }



    protected LocalTime getEndTimebyHeader(int position){
        ListIterator<MainItem> listIterator = values.listIterator(position);
        while (listIterator.hasNext()){
            MainItem t = listIterator.next();
            if (t.getHeaderItem() != null){
                DateTimeFormatter parse = new DateTimeFormatterBuilder().appendPattern("HH:mm").toFormatter();
                LocalTime localTime = LocalTime.parse(t.getHeaderItem().getHeaderText(), parse);
                return localTime;
            }
        }
        return null;
    }


    @Override
    public boolean onItemMove(int fromPosition, int toPosition) {
        if (fromPosition < toPosition) {
            for (int i = fromPosition; i < toPosition; i++) {
                Collections.swap(values, i, i + 1);
            }
        } else {
            for (int i = fromPosition; i > toPosition; i--) {
                Collections.swap(values, i, i - 1);
            }
        }
        notifyItemMoved(fromPosition, toPosition);

        return true;
    }

    @Override
    public void onItemDismiss(int position) {
        values.remove(position);
        notifyItemRemoved(position);
    }
}

My ItemTouchHelper

 ItemTouchHelper.SimpleCallback simpleCallback = new ItemTouchHelper.SimpleCallback(0, 0) {


            @Override
            public boolean canDropOver(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder current, @NonNull RecyclerView.ViewHolder target) {
                if (target.getAdapterPosition() != 0 && target.getAdapterPosition() != dta.getItemCount()-1)
                    return true;
                return false;
            }

            @Override
            public boolean onMove(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, @NonNull RecyclerView.ViewHolder target) {
                Log.d("onMove", Integer.toString(viewHolder.getAdapterPosition()) + " - " + Integer.toString(target.getAdapterPosition()));
                dta.onItemMove(viewHolder.getAdapterPosition(), target.getAdapterPosition());
                return true;

            }

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

            }

            @Override
            public int getMovementFlags(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder) {
                int dragFlags =  viewHolder.getItemViewType() == Constants.ITEM_EVENT_TEXT_VIEWTYPE ? ItemTouchHelper.UP | ItemTouchHelper.DOWN : 0;
                int swipeFlags =  viewHolder.getItemViewType() == Constants.ITEM_EVENT_TEXT_VIEWTYPE ? ItemTouchHelper.START | ItemTouchHelper.END : 0;
                return makeMovementFlags(dragFlags, swipeFlags);
            }

            @Override
            public void clearView(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder) {
                showingDatas.get(viewHolder.getAdapterPosition()).getEventItem().setTimeS(dta.getStartTimebyHeader(viewHolder.getAdapterPosition()));
                showingDatas.get(viewHolder.getAdapterPosition()).getEventItem().setTimeE(dta.getEndTimebyHeader(viewHolder.getAdapterPosition()));
                dta.notifyItemChanged(viewHolder.getAdapterPosition());
                super.clearView(recyclerView, viewHolder);
            }
        };

        ItemTouchHelper itemTouchHelper = new ItemTouchHelper(simpleCallback);
        itemTouchHelper.attachToRecyclerView(rvPlan);

How should I do it?

Source: Android Questions

LEAVE A COMMENT