【问题标题】:Navigation Drawer - Redirected Activity does not have Left Menu导航抽屉 - 重定向的活动没有左菜单
【发布时间】:2017-07-29 02:12:37
【问题描述】:

您好,我想在 Android Studio 中使用 Navigation Drawer Activity 创建一个点击事件。当我单击左侧菜单中的图库时,我有意将其重定向到图库活动。没关系,它可以工作,但画廊活动中没有左侧菜单。我希望所有重定向的活动都有左菜单。我怎样才能做到这一点?

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
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();


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

            // Handle the camera action
        } else if (id == R.id.nav_gallery) {
            Intent intent = new Intent(MainActivity.this, GalleryActivity.class);
            startActivity(intent);
        } else if (id == R.id.nav_slideshow) {
 Intent intent = new Intent(MainActivity.this, Slideshow.class);
            startActivity(intent);
        } 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;
    }
}

我只是通过添加意图线来改变这一点。当我选择 NavigationDrawerActivity 时,其他所有内容都是默认代码。

public class GalleryActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_gallery);
 }
 }

画廊布局

<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.gamze.leftmenu05.GalleryActivity">


<EditText
    android:id="@+id/editText"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:ems="10"
    android:inputType="textPersonName"
    android:text="Name" />

<Button
    android:id="@+id/button"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Button" />

</LinearLayout>

【问题讨论】:

    标签: android android-intent android-activity navigation-drawer


    【解决方案1】:

    是的,您也可以通过活动来做到这一点。 因为在加载视图和盯着屏幕的情况下活动有点困难或沉重。这就是为什么片段是导航抽屉的首选方式。

    你可以的

    • 创建具有抽屉的基本活动并在其余活动中扩展该活动。你可以检查实现herehere
    • 第二个选项可以创建带有导航抽屉的主要活动,并在内容部分从菜单中加载所有片段。为此,您可以查看here

    【讨论】:

    • 这样你每次都在膨胀一个新的抽屉菜单,恕我直言,这是非常低效的。
    • @Fabio 不,这不会发生,因为基本活动将保留在内存中,因此不会再次加载,也不会重新创建视图。
    • 啊好吧,完美。在这种情况下我总是使用片段,但很高兴知道:)
    • @androidnoobdev 不,我无法让它工作。我尝试了第一个选项但不起作用。
    • 当我单击一个活动并且该活动正在启动活动时,它就会起作用。但是如果它没有开始活动,那么应用程序就会停止
    【解决方案2】:

    因为你的 GalleryActivity 有不同的布局。

    如果您想保留除主窗口内容之外的所有内容,我建议您使用图片库的片段而不是活动

    【讨论】:

    • 我想使用活动。可能吗?我想应用这个我的应用程序,它有活动。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多