【问题标题】:Adding Default Navigation Drawer to NewActivity将默认导航抽屉添加到 NewActivity
【发布时间】:2020-04-27 11:01:19
【问题描述】:

我在 3.5.1 版本中使用默认抽屉创建了一个新项目。 我正在创建新的 Activity(ProfileActivity),我也想要 MainActivity 的 Drawer。

当我尝试从导航抽屉打开 ProfileActivity 时,它正在打开 HomeFragment。是的,我给出了正确的 ID。

尝试了几个月,但没有成功,这一次可能会放弃。他们在任何地方都提供有关片段的解决方案。

我试过了

个人资料活动

@Override
protected void onCreate(Bundle savedInstanceState)
{
    // TODO Auto-generated method stub
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        LayoutInflater inflater = (LayoutInflater) this
                .getSystemService( Context.LAYOUT_INFLATER_SERVICE);
        View contentView = inflater.inflate(R.layout.activity_profile, null, false);
        final TextView textView = contentView.findViewById( R.id.text_gallery );
        drawer.addView(contentView, 0);
    }
}

并使抽屉受到保护

当我尝试从导航抽屉打开 ProfileActivity 时,它正在打开 HomeFragment。是的,我给出了正确的 ID

<item
            android:id="@+id/profileActivity"
            android:icon="@drawable/ic_menu_manage"
            android:title="Profile" />

我的 MainActivity 如下

package com.example.iqhut;

import android.os.Bundle;

import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.android.material.snackbar.Snackbar;

import android.view.View;

import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.core.view.ViewCompat;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.ui.AppBarConfiguration;
import androidx.navigation.ui.NavigationUI;

import com.google.android.material.navigation.NavigationView;

import androidx.drawerlayout.widget.DrawerLayout;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import android.view.Menu;
import android.widget.RelativeLayout;

import java.util.Calendar;

public class MainActivity extends AppCompatActivity {

    private AppBarConfiguration mAppBarConfiguration;
    protected DrawerLayout drawer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate( savedInstanceState );
        setContentView( R.layout.activity_main );
        Toolbar toolbar = findViewById( R.id.toolbar );
        setSupportActionBar( toolbar );
        FloatingActionButton fab = 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();
            }
        } );
        drawer = findViewById( R.id.drawer_layout );
        NavigationView navigationView = findViewById( R.id.nav_view );
        // Passing each menu ID as a set of Ids because each
        // menu should be considered as top level destinations.
        mAppBarConfiguration = new AppBarConfiguration.Builder(
                R.id.nav_home, R.id.nav_gallery, R.id.nav_slideshow,
                R.id.nav_tools, R.id.nav_share, R.id.nav_send, R.id.profileActivity )
                .setDrawerLayout( drawer )
                .build();
        NavController navController = Navigation.findNavController( this, R.id.nav_host_fragment );
        NavigationUI.setupActionBarWithNavController( this, navController, mAppBarConfiguration );
        NavigationUI.setupWithNavController( navigationView, navController );

    }

    @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 onSupportNavigateUp() {
        NavController navController = Navigation.findNavController( this, R.id.nav_host_fragment );
        return NavigationUI.navigateUp( navController, mAppBarConfiguration )
                || super.onSupportNavigateUp();
    }
}

【问题讨论】:

  • 你的问题真的不清楚。请您再解释一下吗?
  • @AlanDeep 在导航抽屉中,当我单击活动(ProfileActivity)时,它会打开第一个片段(HomeFragment)。
  • 为什么不在 profileActivity 中设置内容视图?
  • @AlanDeep 我应该怎么做,对不起,真的是 android 新手。
  • 您可以包含您的导航 XML 文件吗?

标签: android android-studio navigation-drawer


【解决方案1】:

如果你想禁用 DrawerLayout,没有理由使用单独的活动。相反,您可以listen for navigation events 并在您的个人资料目的地禁用 DrawerLayout:

navController.addOnDestinationChangedListener(new NavController.OnDestinationChangedListener() {
   @Override
   public void onDestinationChanged(@NonNull NavController controller,
           @NonNull NavDestination destination, @Nullable Bundle arguments) {
       if(destination.getId() == R.id.profileFragment) {
           drawer.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
       } else {
           drawer.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);
       }
   }
});

【讨论】:

  • 如果您需要在多个目的地锁定抽屉,另请参阅 this post 关于使用基于 OnDestinationChangedListener 的参数。
  • 您不需要新的活动。是什么让您认为您需要一项新活动?
  • 实际上,先生,我是 Android 新手,在任何地方我都能找到 Activity 中解释的任何示例。
  • 就像我在这里得到活动代码(不是片段)appsnipp.com/free-android-profile-design-with-source-code
  • 您应该通过Navigation codelab 并观看Single Activity talk,了解您在 2020 年应该做什么。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-03-12
相关资源
最近更新 更多