2013-04-26 45 views
1

我想每一個之間添加4×ImageView用1秒delay, 我的代碼是:添加視圖編程方式與延遲

final LinearLayout ll = (LinearLayout) findViewById(R.id.container); 
    for (int j=0;j<4;j++){ 
     final ImageView newIMG = new ImageView(this); 
     newIMG.setLayoutParams(
         new LayoutParams(LayoutParams.WRAP_CONTENT, 
             LayoutParams.MATCH_PARENT, 
             1.0f)); 
     newIMG.setImageResource(R.drawable.new_image); 
     Handler handler = new Handler(); 
     handler.postDelayed(new Runnable() { 
      public void run() { 
        ll.addView(newIMG); 
      } 
     }, 1000); 
    } 

但我得到的是 - 它等待秒,然後display所有ImageViews一次... (我希望他們表現出了一個又一個)

+0

addView是在單獨的線程上運行。它是非阻塞的。你需要在你的'run()'中引入延遲。 – 2013-04-26 11:45:57

回答

1

嘗試:

final LinearLayout ll = (LinearLayout) findViewById(R.id.container); 
for (int j=0;j<4;j++){ 
    final ImageView newIMG = new ImageView(this); 
    newIMG.setLayoutParams(
        new LayoutParams(LayoutParams.WRAP_CONTENT, 
            LayoutParams.MATCH_PARENT, 
            1.0f)); 
    newIMG.setImageResource(R.drawable.new_image); 
    Handler handler = new Handler(); 
    handler.postDelayed(new Runnable() { 
     public void run() { 
       ll.addView(newIMG); 
     } 
    }, j*1000 + 1000); 
} 
0

試試這個:

int id = 999; 
    final LinearLayout ll = (LinearLayout) findViewById(R.id.LinearLayout1); 
     for (int j=0;j<4;j++){ 
      final ImageView newIMG = new ImageView(this); 

      newIMG.setImageResource(R.drawable.ic_launcher); 
      newIMG.setId(id); 
      RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT); 
      params.setMargins(0, 10, 0, 0); 
      params.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE); 

      if(j > 0) { 
       params.addRule(RelativeLayout.BELOW, id - j); 
      } 

      Handler handler = new Handler(); 
      handler.postDelayed(new Runnable() { 
       public void run() { 
         ll.addView(newIMG); 
       } 
      }, (j+1)*1000); 
      newIMG.setLayoutParams(params); 
      id++; 
     } 
0

如果你不想有很多交錯延遲同時運行的線程,你可以做類似如下:

public class MainActivity extends Activity { 

    private int viewsAddedCount = 0; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     Button b = (Button)findViewById(R.id.button1); 
     b.setOnClickListener(new OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       v.setEnabled(false); 
       Thread thread = new delayedViewAdditionThread(); 
       thread.start(); 
      }   
     }); 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.main, menu); 
     return true; 
    } 

    public class delayedViewAdditionThread extends Thread 
    { 
     private android.os.Handler handler = new android.os.Handler() 
     { 
      @Override    
      public void handleMessage(android.os.Message msg) 
      {     
       switch(msg.what) 
       { 
        case 0 : 
         addTextView(); 
         break; 
        case 1 : 
         Button b = (Button)findViewById(R.id.button1); 
         b.setEnabled(true); 
         break; 
        default : 
         break; 
       } 
      } 
     }; 
     @Override 
     public void run() 
     { 
      for(int i = 0; i < 4; i++) 
      { 
       try 
       { 
        Thread.sleep(1000); 
       } 
       catch (InterruptedException e) 
       { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       }   
       handler.sendEmptyMessage(0);     
      } 
      handler.sendEmptyMessage(1); 
     } 

     private void addTextView() 
     { 
      TextView tv = new TextView(MainActivity.this); 
      tv.setText("TextView No. " + String.valueOf(viewsAddedCount + 1)); 
      tv.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT)); 

      LinearLayout ll = (LinearLayout)findViewById(R.id.expandingLayout); 
      ll.addView(tv); 

      viewsAddedCount++; 
     } 
    } 

}