2014-02-28 65 views
0

我在我的android應用程序中使用smoothScrollTo時遇到了問題smoothScrollTo。當我第一次點擊按鈕時,文本顯示但滾動不起作用,但是當我第二次點擊按鈕時,一切正常。這是我的代碼。有任何想法嗎?謝謝smoothScrollTo無法正常工作

showMoreBt = (Button) findViewById(R.id.buttonMore); 
    showMoreBt.setVisibility(View.VISIBLE); 
    LinearLayout layout = (LinearLayout) findViewById(R.id.animatedLL); 
    layout.setVisibility(View.VISIBLE); 

    scrollView = (ScrollView)findViewById(R.id.scrollView1); 
    showMoreBt.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 

      if (!isShowMore) { 
       isShowMore = true; 
       showMoreBt.setText("Show more"); 
       Animation slideDown = AnimationUtils.loadAnimation(getBaseContext(), R.anim.slide_down);  
       webviewMore.setVisibility(View.VISIBLE); 
       webviewMore.startAnimation(slideDown); 
       focusOnView(); 

      } else { 
       isShowMore = false; 
       showMoreBt.setText("Show less"); 
       webviewMore.setVisibility(View.GONE); 
      } 
     } 
    }); 

這裏是focusOnView方法

private final void focusOnView(){ 
    new Handler().post(new Runnable() { 
     @Override 
     public void run() { 
      int position = webviewTitle.getHeight() + webview.getHeight(); 
      scrollView.smoothScrollTo(0, position); 
     } 
    }); 
} 

,這裏是XML

<LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="vertical" > 

     <WebView 
      android:id="@+id/webview" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" /> 

     <Button 
      android:id="@+id/buttonMore" 
      android:layout_width="fill_parent" 
      android:layout_margin="10dip" 
      android:layout_height="50dip" 
      android:layout_gravity="center" 
      android:textStyle="bold" 
      android:background="@color/light_green" 
      android:drawableLeft="@drawable/arrow_down" 
      android:drawableRight="@drawable/arrow_down" 
      android:visibility="gone" 
      android:text="Show more" /> 


     <LinearLayout 
      android:id="@+id/animatedLL" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:orientation="vertical" > 

      <WebView 
       android:id="@+id/webviewMore" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
      /> 
     </LinearLayout> 

    </LinearLayout> 
</ScrollView> 
+0

什麼是isShowMore變量?它有什麼價值? – Giacomoni

+0

我編輯了我的問題... – Twilie2012

回答

1

的一部分,如果你初始化布爾

isShowMore = false; 

則壽ld按預期工作。

+0

變量isShowMore被初始化爲false ...我不知道問題在哪裏... – Twilie2012

1

我解決我的問題在focusOnView方法的代碼替換代碼:

ViewTreeObserver vto = scrollView.getViewTreeObserver(); 
       vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() { 
         public void onGlobalLayout() { 
          scrollView.scrollTo(0, View.FOCUS_DOWN); 
         } 
       }); 
0

你應該在UI線程做操作。所以你可以在你的UI線程中創建一個Handler而不是另一個線程。在Handler的handleMessage()方法中執行smoothScrollTo()方法。 嘗試這樣的:

private static final int SHOW_MORE = 0X10; 
private static final int SHOW_LESS = 0X20; 

Handler mHandler = new Handler(){ 

    @Override 
    public void handleMessage(Message msg) { 
     switch (msg.what) { 
     case SHOW_LESS: 
      showMoreBt.setText("Show more"); 
      Animation slideDown = AnimationUtils.loadAnimation(getBaseContext(), R.anim.slide_down);  
      webviewMore.setVisibility(View.VISIBLE); 
      webviewMore.startAnimation(slideDown); 
      int position = webviewTitle.getHeight() + webview.getHeight(); 
      scrollView.smoothScrollTo(0, position); 
      break; 

     case SHOW_MORE: 
      showMoreBt.setText("Show less"); 
      webviewMore.setVisibility(View.GONE); 
      break; 

     default: 
      break; 
     } 
    } 

}; 

點擊按鈕:

showMoreBt = (Button) findViewById(R.id.buttonMore); 
showMoreBt.setVisibility(View.VISIBLE); 
LinearLayout layout = (LinearLayout) findViewById(R.id.animatedLL); 
layout.setVisibility(View.VISIBLE); 

scrollView = (ScrollView)findViewById(R.id.scrollView1); 
showMoreBt.setOnClickListener(new OnClickListener() { 

    @Override 
    public void onClick(View v) { 

     if (!isShowMore) { 
      isShowMore = true; 
      mHandler.sendMessage(mHandler.obtainMessage(SHOW_LESS)); 
     } else { 
      isShowMore = false; 
      mHandler.sendMessage(mHandler.obtainMessage(SHOW_MORE)); 
     } 
    } 
});