延迟添加视图

问题描述:

我想在向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秒后同时添加所有视图,对吗? 延迟时间必须像下面那样通过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