先看看官网对于AppBarLayout.OnOffsetChangedListener的解释:

Interface definition for a callback to be invoked when an AppBarLayout's vertical offset changes.

当AppBarLayout垂直方向上的偏移量发生改变时,为触发一个回调方法定义的接口。

触发的回调方法是:

onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset)

Called when the AppBarLayout's layout offset has been changed. This allows child views to implement custom behavior based on the offset (for instance pinning a view at a certain y value).

在AppBarLayout的布局偏移量发生改变时被调用。这个方法允许子view根据偏移量实现自定义的行为(比如在特定Y值的时候固定住一个View)。

解释的非常清楚了,不过我们需要点例子才好理解。

就以stackoverflow上的一个问题为例吧。

问题描述

当我把CollapsingToolbarLayout,CollapsingToolbarLayout以及SwipeRefreshLayout用在一起的时候:

xmlns:android="http://schemas.android.com/apk/res/android"

xmlns:app="http://schemas.android.com/apk/res-auto"

android:id="@+id/drawer_layout"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:fitsSystemWindows="true">

android:id="@+id/coordinator_layout"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:fitsSystemWindows="true">

android:id="@+id/appbar"

android:layout_width="match_parent"

android:layout_height="@dimen/collapse_toolbar_height"

android:fitsSystemWindows="true"

android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

android:id="@+id/collapsing_toolbar"

android:layout_width="match_parent"

android:layout_height="match_parent"

app:contentScrim="?attr/colorPrimary"

android:fitsSystemWindows="true"

app:expandedTitleMarginStart="48dp"

app:expandedTitleMarginEnd="64dp"

app:layout_scrollFlags="scroll|exitUntilCollapsed">

android:id="@+id/toolbar_image"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:scaleType="centerCrop"

android:fitsSystemWindows="true"

app:layout_collapseMode="parallax" />

layout="@layout/activity_main_toolbar"/>

android:id="@+id/swipe_container"

android:layout_width="match_parent"

android:layout_height="match_parent"

app:layout_behavior="@string/appbar_scrolling_view_behavior">

android:id="@+id/mainList"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:scrollbars="vertical" />

android:id="@+id/floating_action_button"

android:layout_height="wrap_content"

android:layout_width="wrap_content"

app:layout_anchor="@id/appbar"

app:layout_anchorGravity="bottom|right|end"

android:layout_margin="16dp"

app:fabSize="mini"

android:src="@drawable/ic_action_edit"

android:onClick="assignSelectedWebCamsToCategory"/>

android:id="@+id/floating_action_menu"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:layout_gravity="bottom|end"

android:paddingRight="10dp"

android:paddingBottom="8dp"

android:paddingLeft="10dp"

fab:menu_shadowColor="#37000000"

fab:menu_colorNormal="#DA4336"

fab:menu_colorPressed="#E75043"

fab:menu_colorRipple="#99FFFFFF"

fab:menu_icon="@drawable/fab_add"

fab:menu_buttonSpacing="10dp"

fab:menu_labels_textColor="@color/very_dark_grey"

fab:menu_labels_textSize="14sp"

fab:menu_labels_colorNormal="@color/white"

fab:menu_labels_colorPressed="@color/next_grey"

fab:menu_labels_colorRipple="#99FFFFFF"

fab:menu_labels_margin="8dp"

fab:menu_backgroundColor="@color/black_transparent">

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:src="@drawable/ic_action_content_import"

fab:fab_size="mini"

fab:fab_label="@string/pref_import_from_server"

fab:fab_colorNormal="@color/white"

app:fab_colorPressed="@color/next_grey"

app:fab_colorRipple="#99FFFFFF"

android:onClick="showSelectionDialog"/>

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:src="@drawable/ic_action_content_manually"

fab:fab_size="mini"

fab:fab_label="@string/create_manually"

fab:fab_colorNormal="@color/white"

app:fab_colorPressed="@color/next_grey"

app:fab_colorRipple="#99FFFFFF"

android:onClick="showAddDialog"/>

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:src="@drawable/ic_action_content_suggestion"

fab:fab_size="mini"

fab:fab_label="@string/submit_suggestion"

fab:fab_colorNormal="@color/white"

app:fab_colorPressed="@color/next_grey"

app:fab_colorRipple="#99FFFFFF"

android:onClick="showSuggestionDialog"/>

layout="@layout/activity_main_drawer"/>

java代码:swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipe_container);

swipeRefreshLayout.setOnRefreshListener(this);

如何才能让滑动刷新只发生在collapsing toolbar完全展开并且 scrollview (或者recyclerview)在顶部 的时候呢?类似于g+和Inbox中的效果。

错误的效果:

正确的效果:

回答:

可以尝试为AppBarLayout添加 offset change listener同时根据情况启用或者关闭滑动刷新,需要增加的代码在这里:

关键改动:public class MainActivity extends AppCompatActivity implements AppBarLayout.OnOffsetChangedListener {

...

private AppBarLayout appBarLayout;

private SwipeRefreshLayout mSwipeRefreshLayout;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

...

mSwipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.contentView);

appBarLayout = (AppBarLayout) findViewById(R.id.appBarLayout);

}

@Override

public void onOffsetChanged(AppBarLayout appBarLayout, int i) {

if (i == 0) {

mSwipeRefreshLayout.setEnabled(true);

} else {

mSwipeRefreshLayout.setEnabled(false);

}

}

@Override

protected void onResume() {

super.onResume();

appBarLayout.addOnOffsetChangedListener(this);

}

@Override

protected void onPause() {

super.onPause();

appBarLayout.removeOnOffsetChangedListener(this);

}}

android framelayout触屏监听,AppBarLayout.OnOffsetChangedListener的使用相关推荐

  1. Android系统截屏监听工具

    Android系统截屏监听工具 做系统截屏通知的时候,找了很多资料,测试后发现要做挺多机型适配的,而且有时候会没有监听到,有时候又监听到多次,不能实现想要的效果.一下是最终的解决方案,已在足够多的手机 ...

  2. Android 屏幕灭屏亮屏广播,屏幕灭屏亮屏监听,广播实现按键监听

    service 类注册广播进行监听 /*** 作者:created by meixi* 邮箱:13164716840@163.com* 日期:2018/9/27 09*/ public class S ...

  3. Android App中监听系统截屏(截屏监听功能)

    功能需求: App内截屏监控功能,当发现用户在我们的app内进行了截屏操作时,进行对图片的二次操作,例如添加二维码,公司logo等一系列操作. 首先来app界面图及截屏监听图添加效果图 主要是利用内容 ...

  4. 仿网易云音乐日推界面(监听AppBarLayout滑动+动态高斯模糊)

    首先来看下日推界面的效果: 网易云音乐日推界面的亮点就是在上拉的时候,banner页面逐渐模糊.字体透明度下降,最后左上角显示出"每日推荐"的字体. 这个界面用户会觉得很清晰.便捷 ...

  5. Android手机屏幕变化监听

    监听手机屏幕的打开关闭   工具类,可直接使用 package com.utils; import android.content.BroadcastReceiver; import android. ...

  6. Android通过使用系统广播监听网络状态的改变

    Android通过使用广播监听网络状态的改变 得先添加权限 //查看网络状态<uses-permission android:name="android.permission.ACCE ...

  7. Android学习按键事件监听与Command模式

    Android学习按键事件监听与Command模式 - Dufresne - 博客园 Android学习按键事件监听与Command模式 一 Command模式 意图: 将一个请求封装为一个对象,从而 ...

  8. bugku 管理员系统 后台代码_不会吧,这也行?iOS后台锁屏监听摇一摇

    [toc] 背景介绍 一般情况下,出于省电.权限.合理性等因素考虑,给人的感觉是很多奇怪的需求安卓可以实现,但是iOS就无法实现!今天要介绍的需求也有这种感觉,就是"当 APP 处于后台或锁 ...

  9. android列表项点击事件,Android 开发 tips(2):监听 Listview 列表项点击事件

    Android 开发 tips(2):监听 Listview 列表项点击事件 (这篇和上篇本来是应该一起写的,但是太过冗长,附链接:[SimpleAdapter 在 Listview 中的应用] ht ...

最新文章

  1. 荒谬而可信的Docker数据中心商业案例
  2. javascript之function1
  3. python小整数池与大整数池
  4. 第6章 Selenium2-Java 自动化测试模型
  5. mysql 数据库编程_MySQL数据库编程(C++语言)
  6. nexus-3本地下载jar的settipng.xml配置
  7. spring map使用annotation泛型注入问题分析
  8. was 程序jvm_【保家护行航】WAS知识学习分享
  9. git为私有仓库设置密码_真香!在局域网下行云流水般使用git
  10. git.exe push --progress origin master:master To https://192.168.3.145:10102/r/~lifan/tms-vue.git !
  11. 标准盒子模型和IE盒子模型的区别
  12. C语言 基于循环结构的程序设计(PTA)
  13. 【影像组学】理论学习——特征类型
  14. 工业应用中基于三维模型的6D目标检测综述
  15. 成人脑力训练 3.951
  16. 微信小程序商城优势在什么地方
  17. PTA L1-039 古风排版
  18. SolidWorks toolbox齿轮再修改方法
  19. word/wps分页
  20. WAVE绘制频谱图(三)——PCM数据处理以及图谱显示

热门文章

  1. mac真的那么好用吗?
  2. 5.pox.xml文件
  3. AG阅读总结10.4.1——参数文件简介
  4. 数字化转型 — 新能源汽车 — 生产制造流程 — 车身车间
  5. javascript实现对图片的随意拖拽,放大缩小
  6. Mac密码该如何更改,Mac密码更改教程
  7. 捕食搜索算法(PS)-求解TSP问题
  8. NOIP2016模拟 JackMa 贪心
  9. 真机联调总是弹窗提示macOS想要使用系统钥匙串
  10. 魅族创始人黄章:一个木匠造手机的传奇