2016-12-14 58 views
0

自从昨天上午以来,我一直在为此而苦苦挣扎。我熟悉C++(只是说我有编程经验)。我正在学习android编程。当我的应用程序启动时,它会启动一个活动。我怀疑它是从导航抽屉回到主页(activity_main)

setContentView(R.layout.activity_main); 

在我的抽屉里,我有3个项目。一个被称为“猪崽子”,这是假设是导航家。 “图形”和“设置”是其他2.我想要它,所以当有人点击导航抽屉中的“存钱罐”时,它会返回到程序启动时加载的原始屏幕。这需要在不创建新活动的情况下完成,主屏幕中的数据非常重要,在返回时不应清除。它应该包含留在那里的数据。

这里是我试过

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

     if (id == R.id.nav_PiggyBank) { 
      setContentView(R.layout.activity_main); 
      Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
      setSupportActionBar(toolbar); 
      // getActionBar().setDisplayHomeAsUpEnabled(true); 

      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.setDrawerListener(toggle); 
      toggle.syncState(); 

      NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view); 
      navigationView.setNavigationItemSelectedListener(this); 
      // ATTENTION: This was auto-generated to implement the App Indexing API. 
      // See https://g.co/AppIndexing/AndroidStudio for more information. 
      client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build(); 

     } else if (id == R.id.nav_Graph) { 
      fragmentManager.beginTransaction() 
        .replace(R.id.content_frame 
          , new Graph()) 
        .commit(); 
     } else if (id == R.id.nav_Settings) { 
      fragmentManager.beginTransaction() 
        .replace(R.id.content_frame 
          , new UserSettings()) 
        .commit(); 
     } 


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

这里是我的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> 
+0

什么在你的activity_main? –

回答

1

你应该把一切的你,如果(ID == R.id.nav_PiggyBank)内{...}在您的onCreate方法的情况下的活动。在if内,你应该告诉FragmentManager用你的PiggyBankFragment替换当前的分段容器(R.id.content_frame)......就像你在其他两个if-else情况下所做的那样;-)

更新:

如果你想使用NavigationDrawer,我建议使用多个片段的内容和一个活动,它包含片段容器和导航抽屉。因此,您的应用程序的结构如下所示: 1.您可以创建包含fragment_container和导航抽屉的PiggyBankActivity。没有其他 - 没有存钱罐的内容 2.您创建一个PiggyBankFragment,其中包含您想要在此“主页”上显示的所有内容 3.您可以在onNavigationItemSelected中使用FragmentManager和FragmentTransactions来替换片段并显示所需的内容。

您可以参考这个文档:https://developer.android.com/training/implementing-navigation/nav-drawer.html 或这个例子 https://www.simplifiedcoding.net/android-navigation-drawer-example-using-fragments/ ;-)

+0

我试过,我得到这个错误(第二个参数下的红线)。错误的第二个参数类型。发现:'com.example.crims.piggybank.MainActivity',要求:'android.app.Fragment'less ... 替换(int,android.app.Fragment) 在FragmentTransaction中无法应用 to(int, com.example.crims.piggybank.MainActivity) – asmcriminal

+0

谢谢,正如我所说的,我是android的新手。我不知道“Fragment_container”是什么......我假设一个片段?更少的经理和交易。但是......我对你在说什么有一个大致的想法,而这正是我想要做的。我会再试一次,你清理了一些东西。 – asmcriminal

+0

(y)...您也可以尝试使用此链接:https://www.simplifiedcoding.net/android-navigation-drawer-example-using-fragments/它提供了您想要实现的详细示例; - ) – jennymo