Android滑动组件悬浮固定在顶部效果
作者:ganshenml 发布时间:2022-12-13 19:24:32
标签:Android,滑动组件,悬浮
本文实例为大家分享了Android滑动组件悬浮固定在顶部效果的具体代码,供大家参考,具体内容如下
要想实现的效果是如下:
场景:有些时候是内容中间的组件当滑动至顶部的时候固定显示在顶部。
实现的思路:
1.目标组件(button)有两套,放在顶部和内容中间;
2.当内容中间的组件滑动至顶部栏位置时控制显示/隐藏顶部和中间的组件(涉及到组件获取在屏幕的位置知识点);
activity代码:
public class MainActivity extends AppCompatActivity implements ObservableScrollView.ScrollViewListener {
private ObservableScrollView scrollView;
private Button topBtn1, topBtn2, middleBtn1, middleBtn2;
private View topPanel, middlePanel;
private int topHeight;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initViews();
initListeners();
}
@Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
Rect frame = new Rect();
getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
int statusBarHeight = frame.top;//状态栏高度
int titleBarHeight = getWindow().findViewById(Window.ID_ANDROID_CONTENT).getTop();//标题栏高度
topHeight = titleBarHeight + statusBarHeight;
}
private void initViews() {
scrollView = (ObservableScrollView) findViewById(R.id.scrollView);
topPanel = findViewById(R.id.topPanel);
topBtn1 = (Button) topPanel.findViewById(R.id.button1);
topBtn2 = (Button) topPanel.findViewById(R.id.button2);
middlePanel = findViewById(R.id.middlePanel);
middleBtn1 = (Button) middlePanel.findViewById(R.id.button1);
middleBtn2 = (Button) middlePanel.findViewById(R.id.button2);
}
private void initListeners() {
topBtn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
middleBtn1.setBackgroundColor(Color.WHITE);
topBtn1.setBackgroundColor(Color.WHITE);
}
});
middleBtn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
middleBtn1.setBackgroundColor(Color.BLUE);
topBtn1.setBackgroundColor(Color.BLUE);
}
});
scrollView.setScrollViewListener(this);
}
@Override
public void onScrollChanged(ObservableScrollView scrollView, int x, int y, int oldx, int oldy) {
int[] location = new int[2];
middleBtn1.getLocationOnScreen(location);
int locationY = location[1];
Log.e("locationY", locationY + " " + "topHeight的值是:" + topHeight);
if (locationY <= topHeight && (topPanel.getVisibility() == View.GONE || topPanel.getVisibility() == View.INVISIBLE)) {
topPanel.setVisibility(View.VISIBLE);
}
if (locationY > topHeight && topPanel.getVisibility() == View.VISIBLE) {
topPanel.setVisibility(View.GONE);
}
}
}
要点解析:
1.在onWindowFocusChanged()方法中获取屏幕状态栏和标题栏的高度(在onCreate()方法中是获取是0);
2.因为布局中的ScrollView的onScrollChangeListener()方法低版本API不支持——>所以activity实现了自定义ScrollView中的onScrollChanged()接口方法——>在此方法中实现组件的显示/隐藏;
自定义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(ObservableScrollView scrollView, int x, int y, int oldx, int oldy);
}
}
然后是布局文件:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.administrator.slideholdapp.MainActivity">
<com.example.administrator.slideholdapp.ObservableScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/scrollView">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="30dp"
android:text="@string/content" />
<include android:id="@+id/middlePanel" layout="@layout/middle_item_layout"></include>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:text="@string/content" />
</LinearLayout>
</com.example.administrator.slideholdapp.ObservableScrollView>
<include android:id="@+id/topPanel" layout="@layout/middle_item_layout" android:visibility="gone"/>
</FrameLayout>


猜你喜欢
- 前言由于多核系统普遍存在,并发性编程的应用无疑比以往任何时候都要广泛。但并发性很难正确实现,用户需要借助新工具来使用它。很多基于 JVM 的
- 一、创建 Spring 项目接下来使用 Maven 来创建⼀个 Spring 项⽬,创建 Spring 项目和 Servlet 类似,总共分
- 逆转交替合并两个链表,即从一个链表的尾指针指向另一个链表的尾指针,依次逆转交替进行合并。下面就通过实例来详细的介绍该逆转交替合并两个链表的思
- 本文实例讲述了Android编程绘图操作之弧形绘制方法。分享给大家供大家参考,具体如下:/** * 绘制弧形图案 * @descriptio
- 本文实例讲述了Java基于动态规划法实现求最长公共子序列及最长公共子字符串。分享给大家供大家参考,具体如下:动态规划法经常会遇到复杂问题不能
- 这篇文章主要介绍了Java方法参数传递机制原理解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可
- 首先给一个简单的Builder设计模式的例子:主实现类代码如下:/** * 实体类 包含一个静态内部类 Builder */public c
- 本文实例讲述了C#判断一个矩阵是否为对称矩阵及反称矩阵的方法。分享给大家供大家参考。具体如下:1.判断对称矩阵对任意i和j,有a[i,j]=
- 作为.NET进阶内容的一部分,垃圾回收器(简称GC)是必须了解的内容。本着“通俗易懂”的原则,本文将解释CLR中垃圾回收器的工作原理。基础知
- Spring MVC Controller控制器,是MVC中的部分C,为什么是部分呢?因为此处的控制器主要负责功能处理部分:收集、验证请求参
- MyBatis 是支持普通 SQL查询,存储过程和高级映射的优秀持久层框架。MyBatis 消除了几乎所有的JDBC代码和参数的手工设置以及
- Java异常简介Java异常是Java提供的一种识别及响应错误的一致性机制。Java异常机制可以使程序中异常处理代码和正常业务代码分离,保证
- 一、BIO、NIO、AIO学习Netty需要了解BIO、NIO、AIO,具体可参考Java网络编程IO模型 — BIO、
- 前言最常用的对字符串操作的类有三个,分别是String,StringBuilder,StringBuffer,下面将会详细的说说这三个类..
- 前言本文告诉大家一些 ValueTuple 的原理,避免在使用出现和期望不相同的值。ValueTuple 是 C# 7 的语法糖,如果使用的
- 一.NET Remoting 介绍简介.NET Remoting与MSMQ不同,它不支持离线可得,另外只适合.NET平台的程序进行通信。它提
- 本文实例为大家分享了Java实现邮件找回密码功能的具体代码,供大家参考,具体内容如下1、有个需求就是,忘记密码后通过邮箱找回。现在的系统在注
- 错误处理到目前为止,我们都没怎么介绍onComplete()和onError()函数。这两个函数用来通知订阅者,被观察的对象将停止发送数据以
- @Autowired注解在抽象类中失效最近在工作中遇到这个问题,在抽象类中使用Autowired这个注解,注入mybatis的dao时,总是
- java "equals"和"==”异同首先简单说一下“equal”和“==”==操作对于基本数据类型比较的是