Showing custom View under swiped RecyclerView item

后端 未结 5 1096
独厮守ぢ
独厮守ぢ 2020-12-24 08:55

First of all, I saw this question: Adding a colored background with text/icon under swiped row when using Android's RecyclerView

However, even though the title s

5条回答
  •  长情又很酷
    2020-12-24 09:44

    I was investigating the same issue. What i ended up doing was, in the layout that contained the recyclerView, I added a simple FrameLayout called 'swipe_bg' of the same height as one of my RecyclerView.ViewHolder items. I set its visibility to "gone", and placed it under the RecyclerView

    Then in my activity where i set the ItemTouchHelper, I override the onChildDraw like so..

    final ItemTouchHelper.SimpleCallback swipeCallback = new ItemTouchHelper.SimpleCallback(0,
                    ItemTouchHelper.LEFT|ItemTouchHelper.RIGHT){
    
    
                @Override
                public void onChildDraw(Canvas c, RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) {
                     View itemView = viewHolder.itemView;
                     swipe_bg.setY(itemView.getTop());
                     if(isCurrentlyActive) {
                         swipe_bg.setVisibility(View.VISIBLE);
                     }else{
                         swipe_bg.setVisibility(View.GONE);
                     }
                     super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);
            }
      };
    

    Don't know if that is the best way, but seemed like the simplest way.

提交回复
热议问题