2012-02-14 128 views
0

我想在向mainview添加視圖時添加延遲,但同時觀看矛。請幫忙。延遲添加視圖

final Handler handler = new Handler(); 
    LinearLayout ll = (LinearLayout)findViewById(R.id.ll); 
    final HorizontalScrollView hsv = new HorizontalScrollView(TestViewActivity.this); 
    final LinearLayout lhsv = new LinearLayout(TestViewActivity.this); 

    final Animation a1 = new AlphaAnimation(0.00f, 1.00f); 
    a1.setDuration(350); 
    a1.setFillAfter(true); 
    for(int k =0; k < 5; k++){ 
     new Handler().postDelayed(new Runnable() { 
       public void run() { 
        //write your code here... 
        TextView tv = new TextView(TestViewActivity.this); 
        tv.setText("Text"); 
        tv.setTextSize(42); 
        tv.setPadding(10, 0, 10, 0); 
        tv.setVisibility(View.INVISIBLE); 
        tv.clearAnimation(); 
        tv.startAnimation(a1); 
        lhsv.addView(tv, temp); 
        temp++; 
       } 
      }, 2000); 
     } 

    hsv.addView(lhsv); 
    ll.addView(hsv); 

temp是靜態int。

回答

2

根據你的代碼,結果可能會在2秒後同時添加所有視圖,對嗎? 延遲時間必須像下面那樣通過int k來改變。

for(int k =0; k < 5; k++){ 
    handler.postDelayed(new Runnable() { 
      public void run() { 
       //write your code here... 
       TextView tv = new TextView(TestViewActivity.this); 
       tv.setText("Text"); 
       tv.setTextSize(42); 
       tv.setPadding(10, 0, 10, 0); 
       tv.setVisibility(View.INVISIBLE); 
       tv.clearAnimation(); 
       tv.startAnimation(a1); 
       lhsv.addView(tv, temp); 
       temp++; 
      } 
     }, 2000 + 2000 * k); 
    } 

我建議不要使處理程序不必要。只需將runnables發佈到一個處理程序。

+0

感謝它的工作....但有一個輕彈的問題,其次你可以提供一個處理程序的後可運行的例子。 – Programmer 2012-02-14 08:37:55

+0

final Handler handler = new Handler();根據這段代碼,你已經構建了一個處理程序。只是發佈到這個處理程序可運行。像這樣,handler.postDelayed(新的Runnable(){...} – lulumeya 2012-02-14 09:05:51

+0

你可以plz指導我關於閃爍的問題,例如以前的textview也消失並回來爲什麼:( – Programmer 2012-02-14 09:14:47