ScrollView返回到顶部效果
来源:互联网 发布:新闻软件哪个好 编辑:程序博客网 时间:2024/06/06 18:36
好多的时候,需求滑倒某一位置,显示返回顶部的按钮,点击按钮,滑到顶部。但是ScrollView提供的onScrollChanged方法没有向外暴露,所以我们就要重写它。
定义接口
public interface ScrollViewListener { void onScrollChanged(ObservableScrollView scrollView, int x, int y, int oldx, int oldy);}
自定义ScrollView
public class ObservableScrollView extends ScrollView { private ScrollViewListener scrollViewListener = null; public ObservableScrollView(Context context) { super(context); } public ObservableScrollView(Context context, AttributeSet attrs) { super(context, attrs); } public ObservableScrollView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } 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); } }}
布局(ScrollView只允许有一个子布局)
<?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" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.ws.scrollcviewrerurntop.MainActivity"> <com.example.ws.scrollcviewrerurntop.ObservableScrollView android:id="@+id/scrollow" android:layout_width="match_parent" android:layout_height="match_parent" android:fillViewport="true"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <ImageView android:layout_width="match_parent" android:layout_height="wrap_content" android:src="@mipmap/ic_launcher" /> <ImageView android:layout_width="match_parent" android:layout_height="wrap_content" android:src="@mipmap/ic_launcher" /> <ImageView android:layout_width="match_parent" android:layout_height="wrap_content" android:src="@mipmap/ic_launcher" /> <ImageView android:layout_width="match_parent" android:layout_height="wrap_content" android:src="@mipmap/ic_launcher" /> <ImageView android:layout_width="match_parent" android:layout_height="wrap_content" android:src="@mipmap/ic_launcher" /> <ImageView android:layout_width="match_parent" android:layout_height="wrap_content" android:src="@mipmap/ic_launcher" /> <ImageView android:layout_width="match_parent" android:layout_height="wrap_content" android:src="@mipmap/ic_launcher" /> <ImageView android:layout_width="match_parent" android:layout_height="wrap_content" android:src="@mipmap/ic_launcher" /> <ImageView android:layout_width="match_parent" android:layout_height="wrap_content" android:src="@mipmap/ic_launcher" /> <ImageView android:layout_width="match_parent" android:layout_height="wrap_content" android:src="@mipmap/ic_launcher" /> <ImageView android:layout_width="match_parent" android:layout_height="wrap_content" android:src="@mipmap/ic_launcher" /> <ImageView android:layout_width="match_parent" android:layout_height="wrap_content" android:src="@mipmap/ic_launcher" /> <ImageView android:layout_width="match_parent" android:layout_height="wrap_content" android:src="@mipmap/ic_launcher" /> <ImageView android:layout_width="match_parent" android:layout_height="wrap_content" android:src="@mipmap/ic_launcher" /> <ImageView android:layout_width="match_parent" android:layout_height="wrap_content" android:src="@mipmap/ic_launcher" /> <ImageView android:layout_width="match_parent" android:layout_height="wrap_content" android:src="@mipmap/ic_launcher" /> <ImageView android:layout_width="match_parent" android:layout_height="wrap_content" android:src="@mipmap/ic_launcher" /> <ImageView android:layout_width="match_parent" android:layout_height="wrap_content" android:src="@mipmap/ic_launcher" /> <ImageView android:layout_width="match_parent" android:layout_height="wrap_content" android:src="@mipmap/ic_launcher" /> <ImageView android:layout_width="match_parent" android:layout_height="wrap_content" android:src="@mipmap/ic_launcher" /> <ImageView android:layout_width="match_parent" android:layout_height="wrap_content" android:src="@mipmap/ic_launcher" /> <ImageView android:layout_width="match_parent" android:layout_height="wrap_content" android:src="@mipmap/ic_launcher" /> <ImageView android:layout_width="match_parent" android:layout_height="wrap_content" android:src="@mipmap/ic_launcher" /> <ImageView android:layout_width="match_parent" android:layout_height="wrap_content" android:src="@mipmap/ic_launcher" /> <ImageView android:layout_width="match_parent" android:layout_height="wrap_content" android:src="@mipmap/ic_launcher" /> </LinearLayout> </com.example.ws.scrollcviewrerurntop.ObservableScrollView> <ImageView android:id="@+id/image" android:visibility="gone" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@mipmap/ic_launcher" android:layout_alignParentRight="true" android:layout_alignParentBottom="true"/></RelativeLayout>
当下滑200像素后显示返回顶部的按钮,点击按钮后回到顶部
public class ScrollViewDemo extends AppCompatActivity { private ObservableScrollView mScrollView; private ImageView mImageView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_scroll_view_demo); mScrollView = (ObservableScrollView) findViewById(R.id.scrollow); mImageView = (ImageView) findViewById(R.id.image); mImageView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { mScrollView.scrollTo(0, 0); } }); mScrollView.setScrollViewListener(new ScrollViewListener() { @Override public void onScrollChanged(ObservableScrollView scrollView, int x, int y, int oldx, int oldy) { if (y > 200) { mImageView.setVisibility(View.VISIBLE); } else { mImageView.setVisibility(View.GONE); } } }); }}
0 0
- ScrollView返回到顶部效果
- 点击控件 让ListView或者ScrollView 返回到顶部
- scrollview滚动到顶部
- 返回顶部效果
- javascript返回顶部效果
- jQuery返回顶部效果
- HTML5 返回顶部 效果
- jQuery返回顶部效果
- 返回顶部效果
- 网站返回顶部效果
- 返回顶部效果!
- JS实现效果-点击按钮返回到页面顶部
- android ScrollView 滚动到顶部
- android ScrollView 滚动到顶部
- 返回到顶部按钮
- 返回到顶部代码
- Android利用贝塞尔曲线实现ScrollView滑动到顶部后阴影反馈效果
- 仿新浪网“返回顶部”效果
- 判断一棵二叉树是否是二叉树的子树
- 一文读懂机器学习大数据/自然语言处理/算法全有了【一】
- iOS--多线程-01
- 一些时间选择的判断和不能设定比当前时间早的日期?
- 菜鸟之插入排序
- ScrollView返回到顶部效果
- awk功能学习
- bootstrap-datetimepicker的基本用法
- Android代码混淆
- Extjs常用函数介绍:
- kibana 安装与使用
- Xcode7/iOS9支持http访问
- 在JetBrains WebStorm 10.0.4中查看历史编辑记录
- 获取客户端Mac地址