Advanced Recycler View - Handling on item click and long click drag operations
<p>I'm using <a href="https://github.com/h6ah4i/android-advancedrecyclerview" rel="nofollow">advanced recycler view</a> to create drag &amp; drop grid view. I based my code on the example provided by the author of the library. Everything seems to work just fine, dragging and dropping nicely save it's state, swaps position etc., but I can't attach to this simple on item click listener. It's like <code>RecyclerViewDragDropManager</code> with it's wrapped adapter can only do those drag and drop operations.
I'm confused where click listener should go in that case, because normally in <code>RecyclerView</code> you can attach to your adapter in <code>bindView</code>, but my adapter fires up only after long click and handles only drag drop.</p>

<p>I tried every explanation from stack on how to attach click listener to RecyclerView, nothing seems to work.</p>

<p><strong>What I need is intercept action up if long click weren't fired up and know on which item it happened.</strong></p>

<p>Here's the code for the fragment setting up the recyclerview and the adapter:</p>

<pre><code>private void setLayout() {
    mRecyclerView = (RecyclerView) mView.findViewById(R.id.recycler_view);
    mLayoutManager = new GridLayoutManager(mContext, Const.GRID_ROW_SIZE, GridLayoutManager.VERTICAL, false);

    //drag &amp; drop manager
    mRecyclerViewDragDropManager = new RecyclerViewDragDropManager();
    mRecyclerViewDragDropManager.setDraggingItemShadowDrawable(
            (NinePatchDrawable) ContextCompat.getDrawable(mContext, R.drawable.material_shadow_z3));

    //start dragging after long press
    mRecyclerViewDragDropManager.setInitiateOnLongPress(true);
    mRecyclerViewDragDropManager.setInitiateOnMove(false);
    mRecyclerViewDragDropManager.setLongPressTimeout(Const.LONG_PRESS_DRAG_TIMEOUT);
}

private void setWrappedAdapter(DraggableGridAdapter draggableGridAdapter) {
    mAdapter = draggableGridAdapter;

    mWrappedAdapter = mRecyclerViewDragDropManager.createWrappedAdapter(draggableGridAdapter); //wrap for dragging

    final GeneralItemAnimator animator = new RefactoredDefaultItemAnimator();

    mRecyclerView.setLayoutManager(mLayoutManager);
    mRecyclerView.setAdapter(mWrappedAdapter); // requires *wrapped* adapter
    mRecyclerView.setItemAnimator(animator);

    if (supportsViewElevation()) {
        // Lollipop or later has native drop shadow feature. ItemShadowDecorator is not required.
    } else {
        mRecyclerView.addItemDecoration(new ItemShadowDecorator((NinePatchDrawable)
                ContextCompat.getDrawable(mContext, R.drawable.material_shadow_z1)));
    }

    mRecyclerViewDragDropManager.attachRecyclerView(mRecyclerView);

    mRecyclerViewDragDropManager.setItemMoveMode(RecyclerViewDragDropManager.ITEM_MOVE_MODE_DEFAULT);
    mAdapter.setItemMoveMode(RecyclerViewDragDropManager.ITEM_MOVE_MODE_DEFAULT);
}
</code></pre>

<p>And the code for the adapter:</p>

<pre><code>public class DraggableGridAdapter extends RecyclerView.Adapter&lt;DraggableGridAdapter.MyViewHolder&gt;
    implements DraggableItemAdapter&lt;DraggableGridAdapter.MyViewHolder&gt; {

private static final String TAG = "MyDraggableItemAdapter";
private int mItemMoveMode = RecyclerViewDragDropManager.ITEM_MOVE_MODE_DEFAULT;

// NOTE: Make accessible with short name
private interface Draggable extends DraggableItemConstants {
}

private AbstractDataProvider mProvider;

public static class MyViewHolder extends AbstractDraggableItemViewHolder {
    public FrameLayout mContainer;
    public View mDragHandle;
    public TextView mTextView;

    public MyViewHolder(View v) {
        super(v);
        mContainer = (FrameLayout) v.findViewById(R.id.container);
        mDragHandle = v.findViewById(R.id.drag_handle);
        mTextView = (TextView) v.findViewById(android.R.id.text1);
    }
}

public DraggableGridAdapter(AbstractDataProvider dataProvider) {
    mProvider = dataProvider;

    // DraggableItemAdapter requires stable ID, and also
    // have to implement the getItemId() method appropriately.
    // Ids must be unique not changing with position.
    setHasStableIds(true);
}

public void setItemMoveMode(int itemMoveMode) {
    mItemMoveMode = itemMoveMode;
}

@Override
public long getItemId(int position) {
    return mProvider.getItem(position).getId();
}

@Override
public int getItemViewType(int position) {
    return mProvider.getItem(position).getViewType();
}

@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    final LayoutInflater inflater = LayoutInflater.from(parent.getContext());
    final View v = inflater.inflate(R.layout.list_grid_item, parent, false);
    return new MyViewHolder(v);
}

@Override
public void onBindViewHolder(MyViewHolder holder, final int position) {
    final AbstractDataProvider.Data item = mProvider.getItem(position);

    // TODO: 2016-09-09 Add switch for checklist items

    //set text
    holder.mTextView.setText(item.getPlainText());

    final int dragState = holder.getDragStateFlags();

    if (((dragState &amp; Draggable.STATE_FLAG_IS_UPDATED) != 0)) {
        int bgResId;

        if ((dragState &amp; Draggable.STATE_FLAG_IS_ACTIVE) != 0) {
            bgResId = R.drawable.bg_item_dragging_active_state;
            com.example.Utilities.DrawableUtils.clearState(holder.mContainer.getForeground());
        } else if ((dragState &amp; Draggable.STATE_FLAG_DRAGGING) != 0) {
            bgResId = R.drawable.bg_item_dragging_state;
        } else {
            bgResId = R.drawable.bg_item_normal_state;
        }
        holder.mContainer.setBackgroundResource(bgResId);
    }

}


@Override
public int getItemCount() {
    return mProvider.getCount();
}

@Override
public void onMoveItem(int fromPosition, int toPosition) {
    Log.d(TAG, "onMoveItem(fromPosition = " + fromPosition + ", toPosition = " + toPosition + ")");

    if (fromPosition == toPosition) {
        return;
    }

    if (mItemMoveMode == RecyclerViewDragDropManager.ITEM_MOVE_MODE_DEFAULT) {
        mProvider.moveItem(fromPosition, toPosition);
        notifyItemMoved(fromPosition, toPosition);
    } else {
        mProvider.swapItem(fromPosition, toPosition);
        notifyDataSetChanged();
    }
}

@Override
public boolean onCheckCanStartDrag(MyViewHolder holder, int position, int x, int y) {
    return true;
}

@Override
public ItemDraggableRange onGetItemDraggableRange(MyViewHolder holder, int position) {
    // no drag-sortable range specified
    return null;
}

@Override
public boolean onCheckCanDrop(int draggingPosition, int dropPosition) {
    return true;
}
</code></pre>

<p>Here's the example code I tried but didn't worked, from adapter in onBindViewHolder method:</p>

<pre><code>public void onBindViewHolder(MyViewHolder holder, final int position) {

    holder.itemView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Log.d(TAG, "Clicked " + position);
        }
    });
}
</code></pre>