Android:有没有办法将ItemTouchHelper.SimpleCallback对象附加到单个CardView?

时间:2018-09-22 02:42:58

标签: android swipe android-cardview

我看到ItemTouchHelper.SimpleCallback对象的设置是将对象附加到RecyclerView。有没有一种方法可以将对象附加到单个CardView?我想用用户自己的Activity向用户显示单个CardView。当用户在单个CardView上滑动时,我试图显示落后的状态。下面是ItemTouchHelper类,用于在多个CardView的完整RecyclerView列表(“ RecyclerView活动”)的CardView中刷卡。

public class RecyclerItemTouchHelper extends ItemTouchHelper.SimpleCallback {
private RecyclerItemTouchHelperListener listener;

RecyclerItemTouchHelper(int dragDirs, int swipeDirs, RecyclerItemTouchHelperListener listener) {
    super(dragDirs, swipeDirs);
    this.listener = listener;
}

@Override
public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
    return true;
}

@Override
public void onSelectedChanged(RecyclerView.ViewHolder viewHolder, int actionState) {
    if (viewHolder != null) {
        final View foregroundView = ((MyRecylerAdapter.ItemHolder) viewHolder).viewForeground;getDefaultUIUtil().onSelected(foregroundView);
    }
} 

 @Override
public void onChildDrawOver(Canvas c, RecyclerView recyclerView,
    RecyclerView.ViewHolder viewHolder, float dX, float dY,
    int actionState, boolean isCurrentlyActive) {

    final View foregroundView = ((MyRecylerAdapter.ItemHolder) viewHolder).viewForeground;        
    getDefaultUIUtil().onDrawOver(c, recyclerView, foregroundView, dX, dY,
        actionState, isCurrentlyActive);
}

@Override
public void clearView(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {

    final View foregroundView = ((MyRecylerAdapter.ItemHolder) viewHolder).viewForeground;
    getDefaultUIUtil().clearView(foregroundView);
}

@Override
public void onChildDraw(Canvas c, RecyclerView recyclerView,
    RecyclerView.ViewHolder viewHolder, float dX, float dY,
    int actionState, boolean isCurrentlyActive) {

    final View foregroundView = ((MyRecylerAdapter.ItemHolder) viewHolder).viewForeground;        
    getDefaultUIUtil().onDraw(c, recyclerView, foregroundView, dX, dY,
        actionState, isCurrentlyActive);
} 

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

@Override
public int convertToAbsoluteDirection(int flags, int layoutDirection) {
    return super.convertToAbsoluteDirection(flags, layoutDirection);
}

public interface RecyclerItemTouchHelperListener {        
    void onSwiped(RecyclerView.ViewHolder viewHolder, int direction, int position);
}
}

// onSwiped() in the RecyclerView Activity 
// removes the CardView from the database 
// and then updates the RecyclerView's 
// adapter so the View is updated.

0 个答案:

没有答案