在包含在ViewPager下面的RecyclerView的NestedScrollView内部未显示ViewPager

时间:2019-04-23 20:36:25

标签: android xml android-recyclerview android-viewpager nestedscrollview

我有一个NestedScrollView,其中包含一个ConstaintLayout,并且在其中有一个Viewpager和Recyclerview 我的问题是,除非具有固定的高度,否则ViewPager不会显示,match_parent和wrap_content无法工作

我尝试将其高度设置为wrap_content并将其测量为最高的孩子的身高,但仍然无法正常工作,我一直在更改fillViewport值和其他视图的高度,将父布局从ConstaintLayout更改为LinearLayout并其layout_weight =“ 1”,但唯一可行的方法是将其高度设置为固定高度

我的布局:

<div id="my-carousel-bg": class="carousel-inner">

由于Viewpager包含动态的RecyclerView,因此我不能将其设置为固定高度...那么如何在不使ViewPager高度固定的情况下解决该问题呢?

2 个答案:

答案 0 :(得分:0)

尝试将name = input("What is your name?") print("Hello {}".format(name)) ViewPagerRecyclerView放在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> this googlesource link

答案 1 :(得分:0)

经过近一天的尝试,我自己想出了一个解决方案,我首先将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)
            }
        })


    }

谁能解释为什么这个问题首先发生?有更好的解决方案吗?