2016-11-17 34 views
0

我'設置一個片段:如何使用FragmentTransaction的add()方法使用從右滑動至左側,並完成這項任務I'have創建下面的方法drawerLayout在RelativeLayout的

XML代碼:

<android.support.v4.widget.DrawerLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:facebook="http://schemas.android.com/apk/res-auto" 
xmlns:tools="http://schemas.android.com/tools" 
android:id="@+id/drawer_layout" 
android:layout_width="match_parent" 
android:layout_height="match_parent" > 

<!-- Framelayout to display Fragments --> 

<RelativeLayout 
    android:id="@+id/mainView" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 


</RelativeLayout> 

<!-- Listview to display slider menu --> 

<RelativeLayout 
    android:id="@+id/drawerView" 
    android:layout_width="240dp" 
    android:layout_height="wrap_content" 
    android:layout_gravity="start" > 

    <ListView 
     android:id="@+id/list_slidermenu" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:background="@color/list_background" 
     android:divider="@color/list_divider" 
     android:dividerHeight="1dp" /> 
</RelativeLayout> 

Java代碼:

public class ProfileActivity extends ActionBarActivity { 
.... 
private DrawerLayout mDrawerLayout; 
private ActionBarDrawerToggle mDrawerToggle; 

RelativeLayout drawerView; 
RelativeLayout mainView; 
.... 

@Override 
protected void onCreate(Bundle savedInstanceState) { 


    ............. // 
    .............// 
    drawerView = (RelativeLayout) findViewById(R.id.drawerView); 
    mainView = (RelativeLayout) findViewById(R.id.mainView); 

    mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.drawable.ic_drawer, R.string.app_name, R.string.app_name) { 
     public void onDrawerClosed(View view) { 
      supportInvalidateOptionsMenu(); 
     } 

     public void onDrawerOpened(View drawerView) { 
      supportInvalidateOptionsMenu(); 
     } 

     @Override 
     public void onDrawerSlide(View drawerView, float slideOffset) { 
      super.onDrawerSlide(drawerView, slideOffset); 
      mainView.setTranslationX(slideOffset * drawerView.getWidth()); 
      mDrawerLayout.bringChildToFront(drawerView); 
      mDrawerLayout.requestLayout(); 
     } 
    }; 
    mDrawerLayout.setDrawerListener(mDrawerToggle); 

} 
    } 

所以我想補充Fragme通過代碼在RelativeLayout中nt。

有什麼想法?

回答

1
FragmentManager fm = getFragmentManager(); 
    FragmentTransaction ft = fm.beginTransaction(); 
    View containerView = findViewById(R.id.mainView); 
    ft.add(R.id.mainView, fragment); 
    ft.commit(); 
    fm.executePendingTransactions(); 

你也許可以在你的onCreate()中調用這段代碼。

+0

Thunks它的工作原理,但有一些變化!但它的確激勵了我:D –

相關問題