配景
对于安卓卡片式交互,已有许多案例,前有“探探”卡片滑动交互,后有各种各样的三方软件,都在互相复制粘贴。今项目中也有类似需求,特此记载。
!!!代码链接在文末!!!
演示gif
思绪
实现如许的结果,其实从宏观上,就是实现了一个layoutmanger以及ItemTouchHelper。
(一)LayoutManager重要是实现recyclerview的结构
(二)ItemTouchHelper重要是实现用户滑动的时间,卡片的交互过程
实现
(一)重写LayoutManger
起首,要确定的是,绘制多少个层级的结构。现在需求是表现三个叠加的item,因此,对于LayoutManager,我们只须要每次革新的时间,绘制三次即可。(ps:开发过程应当把“层级”变量抽象全局化,适配“层级”厘革的环境)
焦点代码如下:
@Override public void onLayoutChildren(RecyclerView.Recycler recycler, RecyclerView.State state) { super.onLayoutChildren(recycler, state); detachAndScrapAttachedViews(recycler); int maxCount = SlideConfig.SHOW_MAX_COUNT; //获取全部item(包罗不可见的)个数 int count = getItemCount(); //由于我们是倒序摆放,以是初始索引从背面开始 int initIndex = count - maxCount; if (initIndex < 0) { initIndex = 0; } //当前次序 int currentIndex = 0; for (int i = initIndex; i < count; i++) { //从缓存中获取view View view = recycler.getViewForPosition(i); //添加到recyclerView addView(view); //丈量一下view measureChild(view, 0, 0); //居中摆放,getDecoratedMeasuredWidth方法是获取带分割线的宽度,比直接使用view.getWidth()准确 int realWidth = getDecoratedMeasuredWidth(view); int realHeight = getDecoratedMeasuredHeight(view); int widthPadding = (int) ((getWidth() - realWidth) / 2f); int heightPadding = (int) ((getHeight() - realHeight) / 2f); //摆放child layoutDecorated(view, widthPadding, heightPadding, widthPadding + realWidth, heightPadding + realHeight); //根据索引,来位移和缩放child int measureHeight = view.getMeasuredHeight(); int trainY = SlideDpUtils.dp2px(SlideConfig.TRANSLATION_Y); RecyclerView.LayoutParams viewParams = (RecyclerView.LayoutParams) view.getLayoutParams(); int paramsHeight = viewParams.height; if (paramsHeight == -1) { viewParams.height = (measureHeight - trainY * (maxCount + 1));// viewParams.height = (500); view.setLayoutParams(viewParams); } float translationY = (maxCount - currentIndex - 1) * trainY; if (currentIndex != maxCount - 1) { view.setTranslationY(translationY); } view.setScaleX(1 - (maxCount - currentIndex - 1) * SlideConfig.SCALE); currentIndex++;// view.setScaleY(1 - level * SlideConfig.SCALE); } }可见,,其实也没啥,就是一些结构中,宽高的适配,以及一些位置盘算罢了。
其实这里的绘制逻辑,是和recyclerview中的LinearLayoutManager中的处置惩罚本事,有着异曲同工之妙,以是,这里就不在叙述了。
值得注意的是,在绘制的过程中,LayoutManager有个居中的逻辑,假如结构高度太小,则会出现上下隔断过宽的标题。假如结构高度添补满了,则会出现表现不全堆叠结果的标题。
这里的办理思绪就是,当结构高度未添补满的环境下,则举行控件的高度举行重新设置,盘算好毛病隔断,然后举行Y轴方向的偏移。
(二)重写ItemTouchHelper
对于ItemTouchHelper,须要在swipe方法举行数据的移除,界面革新以及监听的回调,焦点代码如下:
@Override public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int i) { //关照adapter if (mAdapter != null) { int currentPosition = viewHolder.getLayoutPosition(); //由于是限定了永世只有三个,以是,该current position的值永世少于等于 mAdapter.getDataList().remove(currentPosition);// mAdapter.getDataList().add(0, s); mAdapter.notifyDataSetChanged(); mAdapter.notifyOutSideRefresh(); mAdapter.notifyPageChange(); } }而对于触摸的场景下,卡片的宽高,缩放等设置,则在onChildDraw方法实现即可,焦点代码如下:
@Override public void onChildDraw(@NonNull Canvas c, @NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) { try { //盘算移动隔断 float distance = (float) Math.hypot(dX, dY); float maxDistance = recyclerView.getWidth() / 2f; //比例 float fraction = distance / maxDistance; if (fraction > 1) { fraction = 1; } int maxCount = SlideConfig.SHOW_MAX_COUNT; int trainY = SlideDpUtils.dp2px(SlideConfig.TRANSLATION_Y); //为每个child实验动画 int count = recyclerView.getChildCount(); int adapterCount = mAdapter.getDataList().size(); for (int i = 0; i < count; i++) { if (i != count - 1 && adapterCount > maxCount) { //不是第一层--且数目大于3 View view = recyclerView.getChildAt(i); view.setScaleX(1 - (maxCount - i - 1) * SlideConfig.SCALE + fraction * SlideConfig.SCALE); view.setTranslationY((maxCount - i - 1) * trainY - fraction * trainY); }// int level = SlideConfig.SHOW_MAX_COUNT - i - 1;// if (level != SlideConfig.SHOW_MAX_COUNT)// //获取的view从下层到上层// View view = recyclerView.getChildAt(i);// int level = SlideConfig.SHOW_MAX_COUNT - i - 1;// //level范围(SlideConfig.SHOW_MAX_COUNT-1)-0,每个child最大只移动一个SlideConfig.TRANSLATION_Y和放大SlideConfig.SCALE//// if (level == SlideConfig.SHOW_MAX_COUNT - 1) { // 最下层的不动和末了第二层重叠// view.setTranslationY(SlideConfig.TRANSLATION_Y * (level - 1));// view.setScaleX(1 - SlideConfig.SCALE * (level - 1));// view.setScaleY(1 - SlideConfig.SCALE * (level - 1));// } else if (level > 0) {// view.setTranslationY(level * SlideConfig.TRANSLATION_Y - fraction * SlideConfig.TRANSLATION_Y);// view.setScaleX(1 - level * SlideConfig.SCALE + fraction * SlideConfig.SCALE);// view.setScaleY(1 - level * SlideConfig.SCALE + fraction * SlideConfig.SCALE);// } } } catch (Exception e) { } super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive); }这里,就实现了recyclerview卡片堆叠结果了。
代码所在--库libslidrecyclerview
that's all--------------------------------------------------------------------- |