Android_ScrollView的监听以及回到顶部
2018-01-30 09:34
597 查看
重写ScrollView
public class ObserveScrollView extends ScrollView {
private ScrollListener mListener;
public static interface ScrollListener {//声明接口,用于传递数据
public void scrollOritention(int l, int t, int oldl, int oldt);
}
public ObserveScrollView(Context context) {
super(context);
}
public ObserveScrollView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public ObserveScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected void onScrollChanged(int l, int t, int oldl, int oldt) {
// TODO Auto-generated method stub
super.onScrollChanged(l, t, oldl, oldt);
if (mListener != null) {
mListener.scrollOritention(l, t, oldl, oldt);
}
}
public void setScrollListener(ScrollListener l) {
this.mListener = l;
}
}代码文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.apptext.MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="30dp"
android:background="#ff3600"
android:visibility="invisible"
android:id="@+id/ly"
android:orientation="vertical"
></LinearLayout>
<com.example.apptext.ObserveScrollView
android:id="@+id/sc"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<ImageView
android:layout_width="wrap_content"
android:layout_height="70dp"
android:src="@mipmap/ic_launcher"
/>
</LinearLayout>
</com.example.apptext.ObserveScrollView>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/but"
android:text="置顶"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
/>
</RelativeLayout>
public class ObserveScrollView extends ScrollView {
private ScrollListener mListener;
public static interface ScrollListener {//声明接口,用于传递数据
public void scrollOritention(int l, int t, int oldl, int oldt);
}
public ObserveScrollView(Context context) {
super(context);
}
public ObserveScrollView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public ObserveScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected void onScrollChanged(int l, int t, int oldl, int oldt) {
// TODO Auto-generated method stub
super.onScrollChanged(l, t, oldl, oldt);
if (mListener != null) {
mListener.scrollOritention(l, t, oldl, oldt);
}
}
public void setScrollListener(ScrollListener l) {
this.mListener = l;
}
}代码文件
package com.example.apptext; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.LinearLayout; public class MainActivity extends AppCompatActivity { private ObserveScrollView sc; private Button but; private LinearLayout ly; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //初始化组件 sc = (ObserveScrollView) findViewById(R.id.sc); ly = (LinearLayout) findViewById(R.id.ly); but = (Button) findViewById(R.id.but); //监听事件 sc.setScrollListener(new ObserveScrollView.ScrollListener() { @Override public void scrollOritention(int l, int t, int oldl, int oldt) { if(t>100){ ly.setVisibility(View.VISIBLE); }else{ ly.setVisibility(View.INVISIBLE); } } }); //点击回到顶部事件 but.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { //监听点击回到顶部 sc.scrollTo(0,0); } }); } }布局文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.apptext.MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="30dp"
android:background="#ff3600"
android:visibility="invisible"
android:id="@+id/ly"
android:orientation="vertical"
></LinearLayout>
<com.example.apptext.ObserveScrollView
android:id="@+id/sc"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<ImageView
android:layout_width="wrap_content"
android:layout_height="70dp"
android:src="@mipmap/ic_launcher"
/>
</LinearLayout>
</com.example.apptext.ObserveScrollView>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/but"
android:text="置顶"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
/>
</RelativeLayout>
相关文章推荐
- Android listview ,ScrollView 回到顶部的按钮
- Android ScrollView监听值判断在底部还是顶部
- Android ScrollView监听滑动到顶部和底部的两种方式(你可能不知道的细节)
- Android ScrollView监听滑动到顶部和底部的两种方式
- Android设置ScrollView回到顶部的三种方式 (转)
- android 实现了知乎日报的大部分功能,增加双击标题栏回到顶部以及双击 webview 返回两个实用功能
- Android监听ScrollView或NestedScrollView滑动到顶部和底部
- Android ScrollView监听滑动到顶部和底部的两种方式(你可能不知道的细节)
- Android的ScrollView的滚动的监听以及屏幕静止状态的监听实现
- Android 自定义ScrollView嵌套滚动兼容,监听滑动状态:顶部、底部、停止、滑动中
- [置顶] Android ScrollView监听滑动到顶部和底部的两种方式(你可能不知道的细节)
- android新特性:商城首页一键回到顶部功能实现(包括ListView, RecyclerView 和 ScrollView)
- Android设置ScrollView回到顶部的三种方式
- Android - ScrollView 监听滑动停止,顶部还是底部?
- Android -- 固定在ScrollView顶部的View,类似于新浪微博的评论列表的顶部
- Android发送短信以及监听发送状态
- Android中使用ScrollView指定view的顶部悬停效果
- scrollView和listiew共用的时候如何回到顶部?
- Android中Preference的使用以及监听事件分析
- Android Scrollview不能定位到顶部的问题?