OnSwipe方法在RecyclerView中不起作用

时间:2017-10-09 20:39:33

标签: android android-fragments android-recyclerview

我正在使用应用播放音频文件。创建了带有2个选项卡的选项卡布局。两个中使用的片段都在RecyclerView中使用。名为LibraryFragment的片段包含此RecycleView,其中Swipped的项目必须传递给HomeFragment,这就是我想做的事情。 但即使正在进行动画播放,OnSwipe方法根本无效。

这是我用来向LibraryFragment下的recyclerview项目提供滑动功能的方法。在OnCreateView方法下,我称之为initSwipe()方法。

 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);
}

请帮忙。

1 个答案:

答案 0 :(得分:1)

当项目完全清除时,将触发事件onSwiped()。由于您拥有项目视图,因此永远不会发生

Here,您会发现一个很棒的教程,可以实现这一目标。