【问题标题】:How to show all the tabs in viewPager at time如何同时显示viewPager中的所有选项卡
【发布时间】:2018-11-05 10:36:25
【问题描述】:

我正在使用viewPager,它有一个片段,而那个片段有一个recyclerView。我正在尝试同时显示viewPager 的所有选项卡。这是可能的还是有任何不同的方法来处理这种情况。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:orientation="vertical">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">


        <android.support.design.widget.TabLayout
            android:id="@+id/tabs"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:tabGravity="center"
            android:layout_gravity="center_horizontal"
            app:tabMode="scrollable"
            app:tabPaddingEnd="1dp"
            app:tabPaddingStart="1dp"/>
    </android.support.design.widget.AppBarLayout>

    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center_horizontal"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        tools:layout_editor_absoluteX="0dp"
        tools:layout_editor_absoluteY="0dp" /> 
</LinearLayout>

【问题讨论】:

  • 试试app:tabMode="fixed"
  • @V-rundPuro-hit 我正在使用 app:tabMode="scrollable" 因为更多选项卡将在运行时添加并且能够水平滚动
  • 好的..所以如果标签在屏幕上可见,您希望它们应该像那张图片一样位于中心?
  • @V-rundPuro-hit 不,我只希望所有选项卡同时显示它们的数据,即只有订单 1 显示其数据我想显示所有要显示的订单同时数据
  • 同屏所有数据?

标签: android android-fragments android-viewpager recyclerview-layout


【解决方案1】:

如果您想一次显示每个选项卡中的所有数据,我建议不要使用 ViewPager,因为那样会破坏它的目的。

如果您的想法是根据屏幕尺寸和/或方向来区分数据的呈现方式,那么您可以像这样加载不同的视图:

if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE)
{
    //Load landscape view with all data displaying at once
    setContentView(R.layout.main_landscape);
}
else
{
    //We're in portrait mode, so we don't have space to fit all data at once
    //so here we can take use of the ViewPager.
    setContentView(R.layout.main_portrait);
}

【讨论】:

  • 我正在使用 viewPager 在运行时添加多个可以水平滚动的列表
  • 是的,但是ViewPager 的目的是让您可以……嗯……浏览视图。如果您想一次显示所有内容,则需要考虑其他事情。
  • 是否有任何不同的方法可以显示多个水平对齐的列表。我试过网格布局,但我做不到。
  • 我假设您尝试过GridView?那应该可以。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-05-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多