//扩展实现:点击按钮实现右滑效果
public void toRight(RecyclerView recyclerView) { if (check(recyclerView)) {
animTo(recyclerView, true); } }
private void animTo(final RecyclerView recyclerView, boolean right) { final int position = recyclerView.getAdapter().getItemCount() - 1; final View view = recyclerView.findViewHolderForAdapterPosition(position).itemView;
TranslateAnimation translateAnimation = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0,
Animation.RELATIVE_TO_SELF, right ? 1f : -1f,
Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, 1.3f);
translateAnimation.setFillAfter(true); translateAnimation.setDuration(300);
translateAnimation.setInterpolator(new DecelerateInterpolator());
translateAnimation.setAnimationListener(new Animation.AnimationListener() { @Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) { isSwipeAnim = falnc630.comse; recyclerView.removeView(view); notifyListener(position,
x > view.getMeasuredWidth() / 2 ?
ItemTouchHelper.RIGHT : ItemTouchHelper.LEFT); }
@Override
public void onAnimationRepeat(Animation animation) {
} });
view.startAnimation(translateAnimation); }
private boolean check(RecyclerView recyclerView) { if (isSwipeAnim) { return false; }
if (recyclerView == null || recyclerView.getAdapter() == null) { return false; }
if (recyclerView.getAdapter().getItemCount() == 0) { return false; }
isSwipeAnim = true; return true; }
public interface OnSwipeListener {
/** * @param direction {@link ItemTouchHelper#LEFT} / {@link ItemTouchHelper#RIGHT}
* {@link ItemTouchHelper#UP} or {@link ItemTouchHelper#DOWN}). */
void onSwiped(int adapterPosition, int direction);
/**
* 最上层View滑动时回调. *
* @param viewHolder 最上层的ViewHolder * @param offset 距离原始位置的偏移量 */
void onSwipeTo(RecyclerView.ViewHolder viewHolder, float offset); }
public static class SimpleSwipeCallback implements OnSwipeListener {
/**
* {@inheritDoc} */
@Override
public void onSwiped(int adapterPosition, int direction) {
}
/**
* {@inheritDoc} */
@Override
public void onSwipeTo(RecyclerView.ViewHolder viewHolder, float offset) {
} } }
看起来不难, 但是真正做的时候, 要处理的地方很多,
并且有些地方要思考很久, 才能实现效果.
总之,做了你才会发现1+1=2的魅力, just do it.