简体   繁体   English

导航抽屉和Android中的活动

[英]Navigation Drawer and with Activity in Android

I am trying out the navigation drawer (slide menu) given in this tutorial. 我正在尝试本教程中给出的导航抽屉(幻灯片菜单)

The difference with above link and mine is that instead of calling fragments I am trying to call the activity. 与上面的链接和我的不同之处在于,我试图调用活动而不是调用片段。 When the app opens I am not able to see the Navigation drawer menu I can see only the action bar with HOME activity opened. 当应用程序打开时,我无法看到导航抽屉菜单我只能看到打开了HOME活动的操作栏。

Here is the code that I changed: (Is it necessary to have a fragment or can I use activity for my first screen in Navigation Drawer?) 这是我更改的代码:( 是否需要有一个片段,或者我可以在导航抽屉中使用活动作为我的第一个屏幕?)

    mTitle = mDrawerTitle = getTitle();

    navMenuTitles = getResources().getStringArray(R.array.nav_drawer_items);
    navMenuIcons = getResources().obtainTypedArray(R.array.nav_drawer_icons);
    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    mDrawerList = (ListView) findViewById(R.id.left_drawer);    

    navDrawerItems = new ArrayList<NavDrawerItem>();

    navDrawerItems.add(new NavDrawerItem(navMenuTitles[0], navMenuIcons.getResourceId(1, -1)));
    navDrawerItems.add(new NavDrawerItem(navMenuTitles[1], navMenuIcons.getResourceId(2, -1),true, "200"));

    navMenuIcons.recycle();

    mDrawerList.setOnItemClickListener(new SlideMenuClickListener());

    adapter = new NavDrawerListAdapter(getApplicationContext(), navDrawerItems);
    mDrawerList.setAdapter(adapter);

    getActionBar().setDisplayHomeAsUpEnabled(true);
    getActionBar().setHomeButtonEnabled(true);

    mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
            R.drawable.drawer,
            R.string.drawer_open,
            R.string.drawer_close
            ) 
    {
        public void onDrawerClosed(View view) 
        {
            getActionBar().setTitle(mTitle);
            invalidateOptionsMenu();
        }

        public void onDrawerOpened(View drawerView) 
        {
            getActionBar().setTitle(mDrawerTitle);
            invalidateOptionsMenu();
        }
    };
    mDrawerLayout.setDrawerListener(mDrawerToggle);

    if (savedInstanceState == null) 
    {
        displayView(0);
    }
}

private class SlideMenuClickListener implements
ListView.OnItemClickListener
{
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) 
    {
        displayView(position);
    }
}
private void displayView(int position) 
{
    switch (position) 
    {
    case 0:
        //fragment = new HomeFragment();        

        Intent intent = new Intent(this, Home.class);
        startActivity(intent);

        return;

    case 1:
        //fragment = new FindPeopleFragment();

        Intent intent1 = new Intent(this, Profile.class);
        startActivity(intent1);
        break;

    case 2:
        //fragment = new PhotosFragment();

        Intent intent2 = new Intent(this, Details.class);
        startActivity(intent2);
        break;

    default:
        break;
    }

    mDrawerList.setItemChecked(position, true);
    mDrawerList.setSelection(position);
    setTitle(navMenuTitles[position]);
    mDrawerLayout.closeDrawer(mDrawerList);
}

How do I fix this to show Navigation drawer on my Home Activity? 如何修复此问题以在我的家庭活动中显示导航抽屉?

Update: 更新:

I even tried the following option given in this link: 我甚至尝试了以下链接中给出的以下选项:

How can I call one of my activity using navigation drawer ? 如何使用导航抽屉调用我的某个活动? but I am still not getting the navigation slide menu. 但我还没有得到导航幻灯片菜单。

You can't do that... Navigation drawer is a layout from an activity and you cant show an activity inside another activity, you need to use a fragments for this! 你不能这样做...导航抽屉是一个活动的布局,你不能在另一个活动中显示一个活动,你需要使用一个片段!

An activity is a screen, you cant show a screen inside another screen, but a fragment may be a component of a screen, and you can inflate a fragment inside a container of an activity and then show to the user. 活动是一个屏幕,你不能在另一个屏幕内显示一个屏幕,但是一个片段可能是一个屏幕的一个组件,你可以在一个活动的容器内膨胀一个片段然后向用户显示。

if you wan't do this, you can create a abstract activity and inherit, but you will not have aa activity in a fragment, you will have a multiple activities with each one with your own navigation drawer. 如果你不这样做,你可以创建一个抽象活动并继承,但你不会在一个片段中有一个活动,你将有一个多个活动,每个活动都有你自己的导航抽屉。

If you look at the sample documentation for Android Navigation drawer, it is explicitely defined to use fragments instead of activities to load different 'pages' from the navigation bar. 如果您查看Android导航抽屉的示例文档,则明确定义使用片段而不是活动来从导航栏加载不同的“页面”。 As the app lifecycle for Android goes, fragments are easier to load and produce less impact on the Android system. 随着Android的应用程序生命周期的发展,片段更容易加载,对Android系统的影响也更小。 they can also be easily set into the background and be replaced by other different fragments. 它们也可以很容易地设置在背景中,并被其他不同的片段取代。

Having said that, your best approach would be to convert your activities into fragments and use them to load the different pages you need in your app. 话虽如此,您最好的方法是将您的活动转换为片段,并使用它们加载您应用中所需的不同页面。 It is not such a difficult task, and I will show you how: 这不是一项艰巨的任务,我将告诉你如何:

  1. Change all activities to fragments 将所有活动更改为片段

     public class nameOfFragment extends Fragment { } 
  2. Use the onCreateView method instead of onCreate 使用onCreateView方法而不是onCreate

     public View onCreateView() { View view = inflater.inflate(R.layout.activity_main, container, false); //any other elements in that view need to be included here in this manner. Button rate = (Button) rootView.findViewById(R.id.rate); return view; } 

These changes should be enough to change your activities into fragments. 这些更改应足以将您的活动更改为片段。 You need to do the same for all fragments which are accessed through your navigation bar. 您需要对通过导航栏访问的所有片段执行相同的操作。

Please post your code for the activities you are using if you need any further help. 如果您需要任何进一步的帮助,请发布您正在使用的活动的代码。 Hope this helps :) 希望这可以帮助 :)

You've got the code to build and display a navigation bar right? 你有代码来构建和显示导航栏吗? Put your code above into a base activity class. 将您的代码放在基本活动类中。 Every activity that you want to access the navigation bar from should then inherit from that base class. 然后,您要访问导航栏的每个活动都应该从该基类继承。

Google's IO 2014 app does the same thing, take a look at their source here . 谷歌的IO 2014应用程序做同样的事情,看看他们的来源在这里

When you do this, you will need to find a way to maintain the drawer's state as you transition to the next activity. 执行此操作时,您需要找到一种方法来在转换到下一个活动时保持抽屉的状态。 I'm not sure how to do that, but you'll probably find the answer in the source code for the IO app. 我不知道该怎么做,但你可能会在IO应用程序的源代码中找到答案。

If you have to have single instance of drawer, then you have to go by fragments. 如果你必须有单个抽屉实例,那么你必须通过片段。

If you have no choice to go other than with activities, define a base class for all your activites(say BaseActivity). 如果除了活动之外别无选择,请为所有活动定义基类(比如BaseActivity)。 And in BaseActivity - you can do the necessary coding to integrate drawer. 在BaseActivity中 - 您可以进行必要的编码以集成抽屉。 Each activity extending BaseActivity will now show up drawer menu (but the instances will be different) 扩展BaseActivity的每个活动现在将显示抽屉菜单(但实例将不同)

Now you need to use the toolbar. 现在您需要使用工具栏。 Android has made appcompat library to introduce material design in older versions of android as well. Android已经制作了appcompat库,以便在旧版本的android中引入材料设计。 You need to use compile dependency of appcompat. 您需要使用appcompat的编译依赖项。 Change the style.xml 更改style.xml

<resources>

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
</style>

</resources>

Then we will choose to have no old action bar. 然后我们会选择没有旧动作栏。 After then we make a contract between navigation drawer and toolbar. 之后我们在导航抽屉和工具栏之间签订合同。 So we need not to use old actionbar now. 所以我们现在不需要使用旧的操作栏。 You can refer the code below: 您可以参考以下代码:

<android.support.v7.widget.Toolbar
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#ff6d7fe2"
    app:contentInsetEnd="0dp"
    app:contentInsetStart="0dp"
    ></android.support.v7.widget.Toolbar>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM