当前位置: 移动技术网 > IT编程>移动开发>Android > Android中实现监听ScrollView滑动事件

Android中实现监听ScrollView滑动事件

2019年07月24日  | 移动技术网IT编程  | 我要评论

摩托吧交易区,馄饨的包法图解,耳根作品

时候我们需要监听scroview的滑动情况,比如滑动了多少距离,是否滑到布局的顶部或者底部。可惜的是sdk并没有相应的方法,不过倒是提供了一个

复制代码 代码如下:

protected void onscrollchanged(int x, int y, int oldx, int oldy) 

方法,显然这个方法是不能被外界调用的,因此就需要把它暴露出去,方便使用。解决方式就是写一个接口,
复制代码 代码如下:

package com.example.demo1; 
 
public interface scrollviewlistener { 
 
    void onscrollchanged(observablescrollview scrollview, int x, int y, int oldx, int oldy); 
 

 然后重写scrollview类,给它提供上面写的回调接口。

复制代码 代码如下:

package com.example.demo1; 
 
import android.content.context; 
import android.util.attributeset; 
import android.widget.scrollview; 
 
public class observablescrollview extends scrollview { 
 
    private scrollviewlistener scrollviewlistener = null; 
 
    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 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); 
        } 
    } 
 

注意在xml布局的时候,不要写错了包。

复制代码 代码如下:

<linearlayout 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:orientation="horizontal" 
    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=".mainactivity" > 
 
    <com.example.demo1.observablescrollview 
        android:id="@+id/view1" 
        android:layout_width="wrap_content" 
        android:layout_height="match_parent" > 
 
        <linearlayout 
            android:layout_width="wrap_content" 
            android:layout_height="match_parent" 
            android:orientation="vertical" > 
 
            <textview 
                android:layout_width="100dp" 
                android:layout_height="100dp" 
                android:text="试试1" /> 
 
            <textview 
                android:layout_width="100dp" 
                android:layout_height="100dp" 
                android:text="试试2" /> 
 
            <textview 
                android:layout_width="100dp" 
                android:layout_height="100dp" 
                android:text="试试3" /> 
 
            <textview 
                android:layout_width="100dp" 
                android:layout_height="100dp" 
                android:text="试试4" /> 
 
            <textview 
                android:layout_width="100dp" 
                android:layout_height="100dp" 
                android:text="试试5" /> 
 
            <textview 
                android:layout_width="100dp" 
                android:layout_height="100dp" 
                android:text="试试6" /> 
        </linearlayout> 
    </com.example.demo1.observablescrollview> 
 
    <com.example.demo1.observablescrollview 
        android:id="@+id/view2" 
        android:layout_width="wrap_content" 
        android:layout_height="match_parent" > 
 
        <linearlayout 
            android:layout_width="wrap_content" 
            android:layout_height="match_parent" 
            android:orientation="vertical" > 
 
            <textview 
                android:layout_width="100dp" 
                android:layout_height="100dp" 
                android:text="试试1" /> 
 
            <textview 
                android:layout_width="100dp" 
                android:layout_height="100dp" 
                android:text="试试2" /> 
 
            <textview 
                android:layout_width="100dp" 
                android:layout_height="100dp" 
                android:text="试试3" /> 
 
            <textview 
                android:layout_width="100dp" 
                android:layout_height="100dp" 
                android:text="试试4" /> 
 
            <textview 
                android:layout_width="100dp" 
                android:layout_height="100dp" 
                android:text="试试5" /> 
 
            <textview 
                android:layout_width="100dp" 
                android:layout_height="100dp" 
                android:text="试试6" /> 
        </linearlayout> 
    </com.example.demo1.observablescrollview> 
 
</linearlayout> 
 

  最后activity代码如下,
复制代码 代码如下:

package com.example.demo1; 
 
import android.os.bundle; 
import android.app.activity; 
import android.view.menu; 
 
public class mainactivity extends activity implements scrollviewlistener { 
 
    private observablescrollview scrollview1 = null; 
    private observablescrollview scrollview2 = null; 
 
    @override 
    protected void oncreate(bundle savedinstancestate) { 
        super.oncreate(savedinstancestate); 
        setcontentview(r.layout.activity_main); 
 
        scrollview1 = (observablescrollview) findviewbyid(r.id.view1); 
        scrollview1.setscrollviewlistener(this); 
        scrollview2 = (observablescrollview) findviewbyid(r.id.view2); 
        scrollview2.setscrollviewlistener(this); 
 
    } 
 
    @override 
    public boolean oncreateoptionsmenu(menu menu) { 
        // inflate the menu; this adds items to the action bar if it is present. 
        getmenuinflater().inflate(r.menu.main, menu); 
        return true; 
    } 
 
    @override 
    public void onscrollchanged(observablescrollview scrollview, int x, int y, 
            int oldx, int oldy) { 
        if (scrollview == scrollview1) { 
            scrollview2.scrollto(x, y); 
        } else if (scrollview == scrollview2) { 
            scrollview1.scrollto(x, y); 
        } 
    } 
 


如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网