OnSwipe method not working in RecyclerView

狂风中的少年 提交于 2019-12-10 22:09:57

问题


I'm working on an app for playing audio files. Created a tab layout with 2 tabs. Used fragment in both of them with RecyclerView being used in both of them. The fragment named LibraryFragment has this RecycleView whose items when Swipped must pass on to the HomeFragment, that's what I wanna do. But even though swiping animation is happening, OnSwipe method doesn't works at all.

This is the method I've used to provide the swiping function to my recyclerview's items under the LibraryFragment. Under OnCreateView method I call this initSwipe() method.

 private void initSwipe(){
    ItemTouchHelper.SimpleCallback simpleItemTouchCallback = new ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.LEFT ) {


        private boolean swipeBack = false;
        @Override
        public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
            return false;
        }

        @Override
        public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
            int position = viewHolder.getAdapterPosition();

          if (direction == ItemTouchHelper.LEFT){
                HomeFragment.audioList.clear();
                HomeFragment.audioList.add(audioList.get(position));
                HomeFragment.libraryAdapter.notifyDataSetChanged();
            Log.e(TAG, "onSwiped: " );
            }
        }


        @Override
        public void onChildDraw(Canvas c, RecyclerView recyclerView,
                                RecyclerView.ViewHolder viewHolder, float dX, float dY,
                                int actionState, boolean isCurrentlyActive) {
            recyclerView.setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View v, MotionEvent event)
                {
                    if(event.getAction() == MotionEvent.ACTION_UP || event.getAction() == MotionEvent.ACTION_CANCEL)
                    {
                        swipeBack = true;
                    }
                    else
                    {
                        swipeBack = false;
                    }
                    return false;
                }

            });
            super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);
        }

        @Override
        public int convertToAbsoluteDirection(int flags, int layoutDirection) {
            int value = swipeBack ? 0 : super.convertToAbsoluteDirection(flags, layoutDirection);
            swipeBack = false;
            return value;
        }


    };
    ItemTouchHelper itemTouchHelper = new ItemTouchHelper(simpleItemTouchCallback);
    itemTouchHelper.attachToRecyclerView(libraryRecyclerView);
}

please help out.


回答1:


The Event onSwiped() will be triggered when the item will be completely swiped out. Since you're holding the item view, this never happen

Here you can find an amazing tutorial where you can achieve this.



来源:https://stackoverflow.com/questions/46654609/onswipe-method-not-working-in-recyclerview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!