1、创建  ScrollListener 接口监听滑动距离

public interface ScrollListener {void onScrollChanged(ScrollListenerView scrollView, int x, int y, int oldX, int oldY);
}

view阴影属性

android:elevation="4dp"

2、重写 ScrollView  自定义  ScrollListenerView

public class ScrollListenerView extends ScrollView {private ScrollListener scrollViewListener = null;public ScrollListenerView(Context context) {super(context);}public ScrollListenerView(Context context, AttributeSet attrs, int defStyle) {super(context, attrs, defStyle);}public ScrollListenerView(Context context, AttributeSet attrs) {super(context, attrs);}public void setScrollListener(ScrollListener scrollViewListener) {this.scrollViewListener = scrollViewListener;}@Overrideprotected 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);}}
}

判断scrollView是否滚动到底部方法

@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);}if (getScrollY() + getHeight() >= computeVerticalScrollRange()) {Log.d("lgq", "------滚动到最下方------");} else {Log.d("Lgq", "没有到最下方");}
}

3、在layout xml文件使用ScrollListenerView

<com.tianxinyw.mapclient.views.ScrollListenerViewandroid:id="@+id/slv"android:layout_width="match_parent"android:layout_height="match_parent"android:scrollbars="none"><RelativeLayoutandroid:layout_width="match_parent"android:layout_height="match_parent">
</RelativeLayout></com.tianxinyw.mapclient.views.ScrollListenerView>

上下层标题渐变色布局,titlelitwo是下层

<?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"android:id="@+id/activity_popup"android:orientation="vertical"><LinearLayoutandroid:id="@+id/titlelitwo"android:layout_width="match_parent"android:layout_height="38dp"android:background="@color/homeiconokc"android:gravity="center"android:orientation="horizontal"><TextViewandroid:id="@+id/titletetwo"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="天鑫计费2"android:textColor="@color/white"android:textSize="16dp"android:textStyle="bold"/></LinearLayout><LinearLayoutandroid:id="@+id/lishititleli"android:layout_width="match_parent"android:layout_height="38dp"android:background="@color/homeiconokc"android:gravity="center"android:orientation="horizontal"><TextViewandroid:id="@+id/lishititlete"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="天鑫计费"android:textColor="@color/white"android:textSize="16dp"android:textStyle="bold"/></LinearLayout>

4、Activity 实现滑动距离监听接口

public class HomeFragment extends BaseFragment implements ScrollListener{

初始化

@BindView(R.id.slv)
ScrollListenerView slv;

设置监听

slv.setScrollListener(this);

实现方法:

@Override
public void onScrollChanged(ScrollListenerView scrollView, int x, int y, int oldX, int oldY) {if (y - oldY > 0&&y>0) {float f = (y + 0f) / 400;//滑动距离350pxif (f > 1) {f = 1f;}if (f < 0) {f = 0;}if (f > 0 && f < 0.3) {f = (float) 0.333;}Log.i("lgq", "......向上滑。。。" + f + ".......y====" + y);lishititleli.setBackgroundColor(Utils.changeAlpha(ContextCompat.getColor(getActivity(), R.color.homeiconokc), (int) (1 - f * 1 * 0xff)));lishititlete.setTextColor(Utils.changeAlpha(ContextCompat.getColor(getActivity(), R.color.white), (int) (1 - f * 1 * 0xff)));titlelitwo.setBackgroundColor(Utils.changeAlpha(ContextCompat.getColor(getActivity(), R.color.homeiconokc), (int) (f * 1 * 0xff)));titletetwo.setTextColor(Utils.changeAlpha(ContextCompat.getColor(getActivity(), R.color.white), (int) (f * 1 * 0xff)));} else if (y-oldY<0&&y>=0){float f = (y + 0f) / 400;//滑动距离350pxif (f > 1) {f = 1f;}if (f < 0) {f = 0;}Log.e("lgq", "......向xia滑。。。" + f + ".......y====" + y + "......zhi====" + (1 - f * 1 * 0xff) + ".....yy===" + f * 1 * 0xff);if (y <= 0||f<0.01) {lishititleli.setBackgroundColor(Utils.changeAlpha(ContextCompat.getColor(getActivity(), R.color.homeiconokc), (int) (1 * 1 * 0xff)));lishititlete.setTextColor(Utils.changeAlpha(ContextCompat.getColor(getActivity(), R.color.white), (int) (1 * 1 * 0xff)));return;}lishititleli.setBackgroundColor(Utils.changeAlpha(ContextCompat.getColor(getActivity(), R.color.homeiconokc), (int) (1 - f * 1 * 0xff)));lishititlete.setTextColor(Utils.changeAlpha(ContextCompat.getColor(getActivity(), R.color.white), (int) (1 - f * 1 * 0xff)));titlelitwo.setBackgroundColor(Utils.changeAlpha(ContextCompat.getColor(getActivity(), R.color.homeiconokc), (int) (f * 1 * 0xff)));titletetwo.setTextColor(Utils.changeAlpha(ContextCompat.getColor(getActivity(), R.color.white), (int) (f * 1 * 0xff)));}}

工具方法:

/*** 修改颜色透明度* @param color* @param alpha* @return*/
public static int changeAlpha(int color, int alpha) {int red = Color.red(color);int green = Color.green(color);int blue = Color.blue(color);return Color.argb(alpha, red, green, blue);
}

实现效果:向上滑动渐变颜色,显示隐藏view

demo链接:https://download.csdn.net/download/meixi_android/10966003

附:左滑右滑手势

 //渐变色float x1 = 0;float x2 = 0;private int nowpersion = 0;@Overridepublic boolean dispatchTouchEvent(MotionEvent ev) {if (ev.getAction() == MotionEvent.ACTION_DOWN) {
//            Log.i("lgq","ssssMainActivity_dispatchTouchEvent==="+ev.getX());x1 = ev.getX();}return super.dispatchTouchEvent(ev);}@Overridepublic boolean onTouch(View v, MotionEvent event) {if (event.getAction() == MotionEvent.ACTION_MOVE) {x2 = event.getX();float h = x1 - x2;//            Log.i("lgq","eee==滑动距离aaa===="+(h)+"......"+nowpersion+"...."+x1);float f = (h + 0f) / 640;//滑动距离350pxif (f > 1) {f = 1f;}if (f < 0) {f = 0;}if (nowpersion == 0 && h < 0) {return false;} else if (nowpersion == 0 && h > 0) {if (f < 0.4) {return false;}TextView textView = tabLayout.getTabAt(1).getCustomView().findViewById(R.id.tab_iv);TextView textView0 = tabLayout.getTabAt(0).getCustomView().findViewById(R.id.tab_iv);textView.setTextColor(changeAlpha(ContextCompat.getColor(MainActivity.this, R.color.textlan), (int) (f * 1 * 0xff)));textView0.setTextColor(changeAlpha(ContextCompat.getColor(MainActivity.this, R.color.texthui), (int) (f * 1 * 0xff)));}}return false;}/*** 修改颜色透明度* @param color* @param alpha* @return*/public  int changeAlpha(int color, int alpha) {int red = Color.red(color);int green = Color.green(color);int blue = Color.blue(color);return Color.argb(alpha, red, green, blue);}

android 标题栏颜色渐变和阴影,ScrollView上下滑动监听,及判断scrollView是否滚动到底部相关推荐

  1. ScrollView的滑动监听(以HorizontalScrollView为例)

    ScrollView不能像其他组件一样使用onScrollChanged()方法是因为它用protected封装了 protected void onScrollChanged(int x, int ...

  2. Android中自定义ScrollView的滑动监听事件,并在滑动时渐变标题栏背景颜色

    效果图 滑动前: 滑动中: 滑动到底部: 项目结构 ObservableScrollView package com.jukopro.titlebarcolor;import android.cont ...

  3. android scrollview 滚动监听,Android开发之ScrollView的滑动监听

    我们需要监听ScroView的滑动情况,比如滑动了多少距离,是否滑到布局的顶部或者底部.可惜的是SDK并没有相应的方法,不过倒是提供了一个 protected void onScrollChanged ...

  4. android 监听webView滑动距离和标题栏颜色渐变

    重写webView之 X5WebView import android.annotation.SuppressLint; import android.content.Context; import ...

  5. Android开发之解决NestedScrollView滑动监听兼容低版本的方法

    NestedScrollView的滑动监听目前仅限api23及以上,为了兼容低版本如下自定义方法 可以自定义NestedScrollView即可如下: package cn.net.gfan.worl ...

  6. Android监听左右滑删除通知,Android 滑动监听RecyclerView线性流+左右划删除+上下移动...

    废话不多说了,直接给大家贴代码了.具体代码如下所示: xmlns:tools="http://schemas.android.com/tools" android:layout_w ...

  7. Android RecyclerView(九)滑动监听综述

    Android RecyclerView(九)滑动监听 1 RecyclerView 的滑动监听 1.1 RecyclerView 设置滑动监听 mRecyclerView.setOnScrollLi ...

  8. android触摸滑动监听,Android 滑动监听的实例详解

    Android 滑动监听的实例详解 摘要: ScollBy,ScollTo是对内容的移动,view.ScollyBy是对view的内容的移动 view,ScollTo是对内容的移动(移动到指定位置), ...

  9. android模拟点击滑动,模拟Android的view点击和滑动监听

    一,简单形式的 /*模拟Android中的view 点击和滑动监听 * */ public class View { private OnClickListener onClickListener; ...

最新文章

  1. 互联网金融如何助力实体经济?
  2. 串口的波特率误差对数据发送影响
  3. 黑龙江科技大学计算机是几本,黑龙江科技大学是几本
  4. android simples 结构,android – 如何在simple_list_item_2中构造和显示信息?
  5. 如何设置电脑开机自动提示(防止忘打卡等)
  6. 五、工作量证明链解决拜占庭将军问题之模拟程序(Objective-C)
  7. iis服务器修改内存,修改IIS的虚拟内存
  8. PostgreSQL的pg_basebackup备份恢复详解
  9. python为什么那么多人用_为什么那么多人在学Python?
  10. 表面招助理实为“拉皮条”?招聘平台也有情色陷阱,BOSS直聘回应...
  11. 尘埃落定!熊猫互娱近20亿元投资纠纷已解决
  12. 免费python网络课程-2019年10种免费的Python学习课程
  13. java----数组
  14. mysql判断表字段或索引是否存在,然后修改
  15. mysql.exe怎么全屏_监控怎么调为全屏
  16. 【不存在的人】用Python获取生成随机头像,还不侵权
  17. android 木马行为监控,基于行为分析的Android手机木马检测技术研究
  18. Mendix装备制造业应用 | 质量统计分析人工智能应用APP
  19. matlab抢占时隙算法,ALOHA anti-collision、二进制数搜索算法以及帧时隙算法
  20. Interval (mathematics)

热门文章

  1. [vue] vue怎么实现强制刷新组件?
  2. 工作278:控制数据从字典表获取
  3. 前端学习(2505):小游戏设计
  4. 工作26:后端数据接口问题
  5. 前端学习(1603):脚手架组件使用
  6. 前端学习(1354):集合关联
  7. mybatis学习(36):动态sql-set
  8. 第七十七期:可自动生成代码,5款基于AI的开发工具
  9. Linux tm time_t timeval timespec以及与时间相关函数用法
  10. 全网首发!2020年AI、CV、NLP顶会最全时间表!