使用导航抽屉显示片段 [英] Display fragments with Navigation Drawer

查看:87
本文介绍了使用导航抽屉显示片段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很困惑如何通过单击导航抽屉中的一个项目来打开不同的片段.

I'm confused how to open my different fragments by clicking on one Item in my navigation drawer.

在MainActivity中,我使用以下代码:

In MainActivity I use the following Code:

@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
    Fragment fragment;

    int id = item.getItemId();

    if (id == R.id.nav_listview) {
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        ft.replace(R.id.fragment, new ListFragment());
        ft.commit();

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

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

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

    }

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

首先,我想尝试打开ListFragment:

First of all I want to try to open my ListFragment:

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


public class ListFragment extends Fragment {


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

        return inflater.inflate(R.layout.fragment_list, container, false);
    }
}

在我的内容main.xml中,创建了以下片段,当单击导航抽屉中的特定项目时,应将其替换.

In my content main.xml I created the following fragment which should be replaced when clicking on the specific Items in the Navigation Drawer.

    <fragment
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/fragment" />

但是它不起作用...

But it isnt working...

有人可以帮助我吗?

拉斯塔曼

推荐答案

请更改您的片段类ListFragment的名称,因为Liststrong已经是 android.app 中的片段的子类 android操作系统

Please Change The name of your fragment class ListFragment Because ListFragment is already Sub Class of Fragments in android.app package android OS

这篇关于使用导航抽屉显示片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆