0

我正在尝试使用 ActionBarSherlock 在活动中创建一个 NavigationDrawer 以及一个带有滑动选项卡的 ViewPager。我有标签,并为视图寻呼机和抽屉布局创建了布局。我知道我所拥有的是 viewpager 布局和drawerlayout 在单独的文件中,它们可能应该在同一个文件中以查找视图。虽然我不知道如何使用我拥有的代码来做到这一点。

我已经对如何执行此操作进行了大量研究,标准模式是不执行此操作,尽管正如许多人所指出的那样,Google Play Music 会执行此操作。我希望我的应用程序看起来像这样,并且我知道人们建议有一种解决方法。我只是不确定解决方法如何与 ActionBarSherlock 以及我目前已实现的代码一起使用。请任何人都可以帮我解决这个问题。先感谢您。

这是我的活动代码:

public class SlidingTabsActivity extends SherlockFragmentActivity
{
private ViewPager viewPager;
private TabsAdapter tabsAdapter;
private ActionBar actionBarTabs;

/* Navigation drawer */
private DrawerLayout drawerLayout;
private ListView drawerListView;
private ActionBarDrawerToggle drawerToggle;

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

    viewPager = new ViewPager(this);
    viewPager.setId(R.id.pager);
    setContentView(viewPager);

    /* Navigation Drawer */
    drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    drawerListView = (ListView) findViewById(R.id.left_drawer);

    System.out.println("Drawer Layout test:" + drawerLayout.getId());

    drawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
    drawerListView.setAdapter(new ArrayAdapter<String>(this, R.layout.navigation_drawer_list_item, R.array.navigation_drawer_list));
    drawerListView.setOnItemClickListener(new OnItemClickListener()
    {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) 
        {
            // TODO Auto-generated method stub

        }
    });

    actionBarTabs = getSupportActionBar();
    actionBarTabs.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
    actionBarTabs.setDisplayHomeAsUpEnabled(true);
    actionBarTabs.setHomeButtonEnabled(true);

    drawerToggle = new ActionBarDrawerToggle(this, drawerLayout, R.drawable.ic_drawer, R.string.drawer_open, R.string.drawer_close)
    {
        public void onDrawerClosed(View view) 
        {
            invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
        }

        public void onDrawerOpened(View drawerView) 
        {
            invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
        }
    };
    drawerLayout.setDrawerListener(drawerToggle);

    tabsAdapter = new TabsAdapter(this, viewPager); // Declares the tabs adapter class with the view pager view 

    /* Adds fragments to the tabs adapter */
    tabsAdapter.addTab(actionBarTabs.newTab().setText("PV"), Fragment_1.class, null);
    tabsAdapter.addTab(actionBarTabs.newTab().setText("CONFIG"), Fragment_2.class, null);
    tabsAdapter.addTab(actionBarTabs.newTab().setText("DIAG"), Fragment_3.class, null); 
}

这是我的 DrawerLayout 布局 xml 代码:

<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">

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

<ListView
    android:id="@+id/left_drawer"
    android:layout_width="240dp"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:choiceMode="singleChoice"
    android:divider="@android:color/transparent"
    android:dividerHeight="0dp"
    android:background="#111"/>

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

这是我的 ViewPager 布局 xml 代码:

<RelativeLayout 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=".SlidingTabsActivity" >

<android.support.v4.view.ViewPager
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/pager" />

</RelativeLayout>
4

1 回答 1

1

首先,您的 Activity 同时具有 Navigation Drawer 和 ViewPager,但我看到您将布局描述放在不同的 xml 文件中。但是,您必须为您的 Activity 只放置一个 xml 文件。

考虑到这一点,现在的重点是:“如何在同一个xml中声明DrawerLayout和ViewPager?”。如果您输入错误的顺序,它将无法正常工作(我遇到了同样的问题,这就是我提醒您的原因)。因此,答案是将 DrawerLayout 声明为 root,并在其中声明 ViewPager,然后在关闭 ViewPager“标签”后,声明 DrawerLayout 的 ListView。

一个例子:

<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/pager"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
</android.support.v4.view.ViewPager>

<ListView android:id="@+id/drawer_list"
    android:layout_width="240dp"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:choiceMode="singleChoice"
    android:divider="@android:color/transparent"
    android:dividerHeight="0dp"
    android:background="#111" />
</android.support.v4.widget.DrawerLayout>

我希望答案对你有所帮助。

于 2014-01-03T10:58:32.270 回答