ViewPager 未显示在 NestedScrollView 内部,其中包含 ViewPager 下面的 RecyclerView
ViewPager not showing inside NestedScrollView which contains RecyclerView below ViewPager
我有一个 NestedScrollView,它包含一个 ConstaintLayout,里面有一个 Viewpager 和一个 Recyclerview
我的问题是 ViewPager 不显示,除非它有固定的高度,match_parent 和 wrap_content 不工作
我尝试将其高度设置为 wrap_content 并测量它为最高 child 的高度但仍然没有用,我一直在更改 fillViewport 值和其他视图'高度,将 parent 布局从 ConstaintLayout 更改为 LinearLayout 并使其 layout_weight="1" 但仍然唯一有效的是将其高度设置为固定高度
我的布局:
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/coordinatorlayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorWhite"
>
<android.support.design.widget.AppBarLayout
android:id="@+id/apSelectedLine"
android:layout_width="match_parent"
android:layout_height="350dp"
android:background="@color/colorWhite">
<android.support.design.widget.CollapsingToolbarLayout
android:id="@+id/collapsing_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:minHeight="250dp"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_collapseMode="parallax" />
<android.support.v7.widget.CardView
android:id="@+id/cvStep2Loading"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="0dp"
android:background="@color/colorWhite"
android:backgroundTint="@color/colorWhite"
android:minWidth="100dp"
android:minHeight="40dp"
android:visibility="gone"
app:cardBackgroundColor="@color/colorWhite"
app:cardCornerRadius="8dp"
app:cardElevation="16dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ProgressBar
android:id="@+id/pbStep2Loading"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="32dp"
android:layout_height="32dp"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="8dp"
android:background="@drawable/circle_shape"
android:indeterminate="false"
android:max="100"
android:padding="4dp"
android:progress="0"
android:progressDrawable="@drawable/circular_progress" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="8dp"
android:gravity="center"
android:text="loading.."
android:textColor="@color/grey" />
</LinearLayout>
</android.support.v7.widget.CardView>
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v4.widget.NestedScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/nsBusesAndStops"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorWhite"
android:fillViewport="false"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/llNestSelectedLine"
android:visibility="visible">
<me.relex.circleindicator.CircleIndicator
android:id="@+id/indicator"
android:layout_width="match_parent"
android:layout_height="8dp"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:background="@color/colorWhite"
android:backgroundTint="@color/colorWhite"
app:ci_drawable_unselected="@drawable/unselected_grey_circle"
app:ci_drawable="@drawable/selected_black_circle"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintStart_toStartOf="parent"/>
<android.support.v4.view.ViewPager
android:id="@+id/vpSelectedLine"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorWhite"
app:layout_constraintTop_toBottomOf="@id/indicator"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintBottom_toTopOf="@id/rvStops"
>
</android.support.v4.view.ViewPager>
<android.support.v7.widget.RecyclerView
android:id="@+id/rvStops"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorWhite"
android:backgroundTint="@color/colorWhite"
android:nestedScrollingEnabled="false"
app:layout_constraintTop_toBottomOf="@id/vpSelectedLine"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
/>
</android.support.constraint.ConstraintLayout>
</android.support.v4.widget.NestedScrollView>
</android.support.design.widget.CoordinatorLayout>
我不能让它固定高度,因为 Viewpager 包含一个动态的 RecyclerView...那么如何在不固定 ViewPager 高度的情况下解决这个问题?
尝试将 ViewPager
和 RecyclerView
放在 FitWindowsFrameLayout
和 layout_height="wrap_content"
中,这样你的代码看起来像这样
<android.support.design.widget.CoordinatorLayout
...
<android.support.v4.widget.NestedScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/nsBusesAndStops"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorWhite"
android:fillViewport="false"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/llNestSelectedLine"
android:visibility="visible">
<me.relex.circleindicator.CircleIndicator
android:id="@+id/indicator"
android:layout_width="match_parent"
android:layout_height="8dp"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:background="@color/colorWhite"
android:backgroundTint="@color/colorWhite"
app:ci_drawable_unselected="@drawable/unselected_grey_circle"
app:ci_drawable="@drawable/selected_black_circle"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintStart_toStartOf="parent"/>
<android.support.v7.widget.FitWindowsFrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:ignore="MissingConstraints">
<android.support.v4.view.ViewPager
android:id="@+id/vpSelectedLine"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorWhite"
app:layout_constraintTop_toBottomOf="@id/indicator"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintBottom_toTopOf="@id/rvStops"
>
</android.support.v4.view.ViewPager>
<android.support.v7.widget.RecyclerView
android:id="@+id/rvStops"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorWhite"
android:backgroundTint="@color/colorWhite"
android:nestedScrollingEnabled="false"
app:layout_constraintTop_toBottomOf="@id/vpSelectedLine"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintBottom_toBottomOf="parent"/>
</android.support.v7.widget.FitWindowsFrameLayout>
</android.support.constraint.ConstraintLayout>
</android.support.v4.widget.NestedScrollView>
</android.support.design.widget.CoordinatorLayout>
有关如何使用的更多信息FitWindowsFrameLayout
this googlesource link
在尝试解决这个问题将近一天后,我自己想出了这个解决方案,我最初在 xml 中将 ViewPager 高度设置为固定高度(例如 100dp),然后当数据来自服务器时我将 ViewPager 的高度调整为儿童的总高度,请注意我在 ViewPager 中有一个 RecyclerView,这是我为此使用的 Kotlin 代码:
fun resizeViewPager(busesList: MutableList<Bus>){
childRecyclerView?.viewTreeObserver?.addOnGlobalLayoutListener(object : ViewTreeObserver.OnGlobalLayoutListener{
override fun onGlobalLayout() {
val firstChild = childRecyclerView?.getChildAt(0)
val height = firstChild?.height
if (height != null){
val totalHeight = height * busesList.size
val layoutParams = viewPager.layoutParams
layoutParams.height = totalHeight
viewPager.layoutParams = layoutParams
viewPager.requestLayout()
}
childRecyclerView?.viewTreeObserver?.removeOnGlobalLayoutListener(this)
}
})
}
谁能解释一下为什么会出现这个问题?有更好的解决方案吗?
我有一个 NestedScrollView,它包含一个 ConstaintLayout,里面有一个 Viewpager 和一个 Recyclerview 我的问题是 ViewPager 不显示,除非它有固定的高度,match_parent 和 wrap_content 不工作
我尝试将其高度设置为 wrap_content 并测量它为最高 child 的高度但仍然没有用,我一直在更改 fillViewport 值和其他视图'高度,将 parent 布局从 ConstaintLayout 更改为 LinearLayout 并使其 layout_weight="1" 但仍然唯一有效的是将其高度设置为固定高度
我的布局:
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/coordinatorlayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorWhite"
>
<android.support.design.widget.AppBarLayout
android:id="@+id/apSelectedLine"
android:layout_width="match_parent"
android:layout_height="350dp"
android:background="@color/colorWhite">
<android.support.design.widget.CollapsingToolbarLayout
android:id="@+id/collapsing_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:minHeight="250dp"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_collapseMode="parallax" />
<android.support.v7.widget.CardView
android:id="@+id/cvStep2Loading"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="0dp"
android:background="@color/colorWhite"
android:backgroundTint="@color/colorWhite"
android:minWidth="100dp"
android:minHeight="40dp"
android:visibility="gone"
app:cardBackgroundColor="@color/colorWhite"
app:cardCornerRadius="8dp"
app:cardElevation="16dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ProgressBar
android:id="@+id/pbStep2Loading"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="32dp"
android:layout_height="32dp"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="8dp"
android:background="@drawable/circle_shape"
android:indeterminate="false"
android:max="100"
android:padding="4dp"
android:progress="0"
android:progressDrawable="@drawable/circular_progress" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="8dp"
android:gravity="center"
android:text="loading.."
android:textColor="@color/grey" />
</LinearLayout>
</android.support.v7.widget.CardView>
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v4.widget.NestedScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/nsBusesAndStops"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorWhite"
android:fillViewport="false"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/llNestSelectedLine"
android:visibility="visible">
<me.relex.circleindicator.CircleIndicator
android:id="@+id/indicator"
android:layout_width="match_parent"
android:layout_height="8dp"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:background="@color/colorWhite"
android:backgroundTint="@color/colorWhite"
app:ci_drawable_unselected="@drawable/unselected_grey_circle"
app:ci_drawable="@drawable/selected_black_circle"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintStart_toStartOf="parent"/>
<android.support.v4.view.ViewPager
android:id="@+id/vpSelectedLine"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorWhite"
app:layout_constraintTop_toBottomOf="@id/indicator"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintBottom_toTopOf="@id/rvStops"
>
</android.support.v4.view.ViewPager>
<android.support.v7.widget.RecyclerView
android:id="@+id/rvStops"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorWhite"
android:backgroundTint="@color/colorWhite"
android:nestedScrollingEnabled="false"
app:layout_constraintTop_toBottomOf="@id/vpSelectedLine"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
/>
</android.support.constraint.ConstraintLayout>
</android.support.v4.widget.NestedScrollView>
</android.support.design.widget.CoordinatorLayout>
我不能让它固定高度,因为 Viewpager 包含一个动态的 RecyclerView...那么如何在不固定 ViewPager 高度的情况下解决这个问题?
尝试将 ViewPager
和 RecyclerView
放在 FitWindowsFrameLayout
和 layout_height="wrap_content"
中,这样你的代码看起来像这样
<android.support.design.widget.CoordinatorLayout
...
<android.support.v4.widget.NestedScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/nsBusesAndStops"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorWhite"
android:fillViewport="false"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/llNestSelectedLine"
android:visibility="visible">
<me.relex.circleindicator.CircleIndicator
android:id="@+id/indicator"
android:layout_width="match_parent"
android:layout_height="8dp"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:background="@color/colorWhite"
android:backgroundTint="@color/colorWhite"
app:ci_drawable_unselected="@drawable/unselected_grey_circle"
app:ci_drawable="@drawable/selected_black_circle"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintStart_toStartOf="parent"/>
<android.support.v7.widget.FitWindowsFrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:ignore="MissingConstraints">
<android.support.v4.view.ViewPager
android:id="@+id/vpSelectedLine"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorWhite"
app:layout_constraintTop_toBottomOf="@id/indicator"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintBottom_toTopOf="@id/rvStops"
>
</android.support.v4.view.ViewPager>
<android.support.v7.widget.RecyclerView
android:id="@+id/rvStops"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorWhite"
android:backgroundTint="@color/colorWhite"
android:nestedScrollingEnabled="false"
app:layout_constraintTop_toBottomOf="@id/vpSelectedLine"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintBottom_toBottomOf="parent"/>
</android.support.v7.widget.FitWindowsFrameLayout>
</android.support.constraint.ConstraintLayout>
</android.support.v4.widget.NestedScrollView>
</android.support.design.widget.CoordinatorLayout>
有关如何使用的更多信息FitWindowsFrameLayout
this googlesource link
在尝试解决这个问题将近一天后,我自己想出了这个解决方案,我最初在 xml 中将 ViewPager 高度设置为固定高度(例如 100dp),然后当数据来自服务器时我将 ViewPager 的高度调整为儿童的总高度,请注意我在 ViewPager 中有一个 RecyclerView,这是我为此使用的 Kotlin 代码:
fun resizeViewPager(busesList: MutableList<Bus>){
childRecyclerView?.viewTreeObserver?.addOnGlobalLayoutListener(object : ViewTreeObserver.OnGlobalLayoutListener{
override fun onGlobalLayout() {
val firstChild = childRecyclerView?.getChildAt(0)
val height = firstChild?.height
if (height != null){
val totalHeight = height * busesList.size
val layoutParams = viewPager.layoutParams
layoutParams.height = totalHeight
viewPager.layoutParams = layoutParams
viewPager.requestLayout()
}
childRecyclerView?.viewTreeObserver?.removeOnGlobalLayoutListener(this)
}
})
}
谁能解释一下为什么会出现这个问题?有更好的解决方案吗?