动画片段是错误的

时间:2014-01-23 16:10:49

标签: android android-fragments android-animation

我希望片段在创建活动后输入,而不是输入togheter

我的代码

import android.os.Bundle;
import android.app.Fragment;
import android.app.FragmentTransaction;

public class TranslucentActivity extends Activity {
 //....


@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.translucent);
    }

    @Override
protected void onResume() {
    // TODO Auto-generated method stub

    android.app.Fragment fragment = new ViewDetail();
    android.app.FragmentManager fragmentManager = getFragmentManager(); 
    android.app.FragmentTransaction ft = fragmentManager.beginTransaction(); 
    //ft.setCustomAnimations(R.anim.trans_left_in,R.anim.trans_left_out);
     ft.setCustomAnimations(R.animator.trans_left_in,R.animator.trans_left_out);// now work but is wrong the animation because enter with the activity
    ft.addToBackStack(null); 
    ft.replace(R.id.container, fragment); 
    ft.commit();
    fragmentManager.executePendingTransactions();
    super.onResume();
}

 }

trans_left_in.xml

  <set xmlns:android="http://schemas.android.com/apk/res/android"> 
     <translate 
        android:fromXDelta="100%p" 
        android:toXDelta="0" 
        android:duration="@android:integer/config_longAnimTime"/>
  </set>

trans_left_out.xml

  <set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate
       android:fromXDelta="0" 
       android:toXDelta="100%p" 
       android:duration="@android:integer/config_longAnimTime"/>
  </set>

1 个答案:

答案 0 :(得分:0)

我找到了这个解决方案。我放了一个计时器,我改变了方法

private Fragment fragment;
private FrameLayout frame;
private android.app.FragmentTransaction ft;
private android.app.FragmentManager fragmentManager;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.translucent);

        frame =(FrameLayout)findViewById(R.id.container);
  }

@Override
public void onWindowFocusChanged(boolean hasFocus) {
    // TODO Auto-generated method stub
    super.onWindowFocusChanged(hasFocus);
            frame.postDelayed(new Runnable() {

        @Override
            public void run() {
                // TODO Auto-generated method stub
           fragment = new ViewDettaglioCliente();
           fragmentManager = getFragmentManager(); 
           ft = fragmentManager.beginTransaction(); 
           ft.setCustomAnimations(R.animator.trans_left_in,R.animator.trans_left_out);
           ft.addToBackStack(null); 
           ft.replace(R.id.container, fragment); 
           ft.commit();


            }
        }, 500);
}