2017-04-07 59 views
1

我在我的應用程序中使用NaivgationView,當我從抽屜中選擇項目時,我設置的片段視圖不顯示。我還添加了吐司它顯示,而我選擇該項目可以幫助我這個嗎?如何將片段附加到Navigationview

public class MainActivity extends AppCompatActivity 
     { 

    private DrawerLayout drawerLayout; 


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

     NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view); 
     //Setting Navigation View Item Selected Listener to handle the item click of the navigation menu 
     navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() { 

      // This method will trigger on item Click of navigation menu 
      @Override 
      public boolean onNavigationItemSelected(MenuItem menuItem) { 

       FragmentManager fragmentManager = getFragmentManager(); 

       //Checking if the item is in checked state or not, if not make it in checked state 
       if(menuItem.isChecked()) menuItem.setChecked(false); 
       else menuItem.setChecked(true); 

       //Closing drawer on item click 
       drawerLayout.closeDrawers(); 

       //Check to see which item was being clicked and perform appropriate action 
       switch (menuItem.getItemId()){ 


        //Replacing the main content with ContentFragment Which is our Inbox View; 
        case R.id.home: 
         Toast.makeText(getApplicationContext(),"Inbox Selected", Toast.LENGTH_SHORT).show(); 

         return true; 

        // For rest of the options we just show a toast on click 

        case R.id.be_the_donor: 
         Toast.makeText(getApplicationContext(),"Stared Selected",Toast.LENGTH_SHORT).show(); 
         RegisterDonor frag = new RegisterDonor(); 

         // update the main content by replacing fragments 
         fragmentManager.beginTransaction() 
           .replace(R.id.frame_container, frag) 
           .setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN) 
           .addToBackStack(null) 
           .commit(); 
         return true; 
        case R.id.search_donor: 
         Toast.makeText(getApplicationContext(),"Send Selected",Toast.LENGTH_SHORT).show(); 
         return true; 
        case R.id.tools: 
         Toast.makeText(getApplicationContext(),"Drafts Selected",Toast.LENGTH_SHORT).show(); 
         return true; 

        default: 
         Toast.makeText(getApplicationContext(),"Somethings Wrong",Toast.LENGTH_SHORT).show(); 
         return true; 

       } 
      } 
     }); 

     // Initializing Drawer Layout and ActionBarToggle 
     drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); 
     ActionBarDrawerToggle actionBarDrawerToggle = new ActionBarDrawerToggle(this,drawerLayout,toolbar,R.string.navigation_drawer_open, R.string.navigation_drawer_close){ 

      @Override 
      public void onDrawerClosed(View drawerView) { 
       // Code here will be triggered once the drawer closes as we dont want anything to happen so we leave this blank 
       super.onDrawerClosed(drawerView); 
      } 

      @Override 
      public void onDrawerOpened(View drawerView) { 
       // Code here will be triggered once the drawer open as we dont want anything to happen so we leave this blank 

       super.onDrawerOpened(drawerView); 
      } 
     }; 

     //Setting the actionbarToggle to drawer layout 
     drawerLayout.setDrawerListener(actionBarDrawerToggle); 

     //calling sync state is necessay or else your hamburger icon wont show up 
     actionBarDrawerToggle.syncState(); 

    } 

    @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); 
    } 


} 

片段

public class RegisterDonor extends Fragment{ 



    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     View rootView = inflater.inflate(R.layout.register_donor, container, false); 
     return rootView; 
    } 
} 

activity_main.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"> 

    <LinearLayout 
     android:layout_height="match_parent" 
     android:layout_width="match_parent" 
     android:orientation="vertical" 
     > 
     <include 
      layout="@layout/app_bar_main" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" /> 
     <FrameLayout 
      android:id="@+id/frame_container" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent"> 

     </FrameLayout> 

    </LinearLayout> 



    <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

<?xml version="1.0" encoding="utf-8"?> 
<android.support.constraint.ConstraintLayout 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" 
    app:layout_behavior="@string/appbar_scrolling_view_behavior" 
    tools:context="com.example.dayshift.bloodfinder.MainActivity" 
    tools:showIn="@layout/app_bar_main"> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Hello World!" 
     app:layout_constraintBottom_toBottomOf="parent" 
     app:layout_constraintLeft_toLeftOf="parent" 
     app:layout_constraintRight_toRightOf="parent" 
     app:layout_constraintTop_toTopOf="parent" /> 

</android.support.constraint.ConstraintLayout> 

register_donor

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <android.support.design.widget.TextInputLayout 
     android:id="@+id/usernameWrapper" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"> 

     <EditText 
      android:id="@+id/register_donor_username" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:inputType="textEmailAddress" 
      android:hint="Enter Name"/> 

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

    <android.support.design.widget.TextInputLayout 
     android:id="@+id/mobileWrapper" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"> 

     <EditText 
      android:id="@+id/register_donor_mobile" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:inputType="number" 
      android:hint="Enter Mobile"/> 

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

    <com.weiwangcn.betterspinner.library.material.MaterialBetterSpinner 
     android:id="@+id/register_donor_bloodgroup" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:hint="Material Design Spinner" 
     android:textColorHint="#05ab9a" 
     app:met_floatingLabel="normal" /> 

    <com.weiwangcn.betterspinner.library.material.MaterialBetterSpinner 
     android:id="@+id/register_donor_city" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:hint="Material Design Spinner" 
     android:textColorHint="#05ab9a" 
     app:met_floatingLabel="normal" /> 

    <AutoCompleteTextView 
     android:id="@+id/register_donor_area" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:ems="10" 
     android:text=""> 
     <requestFocus /> 
    </AutoCompleteTextView> 


</LinearLayout> 
+0

請出示RegisterDonor片段。 –

+0

@KaveeshKanwal查詢 – chris

+0

您確定id'be_the_donor'是正確的嗎? –

回答

1

您的應用程序酒吧佔據整個屏幕。

<include 
     layout="@layout/app_bar_main" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" /> 
    <FrameLayout 
     android:id="@+id/frame_container" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 

    </FrameLayout> 

更改應用程序欄包裹內容上的高度

-1

你告訴了祝酒詞都出現上升,但並不是片段,所以這個問題可能是FragmentManager

檢查你fragmentManager的進口應該是

import android.support.v4.app.FragmentManager; 

,不應該

import android.app.FragmentManager; 

而你的碎片交易應該是這樣的

FragmentManager fragmentManager = getSupportFragmentManager(); 
RegisterDonor frag = new RegisterDonor(); 

fragmentManager.beginTransaction() 
          .replace(R.id.frame_container, frag , "") 
          .setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN) 
          .addToBackStack(null) 
          .commit(); 

希望這能幫到你!

+1

仍然是相同的問題 – chris

+0

嘗試初始化您的DrawerLayout與其相應的ID之前,聽衆 – Jagan

+0

也檢查您的RegisterDonor中的片段的導入 – Jagan

0

在activity_main_drawer寫有編號和標題您的項目之後在方法mainactivity onNavigationItemSelected爲您的ID聲明的片段,並承諾

-1

試試這個方式,我想它正常工作:

navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() { 
     @Override 
     public boolean onNavigationItemSelected(MenuItem menuItem) { 

      FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction(); 
      int id = menuItem.getItemId(); 

      switch (id) { 
       case R.id.be_the_donor: 
        Fragment registerDonor = new RegisterDonor(); 
        fragmentTransaction.replace(R.id.frame_container, registerDonor); 
        fragmentTransaction.commit(); 
        drawerLayout.closeDrawers(); 
        break;