Android滑动组件悬浮固定在顶部效果

 更新时间:2020年04月08日 16:29:08   作者:ganshenml  
这篇文章主要为大家详细介绍了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> 

更多关于滑动功能的文章,请点击专题: 《Android滑动功能》

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • Android string-array数据源简单使用

    Android string-array数据源简单使用

    这篇文章主要介绍了Android string-array数据源简单使用的相关资料,需要的朋友可以参考下
    2016-09-09
  • Bootstrap 下拉菜单.dropdown的具体使用方法

    Bootstrap 下拉菜单.dropdown的具体使用方法

    这篇文章主要介绍了Bootstrap 下拉菜单.dropdown的具体使用方法,详细讲解下拉菜单的交互,有兴趣的可以了解一下
    2017-10-10
  • android getActivity.findViewById获取ListView 返回NULL的方法

    android getActivity.findViewById获取ListView 返回NULL的方法

    下面小编就为大家带来一篇android getActivity.findViewById获取ListView 返回NULL的方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2016-11-11
  • Android Flutter使用本地数据库编写备忘录应用

    Android Flutter使用本地数据库编写备忘录应用

    这篇文章主要为大家详细介绍了Android Flutter如何使用本地数据库实现编写简单的备忘录应用,文中的示例代码讲解详细,感兴趣的小伙伴可以了解一下
    2023-03-03
  • Android开发之TabHost选项卡及相关疑难解决方法

    Android开发之TabHost选项卡及相关疑难解决方法

    这篇文章主要介绍了Android开发之TabHost选项卡及相关疑难解决方法,结合实例形式较为详细的分析了Android开发中TabHost选项卡的常见用法以及相关疑难问题解决方法,需要的朋友可以参考下
    2019-03-03
  • 深入理解Android中View绘制的三大流程

    深入理解Android中View绘制的三大流程

    这篇文章主要给大家介绍了关于Android中View绘制的三大流程,View的工作流程主要是指measure、layout、draw这三大流程,即测量、布局和绘制,文中通过示例代码介绍的非常详细,对大家具有一定的参考学习价值,需要的朋友们下面来一起看看吧。
    2017-07-07
  • Android实现倒计时CountDownTimer使用详解

    Android实现倒计时CountDownTimer使用详解

    这篇文章主要为大家详细介绍了Android实现倒计时CountDownTimer的使用方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-12-12
  • Android获得设备状态信息、Mac地址、IP地址的方法

    Android获得设备状态信息、Mac地址、IP地址的方法

    今天小编就为大家分享一篇关于Android获得设备状态信息、Mac地址、IP地址的方法,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
    2018-12-12
  • 利用Kotlin实现破解Android版的微信小游戏--跳一跳

    利用Kotlin实现破解Android版的微信小游戏--跳一跳

    这篇文章主要给大家介绍了关于利用Kotlin实现破解Android版微信小游戏--跳一跳的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧。
    2017-12-12
  • Android fragment实现多个页面切换效果

    Android fragment实现多个页面切换效果

    这篇文章主要为大家详细介绍了fragment实现多个页面切换效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-04-04

最新评论