我在一项活动中创建了 2 个回收视图。一个水平滚动,而另一个垂直滚动。我可以在每个 RecyclerView 内正确滚动,但整个页面不会滚动,即顶部 RecyclerView 始终位于顶部,底部始终位于底部,就像两者都固定在适当的位置。
<ScrollView
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"
android:orientation="vertical"
tools:context="com.shakeelnawaz.recipes.AllRecipesFragmet">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_marginStart="20dp"
android:layout_marginTop="20dp"
android:text="@string/trending_recipes"
android:textSize="18sp" />
<android.support.v7.widget.RecyclerView
android:id="@+id/horizontaltrendingrecycleview"
android:layout_width="match_parent"
android:layout_height="240dp"
android:layout_marginStart="15dp"
android:orientation="horizontal"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
</android.support.v7.widget.RecyclerView>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:text="@string/all_recipes"
android:textSize="18sp" />
<android.support.v7.widget.RecyclerView
android:id="@+id/recycleView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
</android.support.v7.widget.RecyclerView>
</LinearLayout>
</ScrollView>
我阅读了这篇文章“ Scolling with multiple RecyclerViews in Layout ”并以编程方式设置垂直回收视图的高度。像这样
LinearLayout.LayoutParams params = new
LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
// calculate height of RecyclerView based on child count
params.height=1150;
// set height of RecyclerView
recyclerView.setLayoutParams(params);
但问题是我如何根据子计数计算 RecyclerView 的高度?
在您的in 中使用NestedScrollView
代替ScrollView
并使用以下行。Fragment
Activity
ViewCompat.setNestedScrollingEnabled(mYourRecycleView, false);
这将适用于您的所有 Android API 级别。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句