2012-07-23 63 views
1

我有一个应用程序,当用户单击一个表,它会更改布局。 下面是代码如何添加布局之间的转换(不是活动)

<TableRow 
       android:id="@+id/tableRow1" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:background="@drawable/cell_table" 
       android:onClick="goAir" > 

而为的onClick方法:

public void goAir(View view){ 
    setContentView(R.layout.va_air); 
} 

如何添加布局变化之间的过渡。像幻灯片一样。

谢谢

回答

2

有多种方法可以做到这一点。我更喜欢使用片段。

/** 
* Adding given Fragment into content area without transition. 
* This is good for initializing view. 
* @param f 
*/ 
private void addContentView(Fragment f){ 
    FragmentTransaction ft =getSupportFragmentManager().beginTransaction(); 
    ft.add(R.id.contentPane, f).commit(); 
} 

/** 
* Replacing Fragment in content area with given Fragment 
* @param f Fragment to display 
* @param tag String of the content area 
* @param animIn Resource ID for new screen transition. 
* @param animOut Resource ID for old screen transition. 
*/ 
private void replaceContentView(Fragment f, String tag, int animIn, int animOut){ 
    // -1 is passed when I want to use default animation. 
    if(animIn == -1) animIn = R.anim.fragment_slide_left_enter; 
    if(animOut == -1) animOut = R.anim.fragment_slide_left_exit; 
    FragmentTransaction ft =getSupportFragmentManager().beginTransaction(); 
    ft.setCustomAnimations(animIn, animOut); // Animate new view in and existing view out 

    ft.replace(R.id.contentPane, f, tag); // id of the FrameLayout to put the Fragment in 
    ft.commit(); 
}