百行代码打造高级联动特效

2024-02-29 08:08

本文主要是介绍百行代码打造高级联动特效,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

前两天突然看到一个联动效果蛮不错的,虽然不知道具体什么地方会用到。不过也随手鲁了一个。效果如下图:
这里写图片描述

效果是不是挺好玩的~~。那么让我们接下来一步步的分析一下。

思路

首先,让我们想象一下如何实现?自定义view?自定义layout?还是什么?首先这是多层布局的嵌套。肯定会发生的就是事件拦截和分发。我们想像一下Coordinatorlayout+Appbarlayout+CollapsingToolbarLayout和我们现在的效果是不是差不多?只是上者是一个这个是多个而已。于是我的想法就是鲁一个behavior来实现此效果。

自定义layout

首先我们把需要的title和Recyclerview放入一个自定义布局里面。这是为了简单我们的代码。所以把功能放到自定义layout里面。代码如下:

 private void initial(Context context, AttributeSet attrs) {LayoutInflater.from(context).inflate(R.layout.cardlayout, this);TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.SWCardLayout);TextView title = (TextView) findViewById(R.id.title);title.setText(array.getText(R.styleable.SWCardLayout_text));title.setBackgroundColor(array.getColor(R.styleable.SWCardLayout_backgroundcolor,Color.BLACK));RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycler);recyclerView.setLayoutManager(new LinearLayoutManager(context));NumberAdapter numberAdapter = new NumberAdapter(context);recyclerView.setAdapter(numberAdapter);array.recycle();}@Overrideprotected void onSizeChanged(int w, int h, int oldw, int oldh) {if (w != oldw || h != oldh) {titleheight = findViewById(R.id.title).getMeasuredHeight();}}public int getTitleheight() {return titleheight;}

这边代码我就不多说了。返回title的高度为了后面的计算。

随手鲁一个behavior

我们先自定义一个behavior放入我们刚刚写的自定义layout。代码如下:


public class CardBehavior extends Behavior<SWCardLayout> {private int childheight = 0;public boolean onStartNestedScroll(CoordinatorLayout coordinatorLayout, SWCardLayout child,View directTargetChild, View target, int nestedScrollAxes) {return (nestedScrollAxes & ViewCompat.SCROLL_AXIS_VERTICAL) != 0 && directTargetChild == child;}
}

至于nestedscrolling的这个方法我就不多少了。前面有几篇讲到这个的。
现在我们需要思考的是如何摆放他们的位置。其实behavior都帮我们处理好了。那就是onLayoutChild和OnmesureChild。控制他们的宽高和布局显示都在这2个方法里面。接下来我们看下这块的代码如何去写:

//控制子控件的onlayout和onmesure@Overridepublic boolean onLayoutChild(CoordinatorLayout parent, SWCardLayout child, int layoutDirection) {//按照默认的情况控制,会导致child重叠parent.onLayoutChild(child, layoutDirection);//控制顶部的偏移量(上一个距离顶部的边距+自身title高度)SWCardLayout frontChild = getFrontChild(parent, child);if (frontChild != null) {int offset = frontChild.getTop() + frontChild.getTitleheight();child.offsetTopAndBottom(offset);}childheight = child.getTop();return true;}@Overridepublic boolean onMeasureChild(CoordinatorLayout parent, SWCardLayout child, int parentWidthMeasureSpec, int widthUsed, int parentHeightMeasureSpec, int heightUsed) {//防止布局的重绘int offset = getChildOffset(parent, child);int height = View.MeasureSpec.getSize(parentHeightMeasureSpec) - offset;child.measure(parentWidthMeasureSpec, View.MeasureSpec.makeMeasureSpec(height, View.MeasureSpec.EXACTLY));return true;}

我们来分析下,先从onmesure说起。我们需要得到他的偏移量,也就是上一个的title的高度。如果是若干个布局,那就是若干的title的高度。计算title的偏移量的代码很简单,就是叠加title的高度:

    private int getChildOffset(CoordinatorLayout parent, SWCardLayout child) {int offset = 0;for (int i = 0; i < parent.getChildCount(); i++) {View view = parent.getChildAt(i);if (view != child) {if (view instanceof SWCardLayout) {offset += ((SWCardLayout) view).getTitleheight();}}}return offset;}

得到它的偏移量之后我们直接通过子view的onmesure布局扔进去。所以这个不是很难。

现在我们继续看onlayoutchild方法。我注释也写的比较明确了。如果单独通过layout放置的话,child会重叠在一起。那么计算方法就是计算前一个child顶部的距离加上他自身的title的高度。那么我们来看看如何得到前一个child。其他代码很简单,遍历父view就可以。代码如下:

 //得到前一个childprivate SWCardLayout getFrontChild(CoordinatorLayout parent, SWCardLayout child) {int index = parent.indexOfChild(child);for (int i = index - 1; i >= 0; i--) {View view = parent.getChildAt(i);if (view instanceof SWCardLayout) {return (SWCardLayout) view;}}return null;}

我们先来看下效果。
这里写图片描述

我们会发现现在是滚动不了的。那么既然显示都显示出来 了。还怕他滚不了。看老夫随手一段代码分分钟让他滚起来。

那么滚动的处理在那边呢。当然是在onnestprescroll里面。nestedscrolling的一个方法里面处理。这个我前面有介绍过。不了解nestedscrolling可以先翻翻之前的文章。

那么我们应该怎么处理呢?还是老套路,计算偏移量。我们需要先处理自己本身的滑动,在处理其他的滑动来实现联动效果。具体代码如下:

  //为了处理联动public void onNestedPreScroll(CoordinatorLayout coordinatorLayout, SWCardLayout child,View target, int dx, int dy, int[] consumed) {//先处理自己的int min = childheight;int max = childheight + child.getHeight() - child.getTitleheight();int top = child.getTop();int offset = Math.min(Math.max(top - dy, min), max) - top;child.offsetTopAndBottom(offset);consumed[1] = -offset;Log.i("----------", "onNestedPreScroll: " + consumed[1]);//再处理其他的if (consumed[1] == 0) {return;} else if (consumed[1] > 0) {//上滑SWCardLayout current = child;SWCardLayout cardLayout = getFrontChild(coordinatorLayout, child);while (cardLayout != null) {int layoutoffset = getHeightOffset(cardLayout, current);cardLayout.offsetTopAndBottom(-layoutoffset);current = cardLayout;cardLayout = getFrontChild(coordinatorLayout, current);}} else if (consumed[1] < 0) {//下滑SWCardLayout current = child;SWCardLayout cardLayout = getNextChild(coordinatorLayout, child);while (cardLayout != null) {int layoutoffset = getHeightOffset(current, cardLayout);cardLayout.offsetTopAndBottom(layoutoffset);current = cardLayout;cardLayout = getNextChild(coordinatorLayout, current);}}}

上滑的时候我们需要得到前一个的swcardlayout带着他滚动,下滑的时候我们需要得到下一个child进行滚动,得到下一个child的方法其实他之前得到前一个的方法是差不多的。代码如下:

private SWCardLayout getNextChild(CoordinatorLayout parent, SWCardLayout child) {int index = parent.indexOfChild(child);for (int i = index + 1; i < parent.getChildCount(); i++) {View view = parent.getChildAt(i);if (view instanceof SWCardLayout) {return (SWCardLayout) view;}}return null;}

那么就这么写完了。我们先来看下效果:
这里写图片描述

额。。。似乎翻车了。不过就看着图,我们知道应该是偏移量的问题搞错了。我们的偏移量必须要大于0应该。那么我们修改下代码。改成如下的:

else if (consumed[1] > 0) {//上滑SWCardLayout current = child;SWCardLayout cardLayout = getFrontChild(coordinatorLayout, child);while (cardLayout != null) {int layoutoffset = getHeightOffset(cardLayout, current);if (layoutoffset > 0) {cardLayout.offsetTopAndBottom(-layoutoffset);}current = cardLayout;cardLayout = getFrontChild(coordinatorLayout, current);}} else if (consumed[1] < 0) {//下滑SWCardLayout current = child;SWCardLayout cardLayout = getNextChild(coordinatorLayout, child);while (cardLayout != null) {int layoutoffset = getHeightOffset(current, cardLayout);if (layoutoffset > 0) {cardLayout.offsetTopAndBottom(layoutoffset);}current = cardLayout;cardLayout = getNextChild(coordinatorLayout, current);}}

判断下偏移量的值在做child的offset设置。我们在来看一下效果。
这里写图片描述

代码

一个成功的联动效果就这么出来了。最后我放出自定义behavior的整体代码:


public class CardBehavior extends Behavior<SWCardLayout> {private int childheight = 0;public boolean onStartNestedScroll(CoordinatorLayout coordinatorLayout, SWCardLayout child,View directTargetChild, View target, int nestedScrollAxes) {return (nestedScrollAxes & ViewCompat.SCROLL_AXIS_VERTICAL) != 0 && directTargetChild == child;}//为了处理联动public void onNestedPreScroll(CoordinatorLayout coordinatorLayout, SWCardLayout child,View target, int dx, int dy, int[] consumed) {//先处理自己的int min = childheight;int max = childheight + child.getHeight() - child.getTitleheight();int top = child.getTop();int offset = Math.min(Math.max(top - dy, min), max) - top;child.offsetTopAndBottom(offset);consumed[1] = -offset;Log.i("----------", "onNestedPreScroll: " + consumed[1]);//再处理其他的if (consumed[1] == 0) {return;} else if (consumed[1] > 0) {//上滑SWCardLayout current = child;SWCardLayout cardLayout = getFrontChild(coordinatorLayout, child);while (cardLayout != null) {int layoutoffset = getHeightOffset(cardLayout, current);if (layoutoffset > 0) {cardLayout.offsetTopAndBottom(-layoutoffset);}current = cardLayout;cardLayout = getFrontChild(coordinatorLayout, current);}} else if (consumed[1] < 0) {//下滑SWCardLayout current = child;SWCardLayout cardLayout = getNextChild(coordinatorLayout, child);while (cardLayout != null) {int layoutoffset = getHeightOffset(current, cardLayout);if (layoutoffset > 0) {cardLayout.offsetTopAndBottom(layoutoffset);}current = cardLayout;cardLayout = getNextChild(coordinatorLayout, current);}}}private SWCardLayout getNextChild(CoordinatorLayout parent, SWCardLayout child) {int index = parent.indexOfChild(child);for (int i = index + 1; i < parent.getChildCount(); i++) {View view = parent.getChildAt(i);if (view instanceof SWCardLayout) {return (SWCardLayout) view;}}return null;}private int getHeightOffset(SWCardLayout top, SWCardLayout bottom) {return top.getTop() + top.getTitleheight() - bottom.getTop();}//控制子控件的onlayout和onmesure@Overridepublic boolean onLayoutChild(CoordinatorLayout parent, SWCardLayout child, int layoutDirection) {//按照默认的情况控制,会导致child重叠parent.onLayoutChild(child, layoutDirection);//控制顶部的偏移量(上一个距离顶部的边距+自身title高度)SWCardLayout frontChild = getFrontChild(parent, child);if (frontChild != null) {int offset = frontChild.getTop() + frontChild.getTitleheight();child.offsetTopAndBottom(offset);}childheight = child.getTop();return true;}//得到前一个childprivate SWCardLayout getFrontChild(CoordinatorLayout parent, SWCardLayout child) {int index = parent.indexOfChild(child);for (int i = index - 1; i >= 0; i--) {View view = parent.getChildAt(i);if (view instanceof SWCardLayout) {return (SWCardLayout) view;}}return null;}@Overridepublic boolean onMeasureChild(CoordinatorLayout parent, SWCardLayout child, int parentWidthMeasureSpec, int widthUsed, int parentHeightMeasureSpec, int heightUsed) {//防止布局的重绘int offset = getChildOffset(parent, child);int height = View.MeasureSpec.getSize(parentHeightMeasureSpec) - offset;child.measure(parentWidthMeasureSpec, View.MeasureSpec.makeMeasureSpec(height, View.MeasureSpec.EXACTLY));return true;}private int getChildOffset(CoordinatorLayout parent, SWCardLayout child) {int offset = 0;for (int i = 0; i < parent.getChildCount(); i++) {View view = parent.getChildAt(i);if (view != child) {if (view instanceof SWCardLayout) {offset += ((SWCardLayout) view).getTitleheight();}}}return offset;}
}

当然那个SWCardLayout你们可以自己进行封装。想怎么玩怎么玩~。就是这么6。

这篇关于百行代码打造高级联动特效的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/758090

相关文章

Python实例题之pygame开发打飞机游戏实例代码

《Python实例题之pygame开发打飞机游戏实例代码》对于python的学习者,能够写出一个飞机大战的程序代码,是不是感觉到非常的开心,:本文主要介绍Python实例题之pygame开发打飞机... 目录题目pygame-aircraft-game使用 Pygame 开发的打飞机游戏脚本代码解释初始化部

Java中Map.Entry()含义及方法使用代码

《Java中Map.Entry()含义及方法使用代码》:本文主要介绍Java中Map.Entry()含义及方法使用的相关资料,Map.Entry是Java中Map的静态内部接口,用于表示键值对,其... 目录前言 Map.Entry作用核心方法常见使用场景1. 遍历 Map 的所有键值对2. 直接修改 Ma

深入解析 Java Future 类及代码示例

《深入解析JavaFuture类及代码示例》JavaFuture是java.util.concurrent包中用于表示异步计算结果的核心接口,下面给大家介绍JavaFuture类及实例代码,感兴... 目录一、Future 类概述二、核心工作机制代码示例执行流程2. 状态机模型3. 核心方法解析行为总结:三

python获取cmd环境变量值的实现代码

《python获取cmd环境变量值的实现代码》:本文主要介绍在Python中获取命令行(cmd)环境变量的值,可以使用标准库中的os模块,需要的朋友可以参考下... 前言全局说明在执行py过程中,总要使用到系统环境变量一、说明1.1 环境:Windows 11 家庭版 24H2 26100.4061

Python使用Tkinter打造一个完整的桌面应用

《Python使用Tkinter打造一个完整的桌面应用》在Python生态中,Tkinter就像一把瑞士军刀,它没有花哨的特效,却能快速搭建出实用的图形界面,作为Python自带的标准库,无需安装即可... 目录一、界面搭建:像搭积木一样组合控件二、菜单系统:给应用装上“控制中枢”三、事件驱动:让界面“活”

pandas实现数据concat拼接的示例代码

《pandas实现数据concat拼接的示例代码》pandas.concat用于合并DataFrame或Series,本文主要介绍了pandas实现数据concat拼接的示例代码,具有一定的参考价值,... 目录语法示例:使用pandas.concat合并数据默认的concat:参数axis=0,join=

C#代码实现解析WTGPS和BD数据

《C#代码实现解析WTGPS和BD数据》在现代的导航与定位应用中,准确解析GPS和北斗(BD)等卫星定位数据至关重要,本文将使用C#语言实现解析WTGPS和BD数据,需要的可以了解下... 目录一、代码结构概览1. 核心解析方法2. 位置信息解析3. 经纬度转换方法4. 日期和时间戳解析5. 辅助方法二、L

Python使用Code2flow将代码转化为流程图的操作教程

《Python使用Code2flow将代码转化为流程图的操作教程》Code2flow是一款开源工具,能够将代码自动转换为流程图,该工具对于代码审查、调试和理解大型代码库非常有用,在这篇博客中,我们将深... 目录引言1nVflRA、为什么选择 Code2flow?2、安装 Code2flow3、基本功能演示

基于Python+PyQt5打造一个跨平台Emoji表情管理神器

《基于Python+PyQt5打造一个跨平台Emoji表情管理神器》在当今数字化社交时代,Emoji已成为全球通用的视觉语言,本文主要为大家详细介绍了如何使用Python和PyQt5开发一个功能全面的... 目录概述功能特性1. 全量Emoji集合2. 智能搜索系统3. 高效交互设计4. 现代化UI展示效果

IIS 7.0 及更高版本中的 FTP 状态代码

《IIS7.0及更高版本中的FTP状态代码》本文介绍IIS7.0中的FTP状态代码,方便大家在使用iis中发现ftp的问题... 简介尝试使用 FTP 访问运行 Internet Information Services (IIS) 7.0 或更高版本的服务器上的内容时,IIS 将返回指示响应状态的数字代