Android RecyclerView模仿探探左右滑动布局(2)

2019-09-01 20:52

//扩展实现:点击按钮实现右滑效果

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.


Android RecyclerView模仿探探左右滑动布局(2).doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:计算机基础4-6章

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: