如何在默认的Android Studio导航抽屉中切换片段

时间:2015-11-30 16:41:20

标签: java android android-fragments

我在默认导航抽屉中切换片段有问题

MainActivity.java

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

@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, "HILFE:" +
                    " An der oberen linken Ecke, findest Du Drei Striche mit dem sich ein Menü öffnen lässt", 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;
        if (id == R.id.nav_gallery) {

        } else if (id == R.id.Experimente) {

        } else if (id == R.id.nav_formeln) {

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

}

ContentMain.xml

<?xml version="1.0" encoding="utf-8"?>

<TextView android:text="@string/TextViewIntro" android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:elegantTextHeight="false"
    android:gravity="top|center"
    android:textColor="#000000"
    android:textSize="@dimen/abc_text_size_title_material"
    android:id="@+id/TextViewIntro"
    android:layout_alignParentTop="true"
    android:layout_alignParentStart="true" />

<android.support.design.widget.FloatingActionButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/fab"
    android:layout_alignParentBottom="true"
    android:layout_alignParentEnd="true"
    android:layout_marginBottom="53dp"
    android:src="@android:drawable/ic_menu_help" />

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignTop="@+id/TextViewIntro"
    android:layout_alignParentEnd="true"
    android:id="@+id/mainFrame"></FrameLayout>

BlankFragment.java

import android.app.Activity;
import android.net.Uri;
import android.os.Bundle;
import android.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;
package flo.nawigator;

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

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

    View fraglayoutv = inflater.inflate(R.layout.fragment_blank, null);

    fragbtn1 = (Button) fraglayoutv.findViewById(R.id.buttonfrag1);
    fragbtn1.setOnClickListener(this);

    return fraglayoutv;
}


@Override
public void onActivityCreated(Bundle savedInstanceState) {

    super.onActivityCreated(savedInstanceState);
}

@Override
public void onClick(View v) {
    switch (v.getId()) {
        case R.id.buttonfrag1: {

            Toast.makeText(getActivity(), "Button gedrückt!!", Toast.LENGTH_SHORT).show();
        }
    }
}

我有一个BlankFragment!如何使用Android导航抽屉切换到它

MainActivity.xml

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

fragment_blank.xml

<RelativeLayout 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="flo.nawigator.BlankFragment"
android:id="@+id/fragmentgalerie">

<!-- TODO: Update blank fragment layout -->

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="@string/galeriefragtext"
    android:id="@+id/textView"
    android:layout_centerHorizontal="true" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Okay!"
    android:id="@+id/buttonfrag1"
    android:layout_alignParentBottom="true"
    android:layout_alignStart="@+id/textView"
    android:layout_marginBottom="217dp" />

1 个答案:

答案 0 :(得分:2)

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


Fragment fragment;




    if (id == R.id.nav_gallery) {

    } else if (id == R.id.Experimente) {

   BlankFragment bnlfrgmnt = new BlankFragment();
   getSupportFragmentManager().beginTransaction()
                .add(R.id.mainFrame, bnlfrgmnt).commit();
  //do similar for other navigation drawer items to display other fragments.




    } else if (id == R.id.nav_formeln) {

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