如何在没有视图寻呼机的情况下创建选项卡

How can I create tabs without a view pager

我需要三个选项卡来基本上更改下面列表视图的排序。我不需要视图寻呼机或任何太复杂的东西,但我只能找到带有视图寻呼机的示例。我正在使用 appCompat 和工具栏并扩展 AppCompatActivity。

我已经尝试过标签主机,但是当我 运行 应用程序时它是不可见的

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TabWidget
        android:id="@android:id/tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

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

        <LinearLayout
             android:id="@+id/tab1"
             android:layout_width="match_parent"
             android:layout_height="match_parent"
             android:orientation="horizontal" />
...

听起来您只需要按钮,而不是标签。但是您希望它看起来像新的 TabLayout 和应用程序,您应该能够使用 Design Support Libraries 中提供的新的 TabLayout,但是您不需要连接操作来实际分页任何内容并且可以使用它们来触发排序顺序的更改。

您的 Activity 的布局将类似于:

<android.support.design.widget.AppBarLayout
    android:layout_height="wrap_content"
    android:layout_width="match_parent">

    <include layout="@layout/toolbar" />

    <android.support.design.widget.TabLayout
        android:id="@+id/tab_layout"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:visibility="gone"/>

</android.support.design.widget.AppBarLayout>

在您的 Activity 中,您需要类似以下内容的内容:

mTabLayout = mActivity.getTabLayout();
if (mTabLayout != null) {
    // Or manually set if you want
    mTabLayout.setTabsFromPagerAdapter(getPagerAdapter());
    mPageChangeListener = new TabLayout.TabLayoutOnPageChangeListener(mTabLayout);
    // This is the bit that you will not want as that will change pages
    // and you aren't actually changing pages, just listening
    // Leaving these lines in just for reference on what you would do for paging
    // mPager.addOnPageChangeListener(mPageChangeListener);
    mPager.addOnPageChangeListener(this);
    //mTabLayout.setOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(mPager) );
}
loadData();

然后您可以实施各种 onPage* 方法,您可以使用这些方法简单地触发您的排序顺序更改,而不是让它实际更改您的页面。