Android之重寫ScrollView實現兩個ScrollView的同步滾動顯示
1.背景介紹
最近專案用到兩個ScrollView的同步顯示,即拖動左邊的ScrollView滾動的同時,實現右邊的ScrollView同步滾動。此種情形常用在複雜介面佈局中,比如左邊的ScrollView顯示主要專案,只需上下滾動即可;右邊專案是次要專案,可以實現上下或者左右滾動,當上下滾動時,需要左右兩邊的同步顯示。
如圖所示,左側是主專案(日期和股票程式碼),右側是次要專案(開盤價、最高價、成交量....等等資訊)。因為資訊比較多,左側的主專案需要上下拖動顯示,而右側則需要上下左右都可以拖動才能顯示完全(ScrollView巢狀一個HorizontalScrollView)。我們希望左側或右側上下拖動時,能夠實現同步。這就需要實現兩個ScrollView的同步顯示。因為Android控制元件中沒有此種功能,因此需要重寫ScrollView。
2.思路介紹
我們首先想到使用ScrollView的類似與setOnScrollChangedListener的方法來實現,當一個ScrollView滾動時,觸發該方法進而使另外一個ScrollView滾動。不過很遺憾,谷歌沒有提供該方法。通過查詢相應的原始碼,我們發現該方法的原型
protected void onScrollChanged(int x, int y, int oldx, int oldy)
該方法是protected型別,不能直接呼叫,於是需要重新實現ScrollView。
3.具體實現
首先,定一個一個介面(ScrollViewListener.java):
public interface ScrollViewListener {
void onScrollChanged(ObservableScrollView scrollView, int x, int y, int oldx, int oldy);
}
我們需要重寫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);
}
}
}
相關文章
- ScrollView 之 實現檢視的迴圈顯示View
- Android ScrollView滾動到指定View的位置AndroidView
- scrollview滾動到指定位置View
- Xamarin.Forms的滾動檢視ScrollViewORMView
- iOS開發專案實戰——Swift實現ScrollView滾動條功能iOSSwiftView
- 使用ScrollView滾動事件打造動畫框架ScrollAnimationSherlockView事件動畫框架
- C# scrollView個數隨機UI從上部顯示C#View隨機UI
- android:focusableInTouchMode為什麼能解決ScrollView自動滾動的原理分析AndroidView
- android listview and scrollviewAndroidView
- android處理2個ScrollView聯動問題AndroidView
- AutoLayout 實現固定寬度動態高度的 ScrollViewView
- 實現一個帶下拉彈簧動畫的 ScrollView動畫View
- Android中ScrollView實現拖拽反彈效果動畫AndroidView動畫
- iOS開發-探索scrollView的實現iOSView
- ScrollViewView
- 如何給Scrollview裡內容截圖並生成bitmap,注意:Scrollview裡面內容較多有滾動了·View
- 【Android】ScrollView巢狀ListView只顯示第一行問題原理分析AndroidView巢狀
- Android -下拉重新整理,ViewPager和ScrollView巢狀滾動問題解決方案AndroidViewpager巢狀
- android之截圖(包括擷取scrollview與listview的)AndroidView
- IOS ScrollViewiOSView
- android 自定義ScrollView實現背景圖片伸縮的實現程式碼及思路AndroidView
- Android帶有粘性頭部的ScrollViewAndroidView
- Android UI系列-----ScrollView和HorizontalScrollViewAndroidUIView
- ScrollView中巢狀ListView,TextView只顯示一行的高度解決方案巢狀TextView
- 表格顯示滾動條
- ScrollView 觸控事件View事件
- 【程式設計框架】Android可配置的ScrollView框架程式設計框架AndroidView
- scrollView的偏移和縮放、pageControl(小點)和scrollView的連線View
- Masonry 關於ScrollView的使用View
- Flutter Sliver一生之敵 (ScrollView)FlutterView
- Android中RecyclerView與Scrollview組合使用(二)AndroidView
- vue之實現日曆----顯示農曆,滾動日曆監聽年月改變Vue
- ScrollView 與ListView 滑動衝突解決View
- ScrollView(RecyclerView等)為什麼會自動滾動原理分析,還有阻止自動滑動的解決方View
- ScrollView(RecyclerView等)為什麼會自動滾動原理分析,還有阻止自動滑動的解決方案View
- 解決ScrollView巢狀ViewPager出現的滑動衝突問題巢狀Viewpager
- Java——圖片滾動顯示Java
- scrollview 的滑動衝突 viewpager等都適用Viewpager