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):
package com.scrollview.two.ganged;
public interface ScrollViewListener {
void
onScrollChanged(ObservableScrollView scrollView, int x, int y, int
oldx, int oldy);
}
我们需要重写ScrollView才能实现该借口,因此有下面的代码(ObservableScrollView.java):
package com.scrollview.two.ganged;
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):
encoding="utf-8"?>
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#ffffff"
android:orientation="horizontal" >
android:id="@+id/scrollview1"
android:layout_width="400dp"
android:layout_height="wrap_content"
>
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
android:layout_width="wrap_content"
android:layout_height="200dp"
android:layout_weight="1"
android:text="monday"
android:textColor="#000000" />
android:layout_width="wrap_content"
android:layout_height="200dp"
android:layout_weight="1"
android:text="tuesday"
android:textColor="#000000" />
android:layout_width="wrap_content"
android:layout_height="200dp"
android:layout_weight="1"
android:text="wednesday"
android:textColor="#000000" />
android:layout_width="wrap_content"
android:layout_height="200dp"
android:layout_weight="1"
android:text="thursday"
android:textColor="#000000" />
android:layout_width="wrap_content"
android:layout_height="200dp"
android:layout_weight="1"
android:text="friday"
android:textColor="#000000" />
android:layout_width="wrap_content"
android:layout_height="200dp"
android:layout_weight="1"
android:text="saturday"
android:textColor="#000000" />
android:layout_width="wrap_content"
android:layout_height="200dp"
android:layout_weight="1"
android:text="sunday"
android:textColor="#000000" />
android:layout_width="1000dip"
android:layout_height="wrap_content"
>
android:id="@+id/scrollview2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
android:layout_width="wrap_content"
android:layout_height="200dp"
android:layout_weight="1"
android:text="monday"
android:textColor="#000000" />
android:layout_width="wrap_content"
android:layout_height="200dp"
android:layout_weight="1"
android:text="tuesday"
android:textColor="#000000" />
android:layout_width="wrap_content"
android:layout_height="200dp"
android:layout_weight="1"
android:text="wednesday"
android:textColor="#000000" />
android:layout_width="wrap_content"
android:layout_height="200dp"
android:layout_weight="1"
android:text="thursday"
android:textColor="#000000" />
android:layout_width="wrap_content"
android:layout_height="200dp"
android:layout_weight="1"
android:text="friday"
android:textColor="#000000" />
android:layout_width="wrap_content"
android:layout_height="200dp"
android:layout_weight="1"
android:text="saturday"
android:textColor="#000000" />
android:layout_width="wrap_content"
android:layout_height="200dp"
android:layout_weight="1"
android:text="sunday"
android:textColor="#000000" />
最后是我们的主程调用(PadTestActivity.java):
package com.scrollview.two.ganged;
import com.example.listviewframetst.R;
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);
}
}
}
还有另外一种思路:
public class SyncHorizontalScrollViewextends 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改一下即可:
encoding="utf-8"?>
android:id="@android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#001629">
android:layout_height="wrap_content">
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingTop="95px">
在TabWidget外面加上HorizontalScrollView即可。
不过 这样有个问题 宽度貌似改不了了 即使你只有一个tab 它并不会fill_parent
还是原来的宽度。(试试设置HorizontalScrollView的android:fillViewport="true")
但是 如果很多tab的话 还是好使的。