### Question:
ItemTouchHelper extends RecyclerView.ItemDecoration implements RecyclerView.OnChildAttachStateChangeListener { public void attachToRecyclerView(@Nullable RecyclerView recyclerView) { if (mRecyclerView == recyclerView) { return; } if (mRecyclerView != null) { destroyCallbacks(); } mRecyclerView = recyclerView; if (recyclerView != null) { final Resources resources = recyclerView.getResources(); mSwipeEscapeVelocity = resources .getDimension(R.dimen.item_touch_helper_swipe_escape_velocity); mMaxSwipeVelocity = resources .getDimension(R.dimen.item_touch_helper_swipe_escape_max_velocity); setupCallbacks(); } } ItemTouchHelper(@NonNull Callback callback); void attachToRecyclerView(@Nullable RecyclerView recyclerView); @Override void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state); @Override void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state); @Override void onChildViewAttachedToWindow(@NonNull View view); @Override void onChildViewDetachedFromWindow(@NonNull View view); @Override void getItemOffsets(Rect outRect, View view, RecyclerView parent,
            RecyclerView.State state); void startDrag(@NonNull ViewHolder viewHolder); void startSwipe(@NonNull ViewHolder viewHolder); static final int UP; static final int DOWN; static final int LEFT; static final int RIGHT; static final int START; static final int END; static final int ACTION_STATE_IDLE; @SuppressWarnings("WeakerAccess")
    static final int ACTION_STATE_SWIPE; @SuppressWarnings("WeakerAccess")
    static final int ACTION_STATE_DRAG; @SuppressWarnings("WeakerAccess")
    static final int ANIMATION_TYPE_SWIPE_SUCCESS; @SuppressWarnings("WeakerAccess")
    static final int ANIMATION_TYPE_SWIPE_CANCEL; @SuppressWarnings("WeakerAccess")
    static final int ANIMATION_TYPE_DRAG; }### Answer:
@Test public void attachToNullRecycleViewDuringLongPress() throws Throwable { final RecyclerViewState rvs = setupItemTouchHelper(setupRecyclerView(), END, 0); rvs.mLayoutManager.expectLayouts(1); setRecyclerView(rvs.mWrappedRecyclerView); rvs.mLayoutManager.waitForLayout(1); final RecyclerView.ViewHolder target = mRecyclerView .findViewHolderForAdapterPosition(1); target.itemView.setOnLongClickListener(new View.OnLongClickListener() { @Override public boolean onLongClick(View v) { mItemTouchHelper.attachToRecyclerView(null); return false; } }); TouchUtils.longClickView(getInstrumentation(), target.itemView); }
@Test public void attachToAnotherRecycleViewDuringLongPress() throws Throwable { final RecyclerViewState rvs2 = setupRecyclerView(); rvs2.mLayoutManager.expectLayouts(1); mActivityRule.runOnUiThread(new Runnable() { @Override public void run() { getActivity().getContainer().addView(rvs2.mWrappedRecyclerView); } }); rvs2.mLayoutManager.waitForLayout(1); final RecyclerViewState rvs = setupItemTouchHelper(setupRecyclerView(), END, 0); rvs.mLayoutManager.expectLayouts(1); setRecyclerView(rvs.mWrappedRecyclerView); rvs.mLayoutManager.waitForLayout(1); final RecyclerView.ViewHolder target = mRecyclerView .findViewHolderForAdapterPosition(1); target.itemView.setOnLongClickListener(new View.OnLongClickListener() { @Override public boolean onLongClick(View v) { mItemTouchHelper.attachToRecyclerView(rvs2.mWrappedRecyclerView); return false; } }); TouchUtils.longClickView(getInstrumentation(), target.itemView); assertEquals(0, mCalback.mHasDragFlag.size()); }