简体   繁体   English

使用ViewPager和导航抽屉的操作栏选项卡

[英]Action Bar Tabs using ViewPager with Navigation Drawer

Requirement:- Action Bar Tabs using ViewPager with Navigation Drawer . 要求: - 使用ViewPager和导航抽屉的操作栏选项卡。

I can create a Navigation Drawer example 我可以创建一个导航抽屉示例

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

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. 但这两个例子的初始活动是片段活动。

How to implement the action bar tabs on a fragment which is part of the navigation drawer? 如何在作为导航抽屉一部分的片段上实现操作栏选项卡?

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的一部分。

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 第一层有一个抽屉,来自v4支持库
  2. Inside the drawer, the first element is the tabHost, which I have set the width and the height of the content to 0. 在抽屉里面,第一个元素是tabHost,我已经将内容的宽度和高度设置为0。
  3. Under the tabhost, there is the viewpager. 在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! 享受编码!

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回购中获得帮助。

Thanks. 谢谢。

As you have noticed, ActionBar tabs don't play very nicely with Navigation Drawer and this design mode has been deprecated in API 21 anyway. 正如您所注意到的,ActionBar选项卡与Navigation Drawer的效果不是很好,而且这种设计模式无论如何都已在API 21中弃用

I used the classes in SlidingTabs example from Android developers to achieve this effect without having to include a 3rd party library dependency, and am very happy with the result. 我使用Android开发人员的SlidingTabs示例中的类来实现此效果,而不必包含第三方库依赖项,并且对结果非常满意。 There is a video tutorial as well. 还有一个视频教程

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

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