2016-05-06 201 views
0

我创建了一个新的Android Studio项目,我的MainActivity是一个导航抽屉活动。导航抽屉不显示片段

因此,我无法显示片段。我在互联网上阅读过很多文章,也在这里阅读。

解释:

我打开导航抽屉,选择菜单“Podcast”。 PodcastsFragment应该显示,但它仍然显示活动。

MainActivity

enter image description here

MainActivity代码:

import android.os.Bundle; 
import android.support.v4.app.Fragment; 
import android.support.v4.app.FragmentManager; 
import android.support.v4.app.FragmentTransaction; 
import android.util.Log; 
import android.support.design.widget.NavigationView; 
import android.support.v4.view.GravityCompat; 
import android.support.v4.widget.DrawerLayout; 
import android.support.v7.app.ActionBarDrawerToggle; 
import android.support.v7.app.AppCompatActivity; 
import android.support.v7.widget.Toolbar; 
import android.view.Menu; 
import android.view.MenuItem; 

import com.google.android.gms.analytics.HitBuilders; 
import com.google.android.gms.analytics.Tracker; 

public class MainActivity extends AppCompatActivity 
     implements NavigationView.OnNavigationItemSelectedListener { 

    private Tracker mTracker; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
     setSupportActionBar(toolbar); 

     // [START shared_tracker] 
     // Obtain the shared Tracker instance. 
     AnalyticsApplication application = (AnalyticsApplication) getApplication(); 
     mTracker = application.getDefaultTracker(); 
     // [END shared_tracker] 

     DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
     ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
      this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close); 
     drawer.addDrawerListener(toggle); 
     toggle.syncState(); 

     NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view); 
     navigationView.setNavigationItemSelectedListener(this); 


    } 

    @Override 
    protected void onResume() { 
     Log.i("TESTE", "Setting screen name: " + "teste"); 
     mTracker.setScreenName("Main " + "teste"); 
     mTracker.send(new HitBuilders.ScreenViewBuilder().build()); 
     super.onResume(); 
    } 

    @Override 
    public void onBackPressed() { 
     DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
     if (drawer.isDrawerOpen(GravityCompat.START)) { 
      drawer.closeDrawer(GravityCompat.START); 
     } else { 
      super.onBackPressed(); 
     } 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.main, menu); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     // Handle action bar item clicks here. The action bar will 
     // automatically handle clicks on the Home/Up button, so long 
     // as you specify a parent activity in AndroidManifest.xml. 
     int id = item.getItemId(); 

     //noinspection SimplifiableIfStatement 
     if (id == R.id.action_settings) { 
      return true; 
     } 

     return super.onOptionsItemSelected(item); 
    } 

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

     Fragment fragment = null; 
     FragmentTransaction ft = null; 




     if (id == R.id.nav_podcasts) { 
      Log.d("Clicked:", "nav_podcasts"); 
      fragment = new PodcastsFragment(); 
     } else if (id == R.id.nav_feed) { 
      //fragment = new FeedFragment(); 
      Log.d("Clicked:", "nav_feed"); 
      //fragment = new FeedFragment(); 
      FragmentManager fragmentManager = getSupportFragmentManager(); 
      FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); 
      FeedFragment ff = new FeedFragment(); 
      fragmentTransaction.add(R.id.container_body, ff); 
      fragmentTransaction.commit(); 
     } else if (id == R.id.nav_downloads) { 
      //fragment = new DownloadsFragment(); 
      Log.d("Clicked:", "nav_downloads"); 
     } else if (id == R.id.nav_add) { 
      //fragment = new OPMLFragment(); 
      Log.d("Clicked:", "nav_add"); 
     } else if (id == R.id.nav_settings) { 
      //fragment = new SettingsFragment(); 
      Log.d("Clicked:", "nav_settings"); 
     } 

     if (fragment != null) { 
      FragmentManager fragmentManager = getSupportFragmentManager(); 
      fragmentManager.beginTransaction() 
        .replace(R.id.container_body, fragment).commit(); 
     } 

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

PodcastsFragment代码:

import android.content.Context; 
import android.os.Bundle; 
import android.support.v4.app.Fragment; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 

public class PodcastsFragment extends Fragment { 

    public PodcastsFragment() { 
     // Required empty public constructor 
    } 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
    } 

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

     View rootView = inflater.inflate(R.layout.fragment_podcasts, container, false); 

     // Inflate the layout for this fragment 
     return rootView; 
    } 

    @Override 
    public void onAttach(Context context) { 
     super.onAttach(context); 
    } 

    @Override 
    public void onDetach() { 
     super.onDetach(); 
    } 
} 

app_bar_main代码:

<?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="com.test.test.MainActivity"> 

    <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> 

    <include layout="@layout/content_main"/> 



    <!--<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:src="@android:drawable/ic_dialog_email" />--> 

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

activity_main代码:

<?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"> 

    <include 
     layout="@layout/app_bar_main" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" /> 

    <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> 

content_main代码:

<?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="com.test.test.MainActivity"> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="teste"/> 
    <FrameLayout 
     android:id="@+id/container_body" 
     android:layout_width="fill_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1"/> 

</RelativeLayout> 

Log "Clicked

+0

您没有ID为'container_body'的'ViewGroup'来将'Fragment'保存在你发布的布局中。 –

+0

嗨,thx的帮助。我添加了一个我忘记上传的新布局。 – Alan

+0

您需要更具体地了解哪些功能无法正常工作,以及代码中的哪些地方出现故障。 –

回答

1

在您的活动中创建抽屉的列表并添加setOnItemClickListener()。当用户在抽屉列表中选择一个项目时,系统会调用onItemClickListener onItemClickListener给setOnItemClickListener()。

你在onItemClick()方法中做什么取决于你如何实现你的应用程序结构。在以下示例中,在列表中选择的每个项目中插入一个不同的片段插入到主要内容视图(由R.id.content_frame ID标识的的FrameLayout元素): 更多delail请阅读这些文章youtubegithub example