两个回收视图合二为一 activity 滚动问题
Two recycleviews in one activity scroll problem
我在一个 activity 中创建了 2 个回收视图。一个水平滚动,而另一个垂直滚动。我可以在每个 RecyclerView 内正确滚动,但整个页面不会滚动,即顶部 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>
我阅读了这篇 post“”并以编程方式设置垂直回收视图的高度。像这样
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);
但问题是我如何根据child个数来计算RecyclerView的高度?
使用 NestedScrollView
而不是 ScrollView
并在 Activity
.
中的 Fragment
中使用以下行
ViewCompat.setNestedScrollingEnabled(mYourRecycleView, false);
这将适用于您的所有 Android API 水平。
将ScrollView
替换为NestedScrollView
然后添加:horizontaltrendingrecycleview.isNestedScrollingEnabled = false
您的 XML 应如下所示:
<android.support.v4.widget.NestedScrollView
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:fillViewport="true"
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"/>
<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"/>
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
我在一个 activity 中创建了 2 个回收视图。一个水平滚动,而另一个垂直滚动。我可以在每个 RecyclerView 内正确滚动,但整个页面不会滚动,即顶部 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>
我阅读了这篇 post“
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);
但问题是我如何根据child个数来计算RecyclerView的高度?
使用 NestedScrollView
而不是 ScrollView
并在 Activity
.
Fragment
中使用以下行
ViewCompat.setNestedScrollingEnabled(mYourRecycleView, false);
这将适用于您的所有 Android API 水平。
将ScrollView
替换为NestedScrollView
然后添加:horizontaltrendingrecycleview.isNestedScrollingEnabled = false
您的 XML 应如下所示:
<android.support.v4.widget.NestedScrollView
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:fillViewport="true"
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"/>
<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"/>
</LinearLayout>
</android.support.v4.widget.NestedScrollView>