RecyclerView Swipe with a view below it

前端 未结 6 1027
囚心锁ツ
囚心锁ツ 2020-12-07 11:24

I have been doing some research and I have yet to find an example or implementation that allows you to put a view (Example Image Below) underneath the RecyclerView when you

6条回答
  •  误落风尘
    2020-12-07 11:45

    Simple solution without allocation or drawing on canvas. SomeAdapter.SomeVH should contain upper view and under view. And with this approach we will be able to swipe only upper view (container), exposing under view (with label, icon whatever you want)

    class SomeTouchHelper extends ItemTouchHelper.Callback {
    ...
        @Override
        public void onChildDraw(Canvas c, RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder,
                float dX, float dY, int actionState, boolean isCurrentlyActive) {
            if (actionState == ItemTouchHelper.ACTION_STATE_SWIPE) {
                if (viewHolder instanceof SomeAdapter.SomeVH) {
                    SomeAdapter.SomeVH someViewHolder
                            = (SomeAdapter.SomeVH) viewHolder;
                    ViewCompat.setTranslationX(someViewHolder.mContainer, dX);
                }
            } else {
                super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);
            }
        }
    ...
    }
    

    Attach it

    new ItemTouchHelper(SomeTouchHelper).attachToRecyclerView(recyclerView);
    

    don't forget to restore initial view state of SomeVH in adapter onViewRecycled()

    public void onViewRecycled(final SomeVH holder) {
            if (holder.mContainer != null) {
                holder.mContainer.setTranslationX(0);//restore position
            }
    }
    

提交回复
热议问题