如何在Android中的活动之间共享NavigationView(SDK21 +)

时间:2016-01-06 01:12:27

标签: java android material

所以,基本上我有这个类,HomeActivity.class:

import android.content.Intent;
import android.os.Bundle;
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 HomeActivity extends AppCompatActivity
    implements NavigationView.OnNavigationItemSelectedListener {

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

    /* TODO: Relocate this button to appear in each individual place
    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.new_ticket);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "New ticket function here", 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();

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

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

        // Create SB to hold the places data
        final StringBuilder apiPlacesInfo = new StringBuilder();

        // Create new intent
        Intent i = new Intent(getApplicationContext(), PlacesActivity.class);

        // Get all available places from the API
        new Thread(new Runnable() {
            @Override
            public void run() {
                apiPlacesInfo.append(ConnectionHandler.APIConnect("spaces"));
            }
        }).start();

        // Parse the info
        Place[] places = DataParser.ParsePlaces(apiPlacesInfo.toString());

        // Put the extras to get on the next activity
        i.putExtra("places", places);

        // Start the new activity
        startActivity(i);

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

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

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

这是我的activity_drawer.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>

我也有.xml中列出的项目:          

<group android:checkableBehavior="single">
    <item
        android:id="@+id/nav_home"
        android:icon="@drawable/ic_menu_home"
        android:title="@string/menu_home"
        android:checked="true"/>
    <item
        android:id="@+id/nav_places"
        android:icon="@drawable/ic_menu_places"
        android:title="@string/menu_places" />
    <item
        android:id="@+id/nav_slideshow"
        android:icon="@drawable/ic_menu_slideshow"
        android:title="Option 3" />
    <item
        android:id="@+id/nav_manage"
        android:icon="@drawable/ic_menu_manage"
        android:title="Option 4" />
</group>

<item android:title="Separator 1">
    <menu>
        <item
            android:id="@+id/nav_share"
            android:icon="@drawable/ic_menu_share"
            android:title="Option 1" />
        <item
            android:id="@+id/nav_send"
            android:icon="@drawable/ic_menu_send"
            android:title="Option 2" />
    </menu>
</item>
</menu>

基本上大部分都是由AndroidStudio生成的。

我现在想要创建一个新活动 - &gt; PlacesActivity,每当我在navView上按“位置”时,启动该活动。现在一切都很好,问题是我似乎无法在两个活动之间“共享”navigationView。

我已经在这里工作了3个小时:其他类似的问题都没有。我尝试使用HomeActivity扩展PlacesActivity并且它只是部分工作(它不会显示正确的内容,即使这样,我也必须从HomeActivity类复制代码)。

最好的方法是什么?

谢谢!

0 个答案:

没有答案