【转】重写ScrollView实现两个ScrollView的同步滚动显示

本文介绍了一种实现两个ScrollView同步滚动的方法。通过对ScrollView进行扩展并创建一个监听接口,可以在一个ScrollView滚动时触发另一个ScrollView的滚动。文章提供了完整的代码示例,包括自定义的ObservableScrollView类、界面布局文件以及主活动代码。

图片

我们首先想到使用ScrollView的类似与setOnScrollChangedListener的方法来实现,当一个ScrollView滚动时,触发该方法进而使另外一个ScrollView滚动。不过很遗憾,谷歌没有提供该方法。通过查询相应的源代码,我们发现该方法的原型

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

该方法是protected类型,不能直接调用,于是需要重新实现ScrollView

首先,定一个一个接口(ScrollViewListener.java):

public interface ScrollViewListener { void onScrollChanged(ObservableScrollView scrollView, int x, int y, int oldx, intoldy);}

 我们需要重写ScrollView才能实现该借口,因此有下面的代码(ObservableScrollView.java):

package com.devin;

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,这里是一个简单的Demo,如下(main.xml):    

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android=" http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="#ffffff"
    android:orientation="horizontal" >
    <com.devin.ObservableScrollView
        android:id="@+id/scrollview1"
        android:layout_width="400dp"
        android:layout_height="wrap_content" >
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical" >
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="200dp"
                android:layout_weight="1"
                android:text="monday"
                android:textColor="#000000" />
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="200dp"
                android:layout_weight="1"
                android:text="tuesday"
                android:textColor="#000000" />
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="200dp"
                android:layout_weight="1"
                android:text="wednesday"
                android:textColor="#000000" />
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="200dp"
                android:layout_weight="1"
                android:text="thursday"
                android:textColor="#000000" />
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="200dp"
                android:layout_weight="1"
                android:text="friday"
                android:textColor="#000000" />
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="200dp"
                android:layout_weight="1"
                android:text="saturday"
                android:textColor="#000000" />
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="200dp"
                android:layout_weight="1"
                android:text="sunday"
                android:textColor="#000000" />
        </LinearLayout>
    </com.devin.ObservableScrollView>
    <com.devin.ObservableScrollView
        android:id="@+id/scrollview2"
        android:layout_width="400dp"
        android:layout_height="wrap_content" >
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical" >
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="200dp"
                android:layout_weight="1"
                android:text="monday"
                android:textColor="#000000" />
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="200dp"
                android:layout_weight="1"
                android:text="tuesday"
                android:textColor="#000000" />
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="200dp"
                android:layout_weight="1"
                android:text="wednesday"
                android:textColor="#000000" />
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="200dp"
                android:layout_weight="1"
                android:text="thursday"
                android:textColor="#000000" />
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="200dp"
                android:layout_weight="1"
                android:text="friday"
                android:textColor="#000000" />
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="200dp"
                android:layout_weight="1"
                android:text="saturday"
                android:textColor="#000000" />
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="200dp"
                android:layout_weight="1"
                android:text="sunday"
                android:textColor="#000000" />
        </LinearLayout>
    </com.devin.ObservableScrollView>
</LinearLayout>

最后是我们的主程调用(PadTestActivity.java):

package com.devin;
import android.app.Activity;
import android.os.Bundle;
public class PadTestActivity extends Activity implements ScrollViewListener {
    private ObservableScrollView scrollView1 = null;
    private ObservableScrollView scrollView2 = null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        scrollView1 = (ObservableScrollView) findViewById(R.id.scrollview1);
        scrollView1.setScrollViewListener(this);
        scrollView2 = (ObservableScrollView) findViewById(R.id.scrollview2);
        scrollView2.setScrollViewListener(this);
    }
    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);
        }
    }
}

转载于:https://www.cnblogs.com/chenlong-50954265/p/3924130.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值