【问题标题】:Dynamically update android ui for an animation为动画动态更新 android ui
【发布时间】:2013-11-28 08:15:32
【问题描述】:

我通过引用this article 实现了布局更新动画。当我单击按钮 ui 更新时,它工作正常。现在我想自动化这个过程。这意味着自动更新ui。我在 for 循环中使用了处理程序来定期更新 ui,如下所示

for(int i = 0; i < 100; i++){
        Handler handler = new Handler();
        handler.postDelayed(new Runnable() {

            @Override
            public void run() {
                final ViewGroup newView = (ViewGroup) LayoutInflater.from(getApplicationContext()).inflate(
                        R.layout.list_item, containerGroup, false);
                TextView tv = (TextView) newView.findViewById(android.R.id.text1);
                tv.setText(RAVEMSGS[(int) (Math.random()*RAVEMSGS.length)]);
                containerGroup.addView(newView,0);

            }
        },2000);
    }

但这不起作用。如何为动画动态更新 ui?

注意:我只发布了我用来更新列表视图的代码的一部分,如果我使用按钮的 onclick 列表器调用它,这段代码就可以工作。

谢谢。

【问题讨论】:

    标签: android android-layout android-intent android-listview


    【解决方案1】:

    您可以尝试从 runOnUiThread 更新 Ui........

    try {
                     // code runs in a thread
                     runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                // your code for animation
                            }
                     });
               } catch (final Exception ex) {
                   Log.i("---","Exception in thread");
               }
    

    【讨论】:

      【解决方案2】:

      您是否在线程中编写了此代码?试试这个:

              // main thread
              final Handler handler = new Handler();
              new Thread(new Runnable(){
      
                  @Override
                  public void run() {
                      // TODO Auto-generated method stub
                      for(int i = 0; i < 100; i++){
      
                          handler.post(new Runnable() {
      
                              @Override
                              public void run() {
                                  final ViewGroup newView = (ViewGroup) LayoutInflater.from(getApplicationContext()).inflate(
                                          R.layout.list_item, containerGroup, false);
                                  TextView tv = (TextView) newView.findViewById(android.R.id.text1);
                                  tv.setText(RAVEMSGS[(int) (Math.random()*RAVEMSGS.length)]);
                                  containerGroup.addView(newView,0);
      
                              }
                          });
      
                          try {
                              Thread.sleep(2000);
                          } catch (InterruptedException e) {
                              // TODO Auto-generated catch block
                              e.printStackTrace();
                          }
                      }
                  }
      
              }).start();
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多