2014-09-22 67 views
0

我有父RelativeLayout(RL)裏面的ScrollView(SV)。在父母RL中,我還有一個孩子也是RL。我想另一個孩子RL添加到父RL和屏幕上的高度爲什麼smoothScrollTo在加入兄弟之後無法立即生效RelativeLayout

RelativeLayout.LayoutParams lparams = new RelativeLayout.LayoutParams(this.width, 
    this.height); 
lparams.addRule(RelativeLayout.BELOW, first_child.getId()); 
new_layout.setLayoutParams(lparams); 
rl.addView(new_layout); 
int current_pos = sv.getScrollY(); 
sv.smoothScrollTo(0,current_pos + this.height); 

向下滾動新的子RL成功添加到父,但smootScrollTo方法劑量不能正常工作。這就像addView()方法是異步工作,並且當方法smoothScrollTo正在調用,父RL沒有包含新的子RL。

如何在父級添加新的孩子RL並立即向下滾動屏幕?

回答

6

試試這個:

RelativeLayout.LayoutParams lparams = new RelativeLayout.LayoutParams(this.width, 
     this.height); 
    lparams.addRule(RelativeLayout.BELOW, first_child.getId()); 
    new_layout.setLayoutParams(lparams); 
    rl.addView(new_layout); 
    int height = this.height; 
     sv.post(new Runnable() { 
       @Override 
       public void run() { 
        int current_pos = sv.getScrollY(); 
        sv.smoothScrollTo(0,current_pos + height); 
       } 
      }); 
+0

那麼,如果方法post()將消息添加到隊列中,那麼addView是異步方法?如何將數據發送到Runnable對象?在我的情況下,我需要在run()方法中訪問sv?覆蓋calss Runnable? – Raf 2014-09-22 10:49:45

+0

不太明白這個問題。什麼不工作? – user3815165 2014-09-22 13:53:58

+0

如何在run()方法內部獲得對sv或其他變量的訪問? – Raf 2014-09-23 06:03:30

1

下面的代碼將工作:

  final int scrollposition = Math.round(hr/24.0f * 1440f); 

      final ScrollView sv = (ScrollView)findViewById(R.id.graphScrollView); 
      //sv.smoothScrollTo(0, scrollposition); 

      sv.post(new Runnable() { 
       @Override 
       public void run() { 
        sv.smoothScrollTo(0, scrollposition); 
       } 
      }); 

原因: 它將等待滾動視圖運行底層代碼之前公佈。