ScrollView返回Top悬浮按钮
来源:互联网 发布:魔兽世界插件for mac 编辑:程序博客网 时间:2024/06/03 17:51
最近项目不是很忙,但是吧!又闲不住!忽然就想起了这种效果!出于以后项目中有可能会用到这个效果的原因,所以说还是玩玩吧!这种效果虽然不是很多,但是之前玩过的app中也遇到过这种效果所以说还是做一下记录吧! ~重写方法看如下所示:
查了查网上的方法,基本上都是重写ScrollView,so,鄙人就无耻的站在巨人的肩膀上,哈哈
import android.content.Context;import android.util.AttributeSet;import android.widget.ScrollView;public class ObservableScrollView extends ScrollView { private OnScollChangedListener onScollChangedListener; 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 setOnScollChangedListener(OnScollChangedListener onScollChangedListener) { this.onScollChangedListener = onScollChangedListener; } @Override protected void onScrollChanged(int x, int y, int oldx, int oldy) { super.onScrollChanged(x, y, oldx, oldy); if (onScollChangedListener != null) { onScollChangedListener.onScrollChanged(this, x, y, oldx, oldy); } } public interface OnScollChangedListener { void onScrollChanged(ObservableScrollView scrollView, int x, int y, int oldx, int oldy); }}
怎么样!简单吧!有人要问为啥需要重写,ScrollView不也有自己的setOnScollChangeListener?原因:你试了就知道了!有限制条件滴~~
第二步:然后xml中就可以装逼了!!
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".activity.MainActivity"> <com.example.administrator.demo2.view.ObservableScrollView android:id="@+id/scrollView" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TextView android:layout_width="match_parent" android:layout_height="100dp" android:text="镜花水月1" android:gravity="center"/> <TextView android:layout_width="match_parent" android:layout_height="100dp" android:text="镜花水月2" android:gravity="center"/> <TextView android:layout_width="match_parent" android:layout_height="100dp" android:text="镜花水月3" android:gravity="center"/> <TextView android:layout_width="match_parent" android:layout_height="100dp" android:text="镜花水月4" android:gravity="center"/> <TextView android:layout_width="match_parent" android:layout_height="100dp" android:text="镜花水月5" android:gravity="center"/> <TextView android:layout_width="match_parent" android:layout_height="100dp" android:text="镜花水月6" android:gravity="center"/> <TextView android:layout_width="match_parent" android:layout_height="100dp" android:text="镜花水月7" android:gravity="center"/> <TextView android:layout_width="match_parent" android:layout_height="100dp" android:text="镜花水月8" android:gravity="center"/> <TextView android:layout_width="match_parent" android:layout_height="100dp" android:text="镜花水月9" android:gravity="center"/> <TextView android:layout_width="match_parent" android:layout_height="100dp" android:text="镜花水月10" android:gravity="center"/> <TextView android:layout_width="match_parent" android:layout_height="100dp" android:text="镜花水月11" android:gravity="center"/> <TextView android:layout_width="match_parent" android:layout_height="100dp" android:text="镜花水月12" android:gravity="center"/> <TextView android:layout_width="match_parent" android:layout_height="100dp" android:text="镜花水月13" android:gravity="center"/> <TextView android:layout_width="match_parent" android:layout_height="100dp" android:text="镜花水月14" android:gravity="center"/> <TextView android:layout_width="match_parent" android:layout_height="100dp" android:text="镜花水月15" android:gravity="center"/> </LinearLayout> </com.example.administrator.demo2.view.ObservableScrollView> <ImageView android:id="@+id/btn_top" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/back_top" android:visibility="invisible" android:layout_alignParentRight="true" android:layout_alignParentBottom="true" android:layout_marginBottom="12dp" android:layout_marginRight="12dp" /></RelativeLayout>
悬浮按钮是要先设置隐藏的~~
最后,acitivty代码:
package com.example.administrator.demo2.activity;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.widget.ImageView;import com.example.administrator.demo2.R;import com.example.administrator.demo2.view.ObservableScrollView;public class MainActivity extends Activity { private ObservableScrollView scrollView; private ImageView btn_top; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); scrollView = (ObservableScrollView) findViewById(R.id.scrollView); btn_top = (ImageView) findViewById(R.id.btn_top); scrollView.setOnScollChangedListener(new ObservableScrollView.OnScollChangedListener() { @Override public void onScrollChanged(ObservableScrollView scrollView, int x, int y, int oldx, int oldy) { if(y>600){ btn_top.setVisibility(View.VISIBLE); }else { btn_top.setVisibility(View.INVISIBLE); } } }); btn_top.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { scrollView.scrollTo(0,0); } }); }}
看吧?!也不是很难!搞定了!!其实就是监听ScrollView的一个向上滚动的距离!!
0 0
- ScrollView返回Top悬浮按钮
- listview一键返回顶部悬浮按钮
- 悬浮按钮
- 悬浮按钮
- 悬浮按钮
- 悬浮按钮
- 悬浮按钮
- 悬浮球 / 悬浮按钮 / 辅助按钮
- 悬浮链接_按钮
- 悬浮的按钮
- 悬浮按钮的实现
- android 悬浮按钮 FloatActionButton
- onTouchEventListener实现悬浮按钮
- 显示悬浮按钮
- 实现悬浮按钮
- 全局悬浮按钮(FAB)
- iOS-悬浮按钮
- 旋转动画+悬浮按钮
- PendingIntent的用法,转载自网络大腿翻译文章
- 用python + openpyxl处理excel(07+)多文档合并+表格合并 + 一些中文处理的技巧
- 几种机器学习模型的基本思路
- 书单(持续更新中...)
- 一个产品酝酿到落地过程
- ScrollView返回Top悬浮按钮
- Xshell5的简单使用,结合Xftp 4导入.sql文件 进行数据库操作
- jQuery笔记——动画设计——动画队列
- Rabbitmq延迟消息
- Redis 发布订阅
- 深入理解Java虚拟机 读书笔记——垃圾收集器与内存分配策略
- js匀速运动
- 安装Hadoop及Spark(Ubuntu 16.04)
- Fragment概述