Android中使用ScrollView指定view的顶部悬停效果
作者:Highwinzgs 发布时间:2021-10-02 13:32:38
因项目中的需要实现ScrollView顶部的悬停,也不是太难便自己实现功能,话不多说,先上效果图
红色text一到顶上便会悬浮在上面,不会跟随scrollview的滑动而上滑。
原理:
原理其实很简单就是对view的gone和visible,写两个相同的要置顶的view,一个设置为gone,一个为visible,当可见的view超出屏幕范围的时候,将不可以的view设置为visible,不可见的view 与scrollview要同级,这样滑动的时候不会影响到view的位置。
直接上代码
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.lanmai.ObservableScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/scrollview"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<!-- 中间就是填充的view就不写了-->
<!--指定要置顶的view-->
<TextView
android:id="@+id/specific_text_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/holo_red_dark"
android:gravity="center"
android:text="text"
android:textSize="40sp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="200dp"
android:background="@android:color/darker_gray"
android:gravity="center"
android:text="text"
android:textSize="40sp"/>
</LinearLayout>
</RelativeLayout>
</com.lanmai.ObservableScrollView>
<!--指定要置顶的相同的view visibility设置为gone -->
<TextView
android:id="@+id/specific_text_view_gone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/holo_red_dark"
android:gravity="center"
android:text="text"
android:textSize="40sp"
android:visibility="gone"/>
</RelativeLayout>
接下来要重写scrollview,为什么要重写ScrollView,scrollview的滑动监听事件setOnScrollChangeListener 这个方法是在6.0以上才能用的。为了考虑低版本的的需求,要重写ScrollView把接口开放出来。
重写ScrollView
public class ObservableScrollView extends ScrollView {
private ScrollViewListener scrollViewListener = null;
public ObservableScrollView(Context context) {
super(context);
}
public ObservableScrollView(Context context, AttributeSet attrs,
int defStyle) {
super(context, attrs, defStyle);
}
public ObservableScrollView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public void setScrollViewListener(ScrollViewListener scrollViewListener) {
this.scrollViewListener = scrollViewListener;
}
@Override
protected void onScrollChanged(int x, int y, int oldx, int oldy) {
super.onScrollChanged(x, y, oldx, oldy);
if (scrollViewListener != null) {
scrollViewListener.onScrollChanged(this, x, y, oldx, oldy);
}
}
public interface ScrollViewListener {
void onScrollChanged(ScrollView scrollView, int x, int y, int oldx, int oldy);
}
}
我把重写的ScrollView命名为ObservableScrollView,重写三个构造方法,都是换汤不换药的作法,这里就不赘述。 最重要的是重写onScrollChanged这个方法,如何把滑动监听事件开放出去呢,其实也就是写一个监听回调,参数和onScrollChanged里面的的参数一样就可以了,当然主要不是用到这些参数,只是为了判断ScrollView的滑动事件,参数对于这个功并不是很重要。那这样,一个简单的自定义就写好了scrollview
如何去用?
用法也是挺简单的,直接上代码
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_scroll_view);
mTextView = ((TextView) findViewById(R.id.specific_text_view));
mScrollView = ((ObservableScrollView) findViewById(R.id.scrollview));
mVisibleTextView = ((TextView) findViewById(R.id.specific_text_view_gone));
mTextView.setOnClickListener(this);
mScrollView.setScrollViewListener(this);
}
这里onCreate方法里面的,也简单,拿到view 并且设置监听事件,当然,这里多实现了一个点击view置顶的功能,监听设置好以后,实现相应的接,接下来就是重头戏了
@Override
public void onScrollChanged(ScrollView scrollView, int x, int y, int oldx, int oldy) {
int[] location = new int[2];
mTextView.getLocationOnScreen(location);
int xPosition = location[0];
int yPosition = location[1];
Log.d("ScrollViewActivity", "yPosition:" + yPosition);
int statusBarHeight = getStatusBarHeight();
Log.d("ScrollViewActivity", "statusBarHeight:" + statusBarHeight);
if (yPosition <= statusBarHeight) {
mVisibleTextView.setVisibility(View.VISIBLE);
} else {
mVisibleTextView.setVisibility(View.GONE);
}
}
onScrollChanged这个方法就是自己写的监听回调,里面的参数就是Scrollview滑动的时候回调出来的,里面的参数并不用去关心
int[] location = new int[2];
mTextView.getLocationOnScreen(location);
int xPosition = location[0];
int yPosition = location[1];
/* mTextView就是要悬浮的view,getLocationOnScreen(location)这个方法就是拿到view在屏幕中的位置 ,传入一个数组,最后得到的yPosition就是view在屏幕中的高度,这里面调用了native层的实现方式,所以数组能直接附上值*/
// 值得注意的是,拿到的这个高度还包括状态栏的高度。只要减掉就可以了,状态栏的高度获取获取附上代码:
public int getStatusBarHeight() {
int result = 0;
int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
if (resourceId > 0) {
result = getResources().getDimensionPixelSize(resourceId);
}
return result;
}
int statusBarHeight = getStatusBarHeight();
Log.d("ScrollViewActivity", "statusBarHeight:" + statusBarHeight);
通过获取到的状态栏高度,如果小于状态栏的高度就表示已经滑出屏幕了,将要置顶的view设置为visibvle否则设置为gone
if (yPosition <= statusBarHeight) {
mVisibleTextView.setVisibility(View.VISIBLE);
} else {
mVisibleTextView.setVisibility(View.GONE);
}
这样scrollview的悬浮置顶的功能就实现了,这里我也给出点击view置顶的代码
@Override
public void onClick(View v) {
int[] location = new int[2];
v.getLocationOnScreen(location);
int x = location[0];
int y = location[1];
mScrollView.scrollBy(0, location[1] - getStatusBarHeight());
}
当然要缓慢的滑动过程用smoothScrollBy替代就可以了
结论:
实现这种效果,找对了思路就可以很容易的写出来了,这是一种比较简单的实现方式了,源码我就不贴出来了,基本已经都在了。
以上所述是小编给大家介绍的Android中使用ScrollView指定view的悬停效果,希望对大家有所帮助。。。
来源:http://blog.csdn.net/highwinzgs/article/details/69834309


猜你喜欢
- 前言二进制文件读写两个重要的函数 , fread 和 fwrite , fread 用于读取文件 , fwrite 用于写出文件 ;frea
- 一、AOP概述AOP,即面向切面编程,简单来说就是将代码中重复的部分抽取出来,在需要执行的时候使用 * 的技术,在不修改源码的基础上对方法
- 在做asp.net和unity进行http通信的时候,当unity客户端发出表单请求的时候,我要将他要请求的数据以json的格式返回给客户端
- 我先说说这两种的方式的不同之处吧 第一种: 在调动成功之后 不会让你指纹解锁 而是调转到当初你设置指纹解锁时的 手势解锁页面 第二种: 在调
- 本文实例讲述了C#采用OpenXml给Word文档添加表格的方法,是非常实用的操作技巧。分享给大家供大家参考。具体分析如下:这里将展示如何使
- AndroidStudio 实现加载字体资源的方法在android中字体的格式总是不能尽善尽美的显示出来 , 于是要求我们使用一
- 1.内容中含有xml预定好的实体,如“<”和“&”,对xml来说是禁止使用的,针对这种字符,解决方式是使用CDATA部件以&q
- 1、通过C#调用Java的方法:在C#中添加调用的一些代码,利用Unity提供的一些接口实现调用Java!private const str
- 1.打开官网稍微学习一下,了解一下spring cloud是个什么东西,大概有哪些组件等https://spring.io/projects
- 自动配置底层源码分析本次springboot源码来自2.6.6版本。@EnableAutoConfiguration源码解析在springb
- 本文实例为大家分享了Android仿IOS UIAlertView对话框的具体代码,供大家参考,具体内容如下显示效果:我在参考链接中看到了作
- 最近有很多同学,竟然不知道如何使用Intellij IDEA打开Java项目并启动现在来讲一下,希望不要忘记了 1、打开IDEA开机页面 M
- 实例如下所示:public class MainActivity {private static final String fileName
- 本文实例讲述了Android编程实现ListView滚动提示等待框功能。分享给大家供大家参考,具体如下:其实原理很简单,只需要设置监听lis
- 之前在介绍使用JdbcTemplate和Spring-data-jpa时,都使用了单数据源。在单数据源的情况下,Spring Boot的配置
- 一、使用maven加载依赖加载了连接数据库的依赖、mybatis的依赖以及lombok的依赖<dependency>  
- 1.通过用FTP进行上传文件,首先要实现建立FTP连接,一般建立FTP连接,需要知道FTP配置有关的信息。一般要在Bean中建立一个Serv
- package com.smart.frame.task.autoTask;import java.util.Collection;impo
- 路由做Android/iOS原生开发的时候,要打开一个新的页面,你得知道你的目标页面对象,然后初始化一个Intent或者ViewContro
- 一、创建项目创建一个简单的Java项目,其中Main.java为主函数,包含main方法:二、完成JAR配置进入File->Proje