第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

同步滾動(dòng)視圖滾動(dòng)位置-Android

同步滾動(dòng)視圖滾動(dòng)位置-Android

同步滾動(dòng)視圖滾動(dòng)位置-Android我有兩個(gè)ScrollView在我的Android布局。如何同步他們的滾動(dòng)位置?
查看完整描述

3 回答

?
元芳怎么了

TA貢獻(xiàn)1798條經(jīng)驗(yàn) 獲得超7個(gè)贊

在ScrollView中有一種方法.。

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

不幸的是,Google從未想過我們需要訪問它,這就是為什么他們讓它受到保護(hù),并且沒有添加一個(gè)“setOnScrollChangedListener”鉤子。所以我們必須為自己做這件事。

首先我們需要一個(gè)接口。

package com.test;public interface ScrollViewListener {

    void onScrollChanged(ObservableScrollView scrollView, int x, int y, int oldx, int oldy);}

然后,我們需要重寫ScrollView類,以提供ScrollViewListener鉤子。

package com.test;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);
        }
    }}

我們應(yīng)該在布局中指定這個(gè)新的觀察者ScrollView類,而不是現(xiàn)有的ScrollView標(biāo)記。

<com.test.ObservableScrollView
    android:id="@+id/scrollview1"
    ... >

    ...</com.test.ObservableScrollView>

最后,我們將其放在布局類中。

package com.test;import android.app.Activity;import android.os.Bundle;public class Q3948934 extends Activity implements ScrollViewListener {

    private ObservableScrollView scrollView1 = null;
    private ObservableScrollView scrollView2 = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.q3948934);

        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);
        }
    }}

SCROLLTO()代碼為我們處理任何循環(huán)條件,因此我們不需要擔(dān)心這個(gè)問題。唯一的警告是,這個(gè)解決方案不能保證在Android的未來(lái)版本中工作,因?yàn)槲覀冋诟采w一個(gè)受保護(hù)的方法。


查看完整回答
反對(duì) 回復(fù) 2019-06-27
?
慕哥6287543

TA貢獻(xiàn)1831條經(jīng)驗(yàn) 獲得超10個(gè)贊

Andy的解決方案的一個(gè)改進(jìn):在他的代碼中,他使用了滾動(dòng)到,問題是,如果你把一個(gè)滾動(dòng)視圖扔向一個(gè)方向,然后把另一個(gè)滾動(dòng)視圖扔向另一個(gè)方向,你會(huì)注意到第一個(gè)滾動(dòng)視圖并沒有停止他以前的快速移動(dòng)。

這是因?yàn)镾CROLLView使用ComputeScroll()來(lái)執(zhí)行拋出的手勢(shì),并且它與滾動(dòng)到?jīng)_突。

為了防止這種情況,只需按以下方式對(duì)onScrollChanged進(jìn)行編程:

    public void onScrollChanged(ObservableScrollView scrollView, int x, int y, int oldx, int oldy) {
    if(interceptScroll){
        interceptScroll=false;
        if(scrollView == scrollView1) {
            scrollView2.onOverScrolled(x,y,true,true);
        } else if(scrollView == scrollView2) {
            scrollView1.onOverScrolled(x,y,true,true);
        }
        interceptScroll=true;
    }}

使用intereptScroll,初始化為true的靜態(tài)布爾值。(這有助于避免ScrollChanged上的無(wú)限循環(huán))

OnOverScrolled是我發(fā)現(xiàn)的唯一可以用來(lái)阻止?jié)L動(dòng)視圖拋出的函數(shù)(但可能還有其他函數(shù)我錯(cuò)過了!)

為了訪問這個(gè)函數(shù)(它是受保護(hù)的),您必須將它添加到您的觀測(cè)者ScrollViewer中。

public void onOverScrolled(int scrollX, int scrollY, boolean clampedX, boolean clampedY) {
    super.onOverScrolled(scrollX, scrollY, clampedX, clampedY);}


查看完整回答
反對(duì) 回復(fù) 2019-06-27
?
忽然笑

TA貢獻(xiàn)1806條經(jīng)驗(yàn) 獲得超5個(gè)贊

為什么不直接實(shí)現(xiàn)OnTouchListener在你的活動(dòng)中。然后重寫OnTouch方法,然后獲得第一個(gè)滾動(dòng)位置ScrollViewOne.getScrollY()和更新ScrollViewTwo.scrollTo(0, ScrollViewOne.getScrollY());

只是另一個(gè)想法.。*)


查看完整回答
反對(duì) 回復(fù) 2019-06-27
  • 3 回答
  • 0 關(guān)注
  • 444 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)