2016-05-31 59 views
1

我已经发现了很多关于此问题的内容,但我确实无法解决我的问题。我在这里花了几个小时,但没有。无法从导航抽屉项目加载片段

我有一个使用Android Studio 2.1.1模板制作的导航抽屉活动。当然,我想要改变我的应用的视图,当我点击菜单中的一个项目时,显示不同的碎片。这是我在MainActivity代码:

 public boolean onNavigationItemSelected(MenuItem item) { 
     // Handle navigation view item clicks here. 
     int id = item.getItemId(); 

     if (id == R.id.nav_camera) { 

      Context context = getApplicationContext(); 
      CharSequence text = "Hello toast!"; 
      int duration = Toast.LENGTH_SHORT; 

      Toast toast = Toast.makeText(context, text, duration); 
      toast.show(); 

      new gaussFragment(); 

     } else if (id == R.id.nav_gallery) { 

     } else if (id == R.id.nav_slideshow) { 

     } else if (id == R.id.nav_manage) { 

     } else if (id == R.id.nav_share) { 

     } else if (id == R.id.nav_send) { 

     } 

     DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
     drawer.closeDrawer(GravityCompat.START); 
     return true; 
    } 

当我点击抽屉式导航栏(在一个id为R.id.nav_camera)的第一个项目,我可以看到的烤面包,但新的片段没有出现。我使用这个代码在gaussFragment()

public class gaussFragment extends Fragment { 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     // Inflate the layout for this fragment 
     return inflater.inflate(R.layout.fragment_gauss, container, false); 
    } 

} 

当然,你可以看到here的fragment_gauss.xml是有这个代码,它的相对布局:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context="info.androidhive.tabsswipexx.gaussFragment"> 

    <!-- TODO: Update blank fragment layout --> 
    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:text="@string/hello_blank_fragment" /> 

</FrameLayout> 

我怎么能解决这个问题?


content_main是我在应用程序启动时看到的第一个东西。我应该尝试添加一些像

<FrameLayout 
     android:id="@+id/content_frame" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" /> 

并以某种方式加载我的碎片?

+0

好选择每次都被显示出来,你犯了一个新的片段d它立即被垃圾收集......你需要一个FragmentTransaction来将它加载到FrameLayout中 –

+0

我怎么能编码它? –

+0

看看[文档](https://developer.android.com/training/basics/fragments/fragment-ui.html)是否有帮助,然后再回答问题。 –

回答

2

如果您使用由Android工作室在导航抽屉主要活动提供XML然后抽屉式导航模板,你会发现这样的:

<?xml version="1.0" encoding="utf-8"?> 
<android.support.v4.widget.DrawerLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="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" 
    android:fitsSystemWindows="true" 
    tools:openDrawer="start"> 

    <!-- The main content view --> 
    <include layout="@layout/app_bar_main" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" /> 

    <!-- the navigation drawer the comes from the left--> 
    <android.support.design.widget.NavigationView 
     android:id="@+id/nav_view" 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:layout_gravity="start" 
     android:fitsSystemWindows="true" 

     app:headerLayout="@layout/nav_header_main" 
     app:menu="@menu/activity_main_drawer" /> 

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

如果你仔细看看那里你会看到它包括我为你评论的两个主要部分。一个是导航抽屉的东西,另一个是主要内容。如果按Ctrl +点击layout="@layout/app_bar_main"你会看到另一个XML打开是以下几点:

<?xml version="1.0" encoding="utf-8"?> 
<android.support.design.widget.CoordinatorLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:fitsSystemWindows="true" 
    tools:context="copsonic.com.SoundExchange.demoApp.MainActivity"> 

    <!--Main View tool bar--> 
    <android.support.design.widget.AppBarLayout 
     android:layout_height="wrap_content" 
     android:layout_width="match_parent" 
     android:theme="@style/AppTheme.AppBarOverlay"> 

     <android.support.v7.widget.Toolbar 
      android:id="@+id/toolbar" 
      android:layout_width="match_parent" 
      android:layout_height="?attr/actionBarSize" 
      android:background="?attr/colorPrimary" 
      app:popupTheme="@style/AppTheme.PopupOverlay" /> 

    </android.support.design.widget.AppBarLayout> 

    <!--The content that will be displayed in the main view when the navigation drawer is not opened--> 
    <include layout="@layout/content_main" /> 

    <!--Floating Action Button Stuff--> 
    <android.support.design.widget.FloatingActionButton 
     android:id="@+id/fab" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="bottom|end" 
     android:layout_margin="@dimen/fab_margin" 
     android:scaleType="center" 
     android:src="@mipmap/ic_rx_tx" 
     android:saveEnabled="false" 
     app:backgroundTint="#000000" 
     android:adjustViewBounds="false" /> 



</android.support.design.widget.CoordinatorLayout> 

在那里你会看到3个主要部分,一个工具栏,一个浮动操作按钮和控制最后一个用于导航抽屉未打开时将显示在主视图中的内容,这是您关心的内容。如果按Ctrl +单击<include layout="@layout/content_main" />您将看到第三个xml,您必须添加一个Fragmet容器。到底这件事情是这样的:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" 
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    app:layout_behavior="@string/appbar_scrolling_view_behavior" 
    tools:showIn="@layout/app_bar_main" 
    tools:context="copsonic.com.SoundExchange.demoApp.MainActivity"> 

    <!-- A fragmet container to dynamically swap between fragmets--> 
    <FrameLayout 
     android:id="@+id/fragment_container" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" /> 

</RelativeLayout> 

所以这一切后,从您的主要活动代码,你只需要做两两件事,一是管理哪些片段将被首先看到的,您在OnCreate方法做到这一点通过添加:

FragmentTransmitter initialFragment = new FragmentTransmitter(); 


// Check that the activity is using the layout version with 
// the fragment_container FrameLayout 
if (findViewById(R.id.fragment_container) != null) { 

     // this check allows you not to load the default fragment unless 
     //it's the first time you launch the activity after having destroyed it 
     if (savedInstanceState != null) { 
      return; 
     } 

    // Create the fragment that is seen the first time your app opens 

    //pass useful info to the fragment from main activity if needed 
    //(it is recommended not to do this in the constructor, so you have to do it through a Bundle) 
    Bundle args = new Bundle(); 
    args.putSerializable("ModulationType",aModulation); 
    initialFragment.setArguments(args); 


    // In case this activity was started with special instructions from an 
    // Intent, pass the Intent's extras to the fragment as arguments 
    //initialFragment.setArguments(getIntent().getExtras()); 

    // Add the fragment to the 'fragment_container' FrameLayout 
    getSupportFragmentManager().beginTransaction().add(R.id.fragment_container, initialFragment).commit(); 

} 

然后第二件事是妥善处理该片段将一个选项是在导航抽屉

public boolean onNavigationItemSelected(MenuItem item) { 
     // Handle navigation view item clicks here. 
     int id = item.getItemId(); 

     if (id == R.id.nav_camera) { 

      Context context = getApplicationContext(); 
      CharSequence text = "Hello toast!"; 
      int duration = Toast.LENGTH_SHORT; 

      Toast toast = Toast.makeText(context, text, duration); 
      toast.show(); 

      FragmentTransmitter Gauss= new new gaussFragment(); 

      //pass useful info to fragment if needed 
      Bundle args = new Bundle(); 
      args.putSerializable(getString(R.string.ModulationType),aModulation); 
      Gauss.setArguments(args); 

      FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); 
      transaction.replace(R.id.fragment_container, Gauss,"TAG_GaussFragment"); //pacing a tag is not mandatory but it is pretty useful if you want to know later which fragment is being displayed in the fragment container 
      //transaction.addToBackStack(null); 
      transaction.commit(); 



     } else if (id == R.id.nav_gallery) { 

     } else if (id == R.id.nav_slideshow) { 

     } else if (id == R.id.nav_manage) { 

     } else if (id == R.id.nav_share) { 

     } else if (id == R.id.nav_send) { 

     } 

     DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
     drawer.closeDrawer(GravityCompat.START); 
     return true; 
    }