Android 炫动滑动 卡片层叠布局,仿探探、人人影视订阅界面 简单&优雅:LayoutManager+ItemTouchHelper

本文介绍了如何在Android中实现炫动滑动和卡片层叠布局,类似探探和人人影视订阅界面的效果。通过OverLayCardLayoutManager和ItemTouchHelper,实现了滑动删除、滑动时的动画效果,详细讲解了配置、布局管理和触摸监听。文章最后还提到了如何实现类似探探应用的旋转和透明度变化效果。
摘要由CSDN通过智能技术生成

public static int MAX_SHOW_COUNT;

//每一级Scale相差0.05f,translationY相差7dp左右

public static float SCALE_GAP;

public static int TRANS_Y_GAP;

public static void initConfig(Context context) {

MAX_SHOW_COUNT = 6;

SCALE_GAP = 0.05f;

TRANS_Y_GAP = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 15, context.getResources().getDisplayMetrics());

}

}

LayoutManager全部代码如下,布满注释,如果看不懂,建议阅读前置文章LayoutManger系列

public class OverLayCardLayoutManager extends RecyclerView.LayoutManager {

@Override

public RecyclerView.LayoutParams generateDefaultLayoutParams() {

return new RecyclerView.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);

}

@Override

public void onLayoutChildren(RecyclerView.Recycler recycler, RecyclerView.State state) {

detachAndScrapAttachedViews(recycler);

int itemCount = getItemCount();

if (itemCount >= MAX_SHOW_COUNT) {

//从可见的最底层View开始layout,依次层叠上去

for (int position = itemCount - MAX_SHOW_COUNT; position < itemCount; position++) {

View view = recycler.getViewForPosition(position);

addView(view);

measureChildWithMargins(view, 0, 0);

int widthSpace = getWidth() - getDecoratedMeasuredWidth(view);

int heightSpace = getHeight() - getDecoratedMeasuredHeight(view);

//我们在布局时,将childView居中处理,这里也可以改为只水平居中

layoutDecoratedWithMargins(view, widthSpace / 2, heightSpace / 2,

widthSpace / 2 + getDecoratedMeasuredWidth(view),

heightSpace / 2 + getDecoratedMeasuredHeight(view));

/**

  • TopView的Scale 为1,translationY 0

  • 每一级Scale相差0.05f,translationY相差7dp左右

  • 观察人人影视的UI,拖动时,topView被拖动,Scale不变,一直为1.

  • top-1View 的Scale慢慢变化至1,translation也慢慢恢复0

  • top-2View的Scale慢慢变化至 top-1View的Scale,translation 也慢慢变化只top-1View的translation

  • top-3View的Scale要变化,translation岿然不动

*/

//第几层,举例子,count =7, 最后一个TopView(6)是第0层,

int level = itemCount - position - 1;

//除了顶层不需要缩小和位移

if (level > 0 /&& level < mShowCount - 1/) {

//每一层都需要X方向的缩小

view.setScaleX(1 - SCALE_GAP * level);

//前N层,依次向下位移和Y方向的缩小

if (level < MAX_SHOW_COUNT - 1) {

view.setTranslationY(TRANS_Y_GAP * level);

view.setScaleY(1 - SCALE_GAP * level);

} else {//第N层在 向下位移和Y方向的缩小的成都与 N-1层保持一致

view.setTranslationY(TRANS_Y_GAP * (level - 1));

view.setScaleY(1 - SCALE_GAP * (level - 1));

}

}

}

}

}

}

撸到这里,我们的静态界面已经成型

ItemTouchHelper实现炫动滑动:

ItemTouchHelper的基础知识,建议大家自行学习,网上文章很多,我简单介绍一下,

This is a utility class to add swipe to dismiss and drag & drop support to RecyclerView.

It works with a RecyclerView and a Callback class, which configures what type of interactions

are enabled and also receives events when user performs these actions.

Depending on which functionality you support, you should override

{@link Ca

  • 7
    点赞
  • 28
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值