ScrollView 監聽當前的高度改變

來源:互聯網
上載者:User

最近在使用ScrollView的時候遇到一些問題。查看API我們很容易發現使用ScrollView的scrollTo(x, y)很容易的使ScrollView定位到某個位置。
但是如果我在ScrollView中包含了一個TextView對象,我通過getScrollY()的方法取得了當前的高度,並儲存了位置。後面就可以使用scrollTo(x, y)定位了。似乎一切都沒有問題。但是,我如果允許使用者設定TextView裡面的文字大小的話,當TextView包含的內容很長的話,你原先儲存的位置和能已經差的很遠了。

這時我們似乎需要一個百分比的位置來解決這個問題。但Google似乎認為現有的方法已經夠了,你們不可能有這麼“複雜”的要求。無奈啊!

既然沒有就自己想辦法啦, 簡單一看,發現ScrollView的對象有一個computeVerticalScrollRange() 方法, 當然這個方法是protected的,你是沒有辦法直接調用的。這就簡單了,建立一個類繼承ScrollView, 建立一個方法,把這個方法設定成public的,在裡面調用computeVerticalScrollRange(), 一切問題都解決了。

?
code
12345678910111213141516171819202122232425262728293031323334353637383940414243 package
com.aoandroid.scrollviewdemo;
      import
android.content.Context;
import
android.util.AttributeSet;
import
android.widget.ScrollView;
  public
class
AOAScrollView extends
ScrollView {       private
ScrollViewListener scrollViewListener = null;
        public
AOAScrollView(Context context, AttributeSet attrs,
int defStyle) {         super(context, attrs, defStyle);
    }
      public
AOAScrollView(Context context, AttributeSet attrs) {         super(context, attrs);
    }
      public
AOAScrollView(Context context) {         super(context);
    }
      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);
        }
    }
          public
int getTotalVerticalScrollRange() {
        return
computeVerticalScrollRange();     }
            }

這時,getTotalVerticalScrollRange()就可以計算出當前總的高度,再結合getScrollY(), 就可以計算出高度的百分比。改變文字大小的時候, 取到新的總高度,乘以百分比就可以定位到原先的位置了。

當然, xml裡面就要這麼調用了

?
code
1234567 <com.aoandroid.scrollviewdemo.AOAScrollView        android:id="@+id/ScrollView"
android:layout_width="fill_parent"        android:layout_height="fill_parent">
        <TextView
android:id="@+id/contentTV"
android:textColor="#000000"
android:textSize="28dip"            android:layout_width="fill_parent"
android:layout_height="wrap_content"            android:text=""
/> </com.aoandroid.scrollviewdemo.AOAScrollView>

附件是一個執行個體代碼。裡面還包含了一個ScrollView改變的時候的監聽動作, 如果你要在使用者拉多ScrollView作出額外動作的時候,可以參考。
建立一個Listener

?
code
123456789101112131415161718192021222324252627282930313233343536373839404142 package
com.aoandroid.scrollviewdemo;
  public
interface
ScrollViewListener {     void
onScrollChanged(AOAScrollView scrollView, int
x, int
y,
int oldx,
int oldy); }       把Listener和AOAScrollView綁定
    package
com.aoandroid.scrollviewdemo;         import
android.app.Activity;
import
android.os.Bundle;
import
android.util.Log;
import
android.widget.TextView;
  public
class
HomeActivity extends
Activity implements
ScrollViewListener {     AOAScrollView scrollView =
null;               public
void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
                  TextView tv = (TextView)
this.findViewById(R.id.contentTV);
        tv.setText(R.string.content);
          scrollView = (AOAScrollView)
this.findViewById(R.id.ScrollView);
        scrollView.setScrollViewListener(this);
    }
      @Override    public
void onScrollChanged(AOAScrollView scrollView,
int x, int
y, int
oldx,
int oldy) {
        // TODO Auto-generated method stub
        Log.i("HomeActivity",
"AOAScrollView Position x, y, oldx, oldy : "
+ x + " , "
+ y +
" , " + oldx +
" , " + oldy +
" total height=" + scrollView.getTotalVerticalScrollRange());
              

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.