Android 使用带有导航抽屉的 ViewPager 的操作栏选项卡

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20783823/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 03:27:31  来源:igfitidea点击:

Action Bar Tabs using ViewPager with Navigation Drawer

androidandroid-fragmentsandroid-actionbarnavigation-drawer

提问by

Requirement:- Action Bar Tabs using ViewPager with Navigation Drawer .

要求:- 使用 ViewPager 和 Navigation Drawer 的操作栏选项卡。

I can create a Navigation Drawer example

我可以创建一个导航抽屉示例

http://www.omgubuntu.co.uk/wp-content/uploads/2013/07/sidebar.jpg

http://www.omgubuntu.co.uk/wp-content/uploads/2013/07/sidebar.jpg

Action Bar Tabs using ViewPager separately.

单独使用 ViewPager 的操作栏选项卡。

http://developer.android.com/design/media/action_bar_pattern_considerations.png

http://developer.android.com/design/media/action_bar_pattern_thinkations.png

But when I try to use both at once I am having issue.

但是当我尝试同时使用两者时,我遇到了问题。

I can create Navigation Drawer using fragments and Action Bar Tabs using Fragment. But the initial Activity of the both examples is Fragment Activity.

我可以使用 Fragment 使用 Fragment 和 Action Bar Tabs 创建导航抽屉。但是这两个例子的初始 Activity 都是 Fragment Activity。

How to implement the action bar tabs on a fragment which is part of the navigation drawer?

如何在作为导航抽屉一部分的片段上实现操作栏选项卡?

采纳答案by jagmohan

Use the following layout for your main activity.

为您的主要活动使用以下布局。

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager_container"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="#ffe6e1d4"
        android:focusable="true"
        android:focusableInTouchMode="true" />    

    <ListView
        android:id="@+id/left_drawer"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="left"
        android:listSelector="@drawable/drawer_list_selector"        
        android:background="@color/drawer_bg" />

</android.support.v4.widget.DrawerLayout>

Write your FragmentPagerAdapter as show in APPTabsAdapter.

编写您的 FragmentPagerAdapter,如APPTabsAdapter 所示

This is how I had built it in one of my projects.

这就是我在我的一个项目中构建它的方式。

You can try and ask for help, if needed.

如果需要,您可以尝试寻求帮助。

OR

或者

You can take help from this GitHub Repo.

你可以从这个GitHub Repo 获得帮助。

Thanks.

谢谢。

回答by jiahao

The problem of using the tabs of the actionbar is when the drawer appears, it will appears under the tabs, since the tabs are part of the actionBar.

使用actionbar的tabs的问题是drawer出现的时候,它会出现在tabs下面,因为tabs是actionBar的一部分。

I have tried using tabHost instead and it works much better.

我曾尝试使用 tabHost 代替它,它的效果要好得多。

You get the source code here: https://github.com/jiahaoliuliu/DrawerWithTabsAndViewPager

你在这里得到源代码:https: //github.com/jiahaoliuliu/DrawerWithTabsAndViewPager

Here is a little explanation about it:

这里有一个关于它的小解释:

  1. The first level there is a Drawer, from the v4 support library
  2. Inside the drawer, the first element is the tabHost, which I have set the width and the height of the content to 0.
  3. Under the tabhost, there is the viewpager.
  1. 第一层有一个 Drawer,来自 v4 支持库
  2. 在抽屉里面,第一个元素是 tabHost,我已经将内容的宽度和高度设置为 0。
  3. 在 tabhost 下,有 viewpager。

Once everything has been created, what I have done is create a listener for the tabhost and another one for the viewPager, so when the user click on any tab the view pager will set the current item, and viceversa.

创建完所有内容后,我所做的是为 tabhost 创建一个侦听器,为 viewPager 创建另一个侦听器,因此当用户单击任何选项卡时,视图寻呼机将设置当前项目,反之亦然。

Enjoy Coding!

享受编码!

回答by Tim Rae

As you have noticed, ActionBar tabs don't play very nicely with Navigation Drawer and this design mode has been deprecated in API 21anyway.

正如您所注意到的,ActionBar 选项卡不能很好地与 Navigation Drawer 配合使用,而且这种设计模式在 API 21 中已被弃用

I used the classes in SlidingTabs examplefrom Android developers to achieve this effect without having to include a 3rd party library dependency, and am very happy with the result. There is a video tutorialas well.

我使用了Android 开发人员的SlidingTabs 示例中的类来实现此效果,而无需包含第 3 方库依赖项,并且对结果非常满意。还有视频教程