【问题标题】:Using RecyclerView inside ViewPager inside ContraintLayout在 ConstraintLayout 中的 ViewPager 中使用 RecyclerView
【发布时间】:2017-11-22 10:22:04
【问题描述】:

我使用ConstraintLayout 作为我的视图。在其中我放置了一个ViewPager,适配器包含的视图之一是RecyclerView

当我尝试仅放置图像时,我无法再滚动的高度缺失。该高度看起来与我在顶部使用的空间相同 (Toolbar + TabLayout)。

当使用RelativeLayout 而不是ConstraintLayout 时,它可以工作

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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">

    <RelativeLayout
        android:id="@+id/homeToolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="@color/toolbarColor">

        <android.support.v7.widget.AppCompatImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_marginLeft="8dp"
            android:padding="7dp"
            android:src="@drawable/ic_menu"
            tools:ignore="RtlHardcoded" />

        <android.support.v7.widget.AppCompatImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true" />

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:gravity="center"
            android:orientation="horizontal"
            tools:ignore="RtlHardcoded">

            <android.support.v7.widget.AppCompatImageView
                style="@style/RippleEffect"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginRight="5dp"
                android:padding="7dp" />

            <android.support.v7.widget.AppCompatImageView
                style="@style/RippleEffect"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginRight="5dp"
                android:padding="7dp"
                android:src="@drawable/ic_search" />
        </LinearLayout>
    </RelativeLayout>

    <android.support.design.widget.TabLayout
        android:id="@+id/homeTabLayout"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:background="@color/toolbarColor"
        app:layout_constraintTop_toBottomOf="@+id/homeToolbar"
        app:tabMode="scrollable" />

    <android.support.v4.view.ViewPager
        android:id="@+id/homeViewPager"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:layout_constraintTop_toBottomOf="@+id/homeTabLayout"

        />
</android.support.constraint.ConstraintLayout>

这就是我所看到的(见最后一个达斯维德)

【问题讨论】:

  • 我也遇到过类似的情况,确实是工具栏的原因。我使工具栏可滚动,它就这样固定了。您可以使您的工具栏可滚动或在 viewpager 中使用 paddingBottom
  • 使用paddingBottom 不是解决方案,因为我不想计算视图的缺失底部。这将是一个快速的解决方法,但不是解决方案,您不同意吗?
  • 刚刚得到答案@Lukingan。我刚刚发布了解决方案

标签: android android-recyclerview android-viewpager android-constraintlayout


【解决方案1】:

添加解决

app:layout_constraintBottom_toBottomOf="parent"

到 ViewPager

【讨论】:

  • 太好了,你解决了它。但是为什么你把 TabLayout 放在 ViewPager 标签之外呢? Reference on developer.android.com明确描述了TabLayout应该放在ViewPager里面
  • "如果您将 ViewPager 与此布局一起使用,您可以调用 setupWithViewPager(ViewPager) 将两者链接在一起。此布局将自动从 PagerAdapter 的页面标题填充"然后"此视图还支持用作 ViewPager 装饰的一部分,并且可以像这样在布局资源文件中直接添加到 ViewPager 中”,这意味着您可以采用任何一种方式,但感谢您澄清这一点。
猜你喜欢
  • 1970-01-01
  • 2017-07-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-29
  • 2017-03-29
  • 2015-08-12
相关资源
最近更新 更多