ATES
ATES

Reputation: 337

Notify the recyclerview item when it is onMoving without interruption

I've expandable row layouts in RecyclerView, normally they're expanded. Atteched ItemTouchHelper.SimpleCallback to the RecyclerView for moving the rows with onMove(). Everythings is right and moving the rows correctly.

ItemTouchHelper.SimpleCallback:

@Override
public boolean onMove(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, @NonNull RecyclerView.ViewHolder target) {

    int fromPosition = viewHolder.getAdapterPosition();
    int toPosition = target.getAdapterPosition();

    Collections.swap(adapter.getShelves(), fromPosition, toPosition);
    adapter.notifyItemMoved(fromPosition, toPosition);

    return true;
}

// Minifying the rows before the onMove is started:
@Override
public boolean isLongPressDragEnabled() {
    adapter.minifyRow(position);
    return true;
}

It should minify the rows when long pressing, before onMoving() is started. Then should start the moving without any interrupting. The problem is starting dragging and minifiying the row when long press but interrupting the dragging at the moment. So it doesn't dragging or moving the row. How to simulate onMove with notify?

In adapter class:

@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {    
    boolean rowIsExpanded = rows.get(position).isExpanded();
    if(rowIsExpanded){
        holder.rowLayout.setVisibility(View.VISIBLE);
    } else {
        holder.rowLayout.setVisibility(View.GONE);
    }   
}

public void minifyRow(boolean situation, int adapterPos){
    RowModel row = rows.get(adapterPos);
    row.setExpanded(situation);
    notifyItemChanged(adapterPos);
}

Upvotes: 1

Views: 182

Answers (1)

Công Hải
Công Hải

Reputation: 5241

You shouldn't call adapter.minifyRow(position); inside isLongPressDragEnabled it only flags to check your adapter enable long-press or not. You should override onSelectedChanged. Remove method in isLongPressDragEnabled and Try this

    @Override
    public void onSelectedChanged(@Nullable RecyclerView.ViewHolder viewHolder, int actionState) {
        if (viewHolder != null && actionState == ItemTouchHelper.ACTION_STATE_DRAG) {
            ((YourViewHolder)viewHolder).rowLayout.setVisibility(View.GONE);
        }
    }

Upvotes: 1

Related Questions