public class TestTextView extends Activity {
/** Called when the activity is first created. */
LinearLayout lay1,lay2,lay;
private Scroller mScroller;
private boolean s1,s2;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ScrollView s;
mScroller = new Scroller(this);
//HorizontalScrollView hv = new HorizontalScrollView(this);
lay1 = new LinearLayout(this){
@Override
public void computeScroll() {
if (mScroller.computeScrollOffset()) {
// mScrollX = mScroller.getCurrX();
scrollTo(mScroller.getCurrX(), 0);
postInvalidate();
}
}
};
lay2 = new LinearLayout(this){
@Override
public void computeScroll() {
if (mScroller.computeScrollOffset()) {
// mScrollX = mScroller.getCurrX();
scrollTo(mScroller.getCurrX(), 0);
postInvalidate();
}
}
};
lay1.setBackgroundColor(this.getResources().getColor(android.R.color.darker_gray));
lay2.setBackgroundColor(this.getResources().getColor(android.R.color.white));
lay = new LinearLayout(this);
lay.setOrientation(LinearLayout.VERTICAL);
LinearLayout.LayoutParams p0 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,LinearLayout.LayoutParams.FILL_PARENT);
this.setContentView(lay, p0);
LinearLayout.LayoutParams p1 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,LinearLayout.LayoutParams.FILL_PARENT);
p1.weight=1;
lay.addView(lay1,p1);
LinearLayout.LayoutParams p2 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,LinearLayout.LayoutParams.FILL_PARENT);
p2.weight=1;
lay.addView(lay2,p2);
TestButton tx = new TestButton(this);
TestButton tx2 = new TestButton(this);
tx.setText("aaaaaaaaa");
//tx.setBackgroundColor(this.getResources().getColor(android.R.color.black));
tx2.setText("bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb");
tx.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
if(!s1){
mScroller.startScroll(0, 0, 10, 10,10);
s1 = true;
}else{
mScroller.startScroll(0, 0, -10, -10,10);
s1 = false;
}
}
});
tx2.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
if(!s2){
mScroller.startScroll(0, 0, 20, 20,10);
s2=true;
}else{
mScroller.startScroll(20, 20, -20, -20,10);
s2=false;
}
}
});
tx2.setOnKeyListener(new Listener());
lay1.addView(tx);
lay2.addView(tx2);
}
如果点击BUTTON1时,mScroller开始滚动,导致LAY1发生滚动,LAY2不变(背景LAY也不便)
如果点击BUTTON2时,mScroller开始滚动,导致LAY2发生滚动,LAY1不变(背景LAY也不便)
通过这个例子看出
/**
* Called by a parent to request that a child update its values for mScrollX
* and mScrollY if necessary. This will typically be done if the child is
* animating a scroll using a {@link android.widget.Scroller Scroller}
* object.
*/
public void computeScroll()
只有当前LAYOUT中的某个CHILD导致SCROLL发生滚动,才会致使自己的COMPUTESCROLL被调用。
并没有直接的代码指示,哪一个LAYOUT和SCROLLER有联系。
以下理解未考证:
每一个窗口打开时,默认的整个背景LAYOUT长宽与一个虚拟的scroller保持一致,当scroller移动坐标时
layout可以通过计算scroll移动的横向纵向坐标及距离来引发自己的scroll
每一个view都会有scrollto,by方法,通过此方法可以滚动本身view.