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>
![](https://www.aspxhome.com/images/zang.png)
![](https://www.aspxhome.com/images/jiucuo.png)
猜你喜欢
- Java在1.5开始引入了注解,目前流行的框架都在用注解,可想而知注解的强大之处。以下通过自定义注解来深入了解java注解。一、创建自定义注
- 一、队列简介队列是一个有序列表,遵循“先入先出”的原则,即先存入队列的数据要先取出,后存入的数据后取出。队列有两种存储表示,顺序表示和链式表
- 奇怪的不等于(≠)最近,栈长用 IntelliJ IDEA 看源码时发现:咦~这是什么鬼?Java 不等于的写法不是一直都是 != 么?什么
- Java2在1.4中新增了一个关键字:assert。在程序开发过程中使用它创建一个断言(assertion)。,它的语法形式有如下所示的两种
- 引言在unity中可以将不同场景的背景和道具放置在不同的Scene当中,通过对Scene的加载和卸载来实现场景之间的切换。同时创建一个基础场
- ealsticsearch只是后端提供各种api,那么怎么直观的使用它呢?elasticsearch-head将是一款专门针对于elasti
- 后台管理页面往往需要登录才可以进行操作,这时就需要Seession来记录登录状态要实现起来也是非常简单,只需要自定义一个HandlerInt
- 本文实例讲述了Java简单工厂模式定义与用法。分享给大家供大家参考,具体如下:一 代码// 抽象产品(Abstract Product)角色
- 本文以实例形式讲述了C#通过反射创建自定义泛型的实现方法,分享给大家供大家参考。具体如下:比如有这样一个泛型:Demo.GenericsSi
- 方法一:1.在pom.xml文件下添加依赖包<dependency><groupId>com.alibaba<
- 本文实例讲述了Android控件之CheckBox、RadioButton用法。分享给大家供大家参考。具体如下:CheckBox和Radio
- 使用Java实现图像分割,供大家参考,具体内容如下为减少动画制作过程中的IO操作,我们可以使用连续动画来改善动画播放效率。假如我们有如下的一
- 本文实例为大家分享了Java实现扑克牌程序的具体代码,供大家参考,具体内容如下思路:在实现之前,先要想好步骤,思路清晰才不会出错。要实现一副
- 简介本文用示例介绍java的Duration的用法。Duration和Period说明Duration类通过秒和纳秒相结合来描述一个时间量,
- 本文实例讲述了Java调用Shell命令的方法。分享给大家供大家参考。具体如下:近日项目中有这样一个需求:系统中的外币资金调度完成以后,要将
- Spring 封装了 RedisTemplate 来操作 Redis,它支持所有的 Redis 原生的 API。在 Re
- instanceof判断某个对象是否是某个类的实例或者某个类的子类的实例。它的判断方式大概是这样的:public<T> bool
- 本文实例讲述了C#编程实现获取文件夹中所有文件的文件名。分享给大家供大家参考,具体如下:想实现这样一个功能:批量修改一个目录所有jpg文件的
- 使用ByteArrayOutputStream下载文件//文件名称String filepath = ServletActionContex
- 1. 简单工厂介绍简单工厂有一个具体的工厂类,可以生产不同的产品,属于创建型设计模式。注意:简单工厂模式 不属于23种设计模式之列2. 简单