【问题标题】:Persistent header layout in ViewPagerViewPager 中的持久标题布局
【发布时间】:2015-09-02 02:33:20
【问题描述】:

我有一个可以滑动到 3 个不同页面的 ViewPager。所有 3 个页面都应具有完全相同的标题。

有没有办法在所有 3 个页面上保持相同的标题,这样当我滑动到另一个页面时,标题不会移动/更改,但其余内容会移动/更改?

这是我的 ViewPager 布局:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:fitsSystemWindows="true">

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

</LinearLayout>

这是我在 ViewPager 上方加载的标题布局:

<LinearLayout
    android:orientation="horizontal"
    android:layout_width="wrap_content"
    android:layout_height="match_parent">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="User"
        android:layout_gravity="center_vertical" />

</LinearLayout>

这是我膨胀标题的方法:

final ListView listView = (ListView) view.findViewById(R.id.listview);
View header = inflater.inflate(R.layout.header, listView, false);
listView.addHeaderView(header, null, false);

【问题讨论】:

    标签: android android-layout android-fragments android-activity android-xml


    【解决方案1】:

    只需将标题保留在视图寻呼机之外。为 viewpager 使用 layout_weight 以便它占用整个可用空间。

    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:fitsSystemWindows="true">
    
        <LinearLayout
        android:orientation="horizontal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
    
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="User"
                android:layout_gravity="center_vertical" />
    
        </LinearLayout>
    
        <android.support.v4.view.ViewPager
            android:id="@+id/pager"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"/>
    
    </LinearLayout>
    

    【讨论】:

    • 如何像在代码中那样将其设置为标题?否则,布局不可滚动,只有 ViewPager 可以。
    • 上面的布局包括了header和viewpager。 “布局不可滚动”是什么意思?
    • ListView 是一个可滚动的布局。我现在是如何设置的,我使用addHeaderView() 将布局作为标题添加到 ListView。如果我按照你说的做,只有 ViewPager 是可滚动的(屏幕的一小部分),而标题布局始终保持在屏幕上,即使我滚动也是如此。
    • 我很困惑。在问题的第一部分中,您说过要显示被刷页面常见的标题。在第二部分中,您说要向列表视图添加一些标题。我的回答只针对第一部分。
    • 是的,我最初有一个 ListView 的标题,但我也有一个 ViewPager。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-09-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多