实例介绍
android仿探探卡片滑动
public class TanTanCallback extends RenRenCallback { private static final int MAX_ROTATION = 15; //2016 12 26 考虑 探探垂直上下方向滑动,不删除卡片, //判断 此次滑动方向是否是竖直的 ,水平方向上的误差(阈值,默认我给了50dp) int mHorizontalDeviation; public TanTanCallback(RecyclerView rv, RecyclerView.Adapter adapter, List datas) { //this(0, ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT, rv, adapter, datas); super(rv, adapter, datas); mHorizontalDeviation = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 50, mRv.getContext().getResources().getDisplayMetrics()); } public TanTanCallback(int dragDirs, int swipeDirs, RecyclerView rv, RecyclerView.Adapter adapter, List datas) { super(dragDirs, swipeDirs, rv, adapter, datas); } public int getHorizontalDeviation() { return mHorizontalDeviation; } public TanTanCallback setHorizontalDeviation(int horizontalDeviation) { mHorizontalDeviation = horizontalDeviation; return this; } @Override public float getSwipeThreshold(RecyclerView.ViewHolder viewHolder) { /* Log.d("TAG", "getSwipeThreshold() called with: viewHolder.itemView.getX() = [" viewHolder.itemView.getX() "]"); Log.d("TAG", "getSwipeThreshold() called with: viewHolder.itemView.getWidth() / 2 = [" viewHolder.itemView.getWidth() / 2 "]"); Log.d("TAG", "getSwipeThreshold() called with: mRv.getX() = [" mRv.getX() "]"); Log.d("TAG", "getSwipeThreshold() called with: mRv.getWidth() / 2 = [" mRv.getWidth() / 2 "]");*/ if (isTopViewCenterInHorizontal(viewHolder.itemView)) { return Float.MAX_VALUE; } return super.getSwipeThreshold(viewHolder); } @Override public float getSwipeEscapeVelocity(float defaultValue) { View topView = mRv.getChildAt(mRv.getChildCount() - 1); if (isTopViewCenterInHorizontal(topView)) { return Float.MAX_VALUE; } return super.getSwipeEscapeVelocity(defaultValue); } @Override public float getSwipeVelocityThreshold(float defaultValue) { View topView = mRv.getChildAt(mRv.getChildCount() - 1); if (isTopViewCenterInHorizontal(topView)) { return Float.MAX_VALUE; } return super.getSwipeVelocityThreshold(defaultValue); } /** * 返回TopView此时在水平方向上是否是居中的 * * @return */ public boolean isTopViewCenterInHorizontal(View topView) { Log.d("TAG", "getSwipeThreshold() called with: viewHolder.itemView.getX() = [" topView.getX() "]"); Log.d("TAG", "getSwipeThreshold() called with: viewHolder.itemView.getWidth() / 2 = [" topView.getWidth() / 2 "]"); Log.d("TAG", "getSwipeThreshold() called with: mRv.getX() = [" mRv.getX() "]"); Log.d("TAG", "getSwipeThreshold() called with: mRv.getWidth() / 2 = [" mRv.getWidth() / 2 "]"); return Math.abs(mRv.getWidth() / 2 - topView.getX() - (topView.getWidth() / 2)) < mHorizontalDeviation; } @Override public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) { super.onSwiped(viewHolder, direction); //如果不需要循环删除 /* Object remove = mDatas.remove(viewHolder.getLayoutPosition()); mAdapter.notifyDataSetChanged();*/ Log.e("swipecard", "厉害了"); /* if (isLeftSwipe){ Toast.makeText(mRv.getContext(), "左滑删除", Toast.LENGTH_SHORT).show(); }else { Toast.makeText(mRv.getContext(), "右滑删除", Toast.LENGTH_SHORT).show(); }*/ //探探只是第一层加了rotate & alpha的操作 //对rotate进行复位 viewHolder.itemView.setRotation(0); //自己感受一下吧 Alpha if (viewHolder instanceof ViewHolder) { ViewHolder holder = (ViewHolder) viewHolder; holder.setAlpha(R.id.iv_love, 0); holder.setAlpha(R.id.iv_del, 0); } } @Override public void onChildDraw(Canvas c, RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) { super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive); Log.e("swipecard", "onChildDraw() viewHolder = [" viewHolder "], dX = [" dX "], dY = [" dY "], actionState = [" actionState "], isCurrentlyActive = [" isCurrentlyActive "]"); //探探的效果 double swipValue = Math.sqrt(dX * dX dY * dY); double fraction = swipValue / getThreshold(viewHolder); //边界修正 最大为1 if (fraction > 1) { fraction = 1; } int childCount = recyclerView.getChildCount(); for (int i = 0; i < childCount; i ) { View child = recyclerView.getChildAt(i); //第几层,举例子,count =7, 最后一个TopView(6)是第0层, int level = childCount - i - 1; if (level > 0) { child.setScaleX((float) (1 - SCALE_GAP * level fraction * SCALE_GAP)); if (level < MAX_SHOW_COUNT - 1) { child.setScaleY((float) (1 - SCALE_GAP * level fraction * SCALE_GAP)); child.setTranslationY((float) (TRANS_Y_GAP * level - fraction * TRANS_Y_GAP)); } else { //child.setTranslationY((float) (mTranslationYGap * (level - 1) - fraction * mTranslationYGap)); } } else { //探探只是第一层加了rotate & alpha的操作 //不过他区分左右 float xFraction = dX / getThreshold(viewHolder); //边界修正 最大为1 if (xFraction > 1) { xFraction = 1; } else if (xFraction < -1) { xFraction = -1; } //rotate child.setRotation(xFraction * MAX_ROTATION); //自己感受一下吧 Alpha if (viewHolder instanceof ViewHolder) { ViewHolder holder = (ViewHolder) viewHolder; if (dX > 0) { //露出左边,比心 holder.setAlpha(R.id.iv_love, xFraction); } else if (dX < 0) { //露出右边,滚犊子 holder.setAlpha(R.id.iv_del, -xFraction); } else { holder.setAlpha(R.id.iv_love, 0); holder.setAlpha(R.id.iv_del, 0); } } } } //可以在此判断左右滑: float v = mRv.getWidth() / 2 - viewHolder.itemView.getX() - (viewHolder.itemView.getWidth() / 2); if (v > 0) { isLeftSwipe = true; } else if (v < 0) { isLeftSwipe = false; } } //一个flag 判断左右滑 private boolean isLeftSwipe; }
@Override public void onChildDraw(Canvas c, RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) { super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive); //Log.e("swipecard", "onChildDraw() viewHolder = [" viewHolder "], dX = [" dX "], dY = [" dY "], actionState = [" actionState "], isCurrentlyActive = [" isCurrentlyActive "]"); //人人影视的效果 //if (isCurrentlyActive) { //先根据滑动的dxdy 算出现在动画的比例系数fraction double swipValue = Math.sqrt(dX * dX dY * dY); double fraction = swipValue / getThreshold(viewHolder); //边界修正 最大为1 if (fraction > 1) { fraction = 1; } //对每个ChildView进行缩放 位移 int childCount = recyclerView.getChildCount(); for (int i = 0; i < childCount; i ) { View child = recyclerView.getChildAt(i); //第几层,举例子,count =7, 最后一个TopView(6)是第0层, int level = childCount - i - 1; if (level > 0) { child.setScaleX((float) (1 - SCALE_GAP * level fraction * SCALE_GAP)); if (level < MAX_SHOW_COUNT - 1) { child.setScaleY((float) (1 - SCALE_GAP * level fraction * SCALE_GAP)); child.setTranslationY((float) (TRANS_Y_GAP * level - fraction * TRANS_Y_GAP)); } else { //child.setTranslationY((float) (mTranslationYGap * (level - 1) - fraction * mTranslationYGap)); } } } }public class RenRenCallback extends ItemTouchHelper.SimpleCallback { protected RecyclerView mRv; protected List mDatas; protected RecyclerView.Adapter mAdapter; public RenRenCallback(RecyclerView rv, RecyclerView.Adapter adapter, List datas) { this(0, ItemTouchHelper.DOWN | ItemTouchHelper.UP | ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT, rv, adapter, datas); } public RenRenCallback(int dragDirs, int swipeDirs , RecyclerView rv, RecyclerView.Adapter adapter, List datas) { super(dragDirs, swipeDirs); mRv = rv; mAdapter = adapter; mDatas = datas; } //水平方向是否可以被回收掉的阈值 public float getThreshold(RecyclerView.ViewHolder viewHolder) { //2016 12 26 考虑 探探垂直上下方向滑动,不删除卡片,这里参照源码写死0.5f return mRv.getWidth() * /*getSwipeThreshold(viewHolder)*/ 0.5f; } @Override public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) { return false; } @Override public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) { //Log.e("swipecard", "onSwiped() called with: viewHolder = [" viewHolder "], direction = [" direction "]"); //rollBack(viewHolder); //★实现循环的要点 Object remove = mDatas.remove(viewHolder.getLayoutPosition()); mDatas.add(0, remove); mAdapter.notifyDataSetChanged(); } @Override public void onChildDraw(Canvas c, RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) { super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive); //Log.e("swipecard", "onChildDraw() viewHolder = [" viewHolder "], dX = [" dX "], dY = [" dY "], actionState = [" actionState "], isCurrentlyActive = [" isCurrentlyActive "]"); //人人影视的效果 //if (isCurrentlyActive) { //先根据滑动的dxdy 算出现在动画的比例系数fraction double swipValue = Math.sqrt(dX * dX dY * dY); double fraction = swipValue / getThreshold(viewHolder); //边界修正 最大为1 if (fraction > 1) { fraction = 1; } //对每个ChildView进行缩放 位移 int childCount = recyclerView.getChildCount(); for (int i = 0; i < childCount; i ) { View child = recyclerView.getChildAt(i); //第几层,举例子,count =7, 最后一个TopView(6)是第0层, int level = childCount - i - 1; if (level > 0) { child.setScaleX((float) (1 - SCALE_GAP * level fraction * SCALE_GAP)); if (level < MAX_SHOW_COUNT - 1) { child.setScaleY((float) (1 - SCALE_GAP * level fraction * SCALE_GAP)); child.setTranslationY((float) (TRANS_Y_GAP * level - fraction * TRANS_Y_GAP)); } else { //child.setTranslationY((float) (mTranslationYGap * (level - 1) - fraction * mTranslationYGap)); } } } } }
好例子网口号:伸出你的我的手 — 分享!
相关软件
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
网友评论
我要评论