2017-09-25 145 views
0

我使用的是默認抽屜式導航在Android Studio中的默認抽屜式導航模板按鈕,並添加兩個片段的前兩個按鈕,導航抽屜裏。一切正常,我可以在抽屜菜單中切換這些片段。 但我想有一個簡單的按鈕第一個片段,點擊它,我帶到第二個片段。點擊這個按鈕打開第二個片段,但它似乎重疊整個屏幕並消失導航視圖。 這是我的項目的代碼,請大家幫幫我:與Android Studio中

MainActivity.java:

package com.example.arantik.test4; 

import android.os.Bundle; 
import android.support.design.widget.FloatingActionButton; 
import android.support.design.widget.Snackbar; 
import android.support.v4.app.Fragment; 
import android.view.View; 
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; 

public class MainActivity extends AppCompatActivity 
     implements NavigationView.OnNavigationItemSelectedListener { 

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

     FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); 
     fab.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG) 
         .setAction("Action", null).show(); 
      } 
     }); 

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

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

     if (id == R.id.nav_camera) { 
      fragment=new Fragment_1(); 
     } else if (id == R.id.nav_gallery) { 
      fragment=new Fragment_2(); 
     } 

     android.support.v4.app.FragmentTransaction transaction=getSupportFragmentManager().beginTransaction(); 
     transaction.replace(R.id.mainFrame, fragment); 
     transaction.addToBackStack(null); 
     transaction.commit(); 

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

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

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

<?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.arantik.test4.MainActivity" 
    tools:showIn="@layout/app_bar_main"> 

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

</android.support.constraint.ConstraintLayout> 

Fragment_1.java:

package com.example.arantik.test4; 

import android.support.v4.app.Fragment; 
import android.os.Bundle; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.Button; 

public class Fragment_1 extends Fragment { 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 

     View view=inflater.inflate(R.layout.fragment_1_layout, container, false); 

     Button button=(Button)view.findViewById(R.id.button); 
     button.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       android.support.v4.app.FragmentManager fragmentManager = getFragmentManager(); 
       android.support.v4.app.FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); 
       Fragment_2 fragment_2=new Fragment_2(); 
       fragmentTransaction.replace(android.R.id.content, fragment_2); 
       fragmentTransaction.addToBackStack(null); 
       fragmentTransaction.commit(); 
      } 
     }); 
     return view; 
    } 
} 

fragmen_1_layout.xml:

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

    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/textView" 
     android:text="First Fragment" 
     android:textSize="20sp"/> 
    <Button 
     android:layout_width="100dp" 
     android:layout_height="60dp" 
     android:id="@+id/button" 
     android:text="click"/> 

</LinearLayout> 

Fragment_2.java:

package com.example.arantik.test4; 

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

public class Fragment_2 extends Fragment { 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 

     View view=inflater.inflate(R.layout.fragment_2_layout, container, false); 
     return view; 
    } 
} 

fragmen_2_layout.xml:

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

    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/textView" 
     android:text="Second Fragment" 
     android:textSize="20sp"/> 

</LinearLayout> 

我沒有改變其他的Java或XML代碼。對不起,我的英文不好,謝謝。

回答

0

在片段中添加公共方法MainActivity,將getActivity()轉換爲MainActivity,然後可以調用該方法。您也可以使用interface,請參閱Communicating with Other Fragments

+0

您可以解釋更多關於鑄造嗎?我是android新手。 – Arantik

+0

對不起,我認爲現在將FragmentManager放入片段並不是一個好主意。以下可能是更好的方法。 [要旨](https://gist.github.com/z-jp/2abc6a08f80236bc72dba27ba1c3947b) – ZunPiau