LayoutTransition在添加视图onActivityResult()方法时不起作用

时间:2013-01-26 20:40:23

标签: android android-animation

我希望在添加新视图时实现向右移动视图的动画。

我正在使用LayoutTransition.APPEARING的LayoutTransition动画。我在线性布局的第0位添加新视图。

除了从onActivityResult()添加视图的一种情况外,它工作正常。

当我为结果启动Activity并尝试在onActivityResult()方法中添加视图时,视图添加但LayoutTransition动画确实有效。

场景1:活动A为结果启动活动B.活动B setResult()和finish()没有做任何事情。在这种情况下,LayoutTransition动画工作正常。

场景2:活动A为结果启动活动B.活动B有Button。单击按钮活动B setResult()和finish()。在这种情况下,LayoutTransition动画不起作用。

需要输入我出错的地方或实现动画的任何解决方法。

活动A

private LinearLayout ll;
private Button b, addbutton, setResultButton;
private LayoutTransition transitioner;
private Animator defaultAppearingAnim;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.test);

ll = (LinearLayout) findViewById(R.id.ll);

    transitioner = new LayoutTransition();
    defaultAppearingAnim = transitioner
            .getAnimator(LayoutTransition.APPEARING);
    transitioner.setAnimator(LayoutTransition.APPEARING,
            defaultAppearingAnim);
    ll.setLayoutTransition(transitioner);

/*Adds Buttons to Linear Layout*/
    addbutton = (Button) findViewById(R.id.button1);
    addbutton.setOnClickListener(this);

/*Adds Buttons to Linear Layout on onActivityResult method*/
    setResultButton = (Button) findViewById(R.id.button2);
    setResultButton.setOnClickListener(this);


   /*Dummy Button Added here*/
    for (int i = 0; i < 2; i++) {
        Button b = new Button(this);
        b.setText("B " + i);
        ll.addView(b);
    }
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // Result from Activity B we are adding Button to LinearLayout
    super.onActivityResult(requestCode, resultCode, data);
    Button b = new Button(this);
    b.setText("RT");
    ll.addView(b, 0);
}

@Override
public void onClick(View v) {
    if (v == setResultButton) {
        Intent intent = new Intent(this, ResultActivity.class);
        startActivityForResult(intent, 0);
    } else if (v == addbutton) {
        Button b = new Button(this);
        b.setText("T");
        ll.addView(b, 0);
    }
}

活动B

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

  /*   SCENARIO 1: LayoutTransition works if the activity setResult and finish
 setResult(0);  
 finish();  
   */

/* SCENARIO 2: Here the LayoutTransition animation doesnt work*/

    Button setResultButton = (Button) findViewById(R.id.button11);
    setResultButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            setResult(0);
            finish();
        }
    });


}

1 个答案:

答案 0 :(得分:1)

搞定了

方案2的解决方案:

在这种情况下,我使用post()方法

添加了视图
ll.post(new Runnable() {
  @Override
  public void run() {
    // TODO Auto-generated method stub
    Button b = new Button(ActivityA.this);
    b.setText("RT");
    ll.addView(b, 0);
 }
});