2016-05-31 5 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; 
    } 

Когда я нажимаю на первый пункт навигационной панели (тот, с идентификатором 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> 

Как я мог решить эту проблему?


Содержание_main - это первое, что я вижу, когда приложение запускается. Должен ли я попытаться добавить что-то вроде

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

и как-то загружать там мои фрагменты?

+0

Ok, вы сделали новый фрагментировать d он сразу же собрал мусор ... Вам нужно FragmentTransaction, чтобы загрузить его в FrameLayout –

+0

Как мне его закодировать? –

+0

Посмотрите, поможет ли [документация] (https://developer.android.com/training/basics/fragments/fragment-ui.html), а затем вернется с вопросами. –

ответ

2

Если вы используете шаблон навигации выдвижного ящичка на Android Studio, то в панели навигации основной деятельности 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> 

Если приглядеться там вы увидите что он состоит из двух основных частей, которые я прокомментировал для вас. Один из них относится к материалу навигационного ящика, а другой - к основному содержимому wiew. Если 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 основных части, одна для панели инструментов, один для плавучего управления Button Действия и наконец, один для контента, который будет отображаться в главном окне, когда ящик навигации не открывается, это тот, о котором вы заботитесь. Если вы ctrl + click в <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; 
    }