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
原创粉丝点击