我正在使用应用播放音频文件。创建了带有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);
}
请帮忙。