簡體   English   中英

Android導航抽屜-片段導航無法正常工作

[英]Android Navigation Drawer - Fragment Navigation not working properly

我設計了導航抽屜應用程序。 除片段導航外,所有其他功能均正常工作。

當我嘗試從側面菜單中選擇一個項目時,它會在activity_main布局的頂部顯示所選的布局。我認為transition.replace()方法無法正常工作,或者無法將當前的布局替換為選定的布局。

這是我的代碼

public class MainActivity extends FragmentActivity {
ActionBarDrawerToggle icon;
final String[] listContent ={"Account Setup","Fragment Two",
"Fragment Three","Main  Page"};
final String[] fragments ={
        "com.strawberryapps.accountsetup.AccountSetup",
        "com.strawberryapps.backupnotes.BackupNotes",
        "com.strawberryapps.notes.AddNote",
        "com.strawberryapps.myapp.MainPage"};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
       ArrayAdapter<String> ad = new ArrayAdapter<String> (getActionBar().getThemedContext(), android.R.layout.simple_list_item_1, listContent);
       final DrawerLayout drawer = (DrawerLayout)findViewById(R.id.drawer_layout);
        final ListView list = (ListView) findViewById(R.id.drawer);
        list.setAdapter(ad);

        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, final int position, long id) {
                drawer.setDrawerListener( new DrawerLayout.SimpleDrawerListener(){
                    @Override
                    public void onDrawerClosed(View drawerView){
                        super.onDrawerClosed(drawerView);
                        android.support.v4.app.FragmentTransaction transition = getSupportFragmentManager().beginTransaction();

                        transition.replace(R.id.main, Fragment.instantiate(MainActivity.this, fragments[position]));

                        transition.commit();
                    }
                });

            }
        });


}

這是我的activity_main.xml

  <android.support.v4.widget.DrawerLayout 
  xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer_layout"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  tools:context=".MainActivity"
  android:background="#FFFFFF"
                                    >
    <FrameLayout
      android:id="@+id/main"
      android:layout_width="match_parent"
      android:layout_height="match_parent" >



     </FrameLayout>

      <ListView
      android:id="@+id/drawer"
      android:layout_width="240dp"
      android:layout_height="match_parent"
      android:layout_gravity="start"
      android:background="#454545"
      android:choiceMode="singleChoice"/>

    </android.support.v4.widget.DrawerLayout>

我正在用Android 4.3開發

請更改片段的布局.xml文件的背景顏色。...在父布局的片段的布局文件中使用此代碼

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_home"
android:background="#FFF"
android:layout_marginTop="50dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin">

</RelativeLayout>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM