package com.ql.view;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.HorizontalScrollView;
public class SyncHorizontalScrollView extends HorizontalScrollView{
private View mView;
public SyncHorizontalScrollView(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
public SyncHorizontalScrollView(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}
protected void onScrollChanged(int l, int t, int oldl, int oldt) {
super.onScrollChanged(l, t, oldl, oldt);
if(mView!=null){
mView.scrollTo(l, t);
}
}
public void setScrollView(View view){
mView = view;
}
}
用法:
scrollView0=(SyncHorizontalScrollView)findViewById(R.id.scrollView0);
scrollView1=(SyncHorizontalScrollView)findViewById(R.id.scrollView1);
scrollView0.setScrollView(scrollView1);
scrollView1.setScrollView(scrollView0);
這樣其中一個滾動時另一個也跟着滾動了。
水平滾動解決了,那豎直滾動的ScrollView也類似的寫法。
重寫ScrollView實現兩個ScrollView的同步滾動顯示
http://www.cnblogs.com/devinzhang/archive/2012/07/13/2590222.html
還有一個問題:
如何讓HorizontalScrollView總是停留在幾個固定的位置?
比如HorizontalScrollView滾動到112位置的時候,需要判斷它離100近還是200近,如果離100近就接着滾動到100的位置,否則滾動到200的位置。如何解決?
HorizontalScrollView可以解決Tab過多的問題
網上大多都是用Gallery來模擬TabHost 但是自己實在是懶 就沒這麼做 一種可以湊合着用的簡單做法,只需要吧layout改一下即可:
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#001629">
<HorizontalScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TabWidget android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</HorizontalScrollView>
<FrameLayout android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingTop="95px">
</FrameLayout>
</TabHost>
在TabWidget外面加上HorizontalScrollView即可。
不過 這樣有個問題 寬度貌似改不了了 即使你只有一個tab 它並不會fill_parent 還是原來的寬度。(試試設置HorizontalScrollView的android:fillViewport="true")
但是 如果很多tab的話 還是好使的。
擴展HorizontalScrollView實現整個屏幕滾動
http://www.eoeandroid.com/forum.php?mod=viewthread&tid=148434&fromuid=30206