使用Fragment或Activity实现Android Studio导航抽屉

5
我将为您翻译以下it技术方面的内容,内容涉及使用Android Studio中的导航抽屉模板开发应用。我创建了一个新项目,并使用这个模板,但当我运行程序并点击菜单项时,视图没有改变。我在互联网上搜索了很多地方,但是没有找到我如何处理它的方法。
以下是Android Studio提供的代码:
 public boolean onNavigationItemSelected(MenuItem item) {
    // Handle navigation view item clicks here.

    int id = item.getItemId();

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


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

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

我想要实现的是用适当的视图替换当前视图以响应菜单项的点击。
使用Fragment是最好的方法吗?还是应该为每个菜单项创建不同的活动?

你应该使用Fragment,因为它可以在同一个Activity中轻松使用相同的导航抽屉。 - GiapLee
5个回答

21
在您的情况下,添加片段将是最佳解决方案。
创建一个名为BlankFragment.java的片段。
public class BlankFragment extends Fragment {

    public BlankFragment() {
        // Required empty public constructor
    }

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

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_blank, container, false);
    }

}

并创建fragment_black.xml

    <FrameLayout 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="com.above_inc.shyam.drawer.BlankFragment">

    <!-- TODO: Update blank fragment layout -->
    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="@string/hello_blank_fragment" />

</FrameLayout>

现在更换你的方法

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

    Fragment fragment = null;
    if (id == R.id.nav_camera) {
        // Handle the camera action
        fragment = new BlankFragment();
    } else if (id == R.id.nav_gallery) {

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

    }

    if (fragment != null) {
        FragmentManager fragmentManager = getSupportFragmentManager();
        fragmentManager.beginTransaction()
                .replace(R.id.frame_container, fragment).commit();

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

}

请在您的content_main.xml中添加以下代码

 <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.above_inc.shyam.drawer.MainActivity"
    tools:showIn="@layout/app_bar_main">


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

你也可以像上面代码中的相机选项一样,向其他选项添加更多的片段


2

如果你想在点击菜单项后在下一个屏幕上显示导航抽屉,则使用Fragment将是最佳选择。

如果你使用activity,那么除非你创建一个扩展了Navigation Drawer的BaseActivity并在每个地方使用它,否则导航抽屉将不会显示。在这种情况下,你还需要更改活动转换动画,因为点击菜单后会弹出新活动,这可能看起来很奇怪。


0

@Passiondroid答案关于仅在全局使用导航抽屉时使用Fragment是正确的。

关于在单击菜单项时启动活动,在我的一个应用程序中,我遵循这个方法,

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

        switch(id)
        {

            case R.id.nav_profile:
            selection = 1;
                intent = new Intent("android.intent.action.WEB");
                intent.putExtra("selection",selection);
                startActivity(intent);
                break;
            case R.id.nav_books:
                intent = new Intent("android.intent.action.WEBD");
                selection = 2;
                intent.putExtra("selection",selection);
                startActivity(intent);
                break;
            case R.id.fav_quotes:
                intent = new Intent ("android.intent.action.SHOWFAVTES");
                selection = 3;
                intent.putExtra("selection",selection);
                startActivity(intent);
                break;
             default:
                break;
        }

要更改和配置菜单项,您可以在res->Menu下找到“activity_main_drawer.xml”。

并且在单击每个菜单项时,您可以根据自己的意愿启动活动。在使用之前,您必须在AndroidManifest.xml文件中声明这些活动。


0

在您的项目上右键单击 -> 新建 -> 片段。

相应地编辑开关案例。


0

enter image description here

从 Android Studio > 3.5 开始,当您创建导航抽屉活动时,您会发现它生成片段,在单击抽屉项时打开。

如果您想打开一个活动,可以添加监听器。

 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.nav_accounts)
            .setDrawerLayout(drawer)
            .build();
    NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
    NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
    NavigationUI.setupWithNavController(navigationView, navController);


    navController.addOnDestinationChangedListener(new NavController.OnDestinationChangedListener() {
        @Override
        public void onDestinationChanged(@NonNull NavController controller, @NonNull NavDestination destination, @Nullable Bundle arguments) {

            int menuId = destination.getId();

            switch (menuId){
                case R.id.nav_gallery:
                    Toast.makeText(MainActivity.this,"You tapped gallery",Toast.LENGTH_LONG).show();
                    fab.hide();

                    break;
                    default:
                        fab.show();
                        break;
            }

        }
    });

查看完整教程。导航抽屉


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接