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