Android顶部粘至视图具体解释

不知从某某时间開始,这样的效果開始在UI设计中流行起来了。让我们先来看看效果:


大家在支付宝、美团等非常多App中都有使用。要实现这个效果,我们能够来分析下思路:

我们肯定要用2个一样的布局来显示我们的粘至布局。一个是正常的、还有一个是到顶部不动的。正常的那个,随着scroll一起滚,该滚到哪滚到哪。仅仅是他滚到最上面的时候,

我们须要用粘至的布局,放到顶部。当然。他还在后面继续滚,ok。如今我们来看看详细怎样实现:

先看布局,just a demo。用几张图片略微做做样子。

粘至布局:

<?

xml version="1.0" encoding="UTF-8"?

> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="50dp" android:orientation="horizontal" > <TextView android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:textColor="#FFFFFF" android:background="#232323" android:text="我不会动" android:textSize="30dp" /> </LinearLayout>


主布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:id="@+id/parent_layout"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical" ><com.xys.scrolltrick.TrickScrollandroid:id="@+id/scrollView"android:layout_width="fill_parent"android:layout_height="fill_parent" ><FrameLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content" ><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="vertical" ><ImageViewandroid:id="@+id/iamge"android:layout_width="match_parent"android:layout_height="200dp"android:background="@drawable/ic_launcher"android:scaleType="centerCrop" /><includeandroid:id="@+id/stick"layout="@layout/stick_layout" /><ImageViewandroid:layout_width="match_parent"android:layout_height="200dp"android:background="@drawable/ic_launcher"android:scaleType="centerCrop" /><ImageViewandroid:layout_width="match_parent"android:layout_height="200dp"android:background="@drawable/ic_launcher"android:scaleType="centerCrop" /><ImageViewandroid:layout_width="match_parent"android:layout_height="200dp"android:background="@drawable/ic_launcher"android:scaleType="centerCrop" /></LinearLayout><includeandroid:id="@+id/normal"layout="@layout/stick_layout" /></FrameLayout></com.xys.scrolltrick.TrickScroll></LinearLayout>

加入多个imageview是为了让他能滚起来

因为ScrollView中并没有提供scroll listener,因此我们仅仅能重写下,来创建一个接口:

package com.xys.scrolltrick;import android.content.Context;
import android.util.AttributeSet;
import android.widget.ScrollView;public class TrickScroll extends ScrollView {public interface onScrollListener {public void onScroll(int scrollY);}private onScrollListener onScrollListener;public TrickScroll(Context context, AttributeSet attrs, int defStyleAttr) {super(context, attrs, defStyleAttr);}public TrickScroll(Context context, AttributeSet attrs) {super(context, attrs);}public TrickScroll(Context context) {super(context);}public void setOnScrollListener(onScrollListener onsScrollListener) {this.onScrollListener = onsScrollListener;}@Overrideprotected void onScrollChanged(int l, int t, int oldl, int oldt) {super.onScrollChanged(l, t, oldl, oldt);if (onScrollListener != null) {onScrollListener.onScroll(t);}}
}

我们给他的滑动。提供一个监听接口。

主程序:

package com.xys.scrolltrick;import android.app.Activity;
import android.os.Bundle;
import android.view.ViewTreeObserver.OnGlobalLayoutListener;
import android.widget.LinearLayout;import com.xys.scrolltrick.TrickScroll.onScrollListener;public class MainActivity extends Activity implements onScrollListener {private TrickScroll mScroll;// 正常状态下的布局private LinearLayout mLayout1;// 顶部粘至的布局private LinearLayout mLayout2;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);mScroll = (TrickScroll) findViewById(R.id.scrollView);mLayout1 = (LinearLayout) findViewById(R.id.stick);mLayout2 = (LinearLayout) findViewById(R.id.normal);mScroll.setOnScrollListener(this);// 根布局状态下。监听布局改变findViewById(R.id.parent_layout).getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {@Overridepublic void onGlobalLayout() {onScroll(mScroll.getScrollY());}});}@Overridepublic void onScroll(int scrollY) {// 获取正常布局的位置来又一次设置粘至布局的位置int layoutTop = Math.max(scrollY, mLayout1.getTop());mLayout2.layout(0, layoutTop, mLayout2.getWidth(),layoutTop + mLayout2.getHeight());}
}

当中的核心就在onScroll(int scrollY)这种方法中。我们用max函数来推断当前最大值


这里须要注意2个方法:

1、getTop():该方法返回该view到容器的top像素

2、getScrollY():该方法返回的是。你的scrollview已经滚了多少

-------------------一旦这个世界有了scroll整个世界就不一样了-------------------

知道了这2个方法后。我们就能够推断了,当滚的距离小于getTop()的时候。保持与正常的一样,大于的时候,就须要用getScrollY()了,这个比較难理解,事实上你能够把布局想象成一个纸带,而手机屏幕是一个挖了孔的框,我们在后面从下往上拉纸带,这样就模拟了scroll滑动,这样理解的话,会比較清楚点


以上。


转载于:https://www.cnblogs.com/blfbuaa/p/7077769.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/252446.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

在实际项目开发中keil的调试方法

转载2015-06-14 20:23:04 一.在keilc的调试状态下&#xff0c;如何观察各个片内外设的运行状态&#xff1f;如何修改它们的设置&#xff1f;​ 在调试状态下&#xff0c;点击Peripherals菜单下的不同外设选项命令&#xff0c;就会显示或隐藏对应外设的观察窗口。 在程序运行时&…

slam 常用数据集的帧率

1. kitti数据集的帧率约约为10fps,图像分辨率为1241x376 2. Euroc数据集的帧率约为20fps,图像分辨率为752x480 3.TUM数据集的帧率约为30fps, 图像分辨率为640x360 zed相机获取的HD图像的分辨率为1280x720p,获取的VGA图像分辨率为672x376,mynt相机获取的VGA图像的分辨率为640x…

小李飞刀:用python刷题ing....

叨逼叨 默认每天都要刷两道题。今天目标已完成。 第一题 26. 删除排序数组中的重复项难度&#xff1a;简单类型&#xff1a;数组 给定一个排序数组&#xff0c;你需要在原地删除重复出现的元素&#xff0c;使得每个元素只出现一次&#xff0c;返回移除后数组的新长度。不要使用…

【Log4J】

学习mybatis中用到了Log4J 在此记录下 引入 引入Maven配置 <!-- https://mvnrepository.com/artifact/log4j/log4j --><dependency><groupId>log4j</groupId><artifactId>log4j</artifactId><version>1.2.17</version></de…

VI-ORB环境配置

参考博客:https://blog.csdn.net/qq_38589460/article/details/82559816 https://blog.csdn.net/Robot_Starscream/article/details/90245456 本机安装的是opencv3.0 在Examples/ROS/ORB-VIO以及/VI-ORB/src/LearnVIORB-RT下的CMakeLists.txt都要进行修改 将find_package(O…

.NET Core 3.0中的数据库驱动框架System.Data

虽然没有得到很多关注&#xff0c;但System.Data对于.NET中任何关系型数据库的访问都至关重要。因为其前身是ActiveX Data Objects&#xff0c;所以它也被称为ADO.NET。System.Data提供了一个通用框架&#xff0c;是构建.NET数据库驱动程序的基础。该框架提供了数据库驱动可以遵…

linux vg lv pv

pv由物理卷或者分区组成 pv可以组成一个或者多个vg vg可以分成多个lv 方便扩展 pvs vgs lvs 可以查看当前存在的pv vg lv 我的centos硬盘20g 使用了一段时间 加了100g 这时候 我们可以使用扩展来扩展我们的分区大小 查看自己拥有多少个硬盘 ls /dev/sd* | grep -v [0-9] …

mynt product model: D1000-IR-120标定相机和IMU外参

1. 首先是安装相应的mynt SDK. http://www.myntai.com/mynteye/depth小觅官网,在sdk下拉菜单中点击MYNT EYE Depth SDK,然后选择Linux Installation安装安装步骤说明一步步的安装,安装sample后,测试一下安装是否成功.我的电脑上安装了ROS,所以可以点击上面第一幅图中的ROS Ins…

吉林省第二条国际铁路联运大通道“长珲欧”启动测试

29日&#xff0c;吉林省第二条国际铁路联运大通道“长珲欧”在俄罗斯启动测试。吉林省商务厅供图 29日&#xff0c;吉林省第二条国际铁路联运大通道“长珲欧”在俄罗斯启动测试。吉林省商务厅供图 中新网长春1月29日电 (郭佳)记者29日从吉林省商务厅获悉&#xff0c;该省第二条…

使用Ajax解析数据遇到的问题

数据格式 我最近在使用JQuery的$.ajax访问后台的时候&#xff0c;发现竟然无法解析返回的数据&#xff0c;具体的错误记不清了(以后在遇到问题先截个图)&#xff0c;可以在浏览器的Console中看到一个错误&#xff0c;但是去看这条请求是有数据返回的&#xff0c;所以刚开始我一…

49、剑指offer--把字符串转换成整数

题目描述将一个字符串转换成一个整数&#xff0c;要求不能使用字符串转换整数的库函数。 数值为0或者字符串不是一个合法的数值则返回0 输入描述:输入一个字符串,包括数字字母符号,可以为空输出描述:如果是合法的数值表达则返回该数字&#xff0c;否则返回0输入例子:2147483647…

Git丢弃修改

Git是如何跟踪修改的&#xff1f;我们之前修改文件后都用到了两个命令git add <file>、git commit&#xff0c;其实在Git中&#xff0c;每次修改后&#xff0c;如果不add到暂存区&#xff0c;那就不会加入到commit。 查看一下文件内容&#xff1a; 在其中添加一行记录…

隐藏界面没有必要应用场景

转载于:https://www.cnblogs.com/zengsf/p/8366572.html

mynt product model: D1000-IR-120标定相机和IMU外参之二

1. 在之一中使用kalibr标定mynt相机和内置imu的外参数,使用的是720p,30fps的双目图像和200hz的imu数据,标定结果误差比较大,这一次我们改用480p,60hz的双目图像和200hz的imu数据进行标定,需要在mynt_sdk中的mynteye.launch中进行如下修改. 默认获取图像的2560x720,30fps&#…

AODp

一、AOP是OOP的延续&#xff0c;是&#xff08;Aspect Oriented Programming&#xff09;的缩写&#xff0c;意思是面向切面编程。 AOP&#xff08;Aspect Orient Programming&#xff09;&#xff0c;作为面向对象编程的一种补充&#xff0c;广泛应用于处理一些具有横切性质的…

[洛谷P4174][NOI2006]最大获利

题目大意&#xff1a;同Petya and Graph&#xff0c;数据范围改成$n\leqslant5\times10^3,m\leqslant5\times10^4$ 题解&#xff1a;同上 卡点&#xff1a;无 C Code&#xff1a; #include <algorithm> #include <cstdio> #define maxn 5010 #define maxm 50010 co…

使用ros发布UVC相机和串口IMU数据

&#xff11;&#xff0e;目的&#xff1a;为了可以标定普通USB相机和固定在相机上的外置IMU的外参,我希望通过ROS获取更高分辨率和更高频率的图像数据,并且可以将图像和imu的topic发布出来,直接使用rosbag record录制话题数据,写入bag文件,这样获得的bag文件直接可以用于相机和…

API自动化测试利器——Postman

自从开始做API开发之后&#xff0c;我就在寻找合适的API测试工具。一开始不是很想用Chrome扩展&#xff0c;用的WizTools的工具&#xff0c;后来试过一次Postman之后就停不下来了&#xff0c;还买了付费的Jetpacks。推出Team Sync Beta之后我又把这个工具推广给团队&#xff0c…

gcc,cc,g++,CC的区别

***gcc是C编译器&#xff1b; ***g是C编译器&#xff1b; ***linux下cc一般是一个符号连接&#xff0c;指向gcc ***gcc说明 1.gcc编译常用格式&#xff1a; gcc C源文件 -o 目标文件 或 gcc -o 目标文件 C源文件 或 gcc C源文件 最后一种情况产生的目标文件默认为a.out 2.gcc…

云原生实践之 RSocket 从入门到落地:Servlet vs RSocket

技术实践的作用在于&#xff1a;除了用于构建业务&#xff0c;也是为了验证某项技术或框架是否值得大规模推广。 本期开始&#xff0c;我们推出《RSocket 从入门到落地》系列文章&#xff0c;通过实例和对比来介绍RSocket。主要围绕RSocket如何实现Polyglot RPC、Service Regis…