我正在重构项目中的个人资料页面。个人资料页面(屏幕)是一个片段。它具有包含ViewPager的NestedScrollView(viewpager具有2个选项卡和2个片段)。第一个标签(片段)显示3个不同的recyclerviews,每个recyclerview限于3个项目或行。 每个recyclerview都在另一个片段内。顶部的recyclerview以水平样式显示卡片,其余为垂直样式。
底部的recyclerview调用api来获取数据并填充项目,但是在成功获取项目后,它根本没有扩展或显示内容(如果高度恒定,则显示内容)。
顶部的recyclerview(水平),它也调用api来获取数据和填充项目,如果将其设置为LinearLayoutManager.VERTICAL,则可以很好地显示输入代码。问题是,如果将其设置为“水平”,则不会显示任何内容。
中间的recyclerview正在从本地获取数据,并始终显示数据并滚动良好。问题在于顶部和底部的recyclerviews
如果我将viewpager的高度设置为例如1000dp。 recyclerviews数据的显示效果很好,但显然viewpager片段底部有多余的空间。如果我们将其设置为wrap_content,则top(Horizontal)和bottom(Vertical)回收视图不显示数据。
注意:每个Recyclerview都在一个片段内。因此viewpager片段内部有三个片段
尝试过nestedScrollingEnabled = true和可动态获取的增强型viewpager测量片段的高度。对我没用。
`EnhancedWrapContentViewPager类:ViewPager {
constructor(context: Context) : super(context) {}
constructor(context: Context, attrs: AttributeSet) : super(context, attrs) {}
override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
var heightMeasureSpec = heightMeasureSpec
val mode = MeasureSpec.getMode(heightMeasureSpec)
// Unspecified means that the ViewPager is in a ScrollView WRAP_CONTENT.
// At Most means that the ViewPager is not in a ScrollView WRAP_CONTENT.
if (mode == MeasureSpec.UNSPECIFIED || mode == MeasureSpec.AT_MOST) {
// super has to be called in the beginning so the child views can be initialized.
super.onMeasure(widthMeasureSpec, heightMeasureSpec)
var height = 0
for (i in 0 until childCount) {
val child = getChildAt(i)
child.measure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED))
val childMeasuredHeight = child.measuredHeight
if (childMeasuredHeight > height) {
height = childMeasuredHeight
}
}
heightMeasureSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY)
}
// super has to be called again so the new specs are treated as exact measurements
super.onMeasure(widthMeasureSpec, heightMeasureSpec)
}
}
<android.support.v4.widget.NestedScrollView
android:id="@+id/scrollView2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white"
android:fillViewport="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="@dimen/element_spacing_default"
android:orientation="vertical">
<android.support.design.widget.AppBarLayout
android:id="@+id/appBarLayout"
style="@style/Theme.Design.NoActionBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:elevation="0dp"
android:background="@color/white">
<android.support.design.widget.TabLayout
android:id="@+id/tabLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white"
app:tabIndicatorColor="@color/black"
app:tabMode="fixed"
app:tabSelectedTextColor="@color/black"
app:tabTextAppearance="@style/tab_text_bold"
app:tabTextColor="@color/dark_grey" />
</android.support.design.widget.AppBarLayout>
<com.eatsleepride.esrandroid.ui.EnhancedWrapContentViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</android.support.v4.widget.NestedScrollView>`
我希望Recyclerviews内容会显示并随着大小的增加而自动扩展,以便我们可以滚动浏览3个Recyclerviews内容并使用NestedScrollView