RecyclerView不是真正在ScrollView上wrap_content [英] RecyclerView is not trully wrap_content at the ScrollView
问题描述
我有一个ScrollView,
,其中包含一个垂直的LinearLayout.
.在这里,我添加了一些称为"Section"的视图. "Section"是一个LinearLayout,
,其中包含一个TextView
和`RecyclerView.
I have a ScrollView,
which contains a vertical LinearLayout.
This is a place, where I add some amount of Views called "Section". "Section" is a LinearLayout,
which contains a TextView
and `RecyclerView.
<ScrollView
android:id="@+id/main_scrollview"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/sections_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" />
</ScrollView>
部分:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:textSize="@dimen/activity_starred_title_textsize" />
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
问题是RecyclerView
有时不是真正的wrap_content
.因此,它会产生2种类型的问题(取决于我尝试使用的解决方案类型).
The problem is that RecyclerView
is not truly wrap_content
sometimes. Therefore it creates 2 types of problems (depending on solution type I'm trying to use).
-
它可能无法滚动(因此它与屏幕高度匹配,因此我无法向下滚动以查看其中的其他项).
It can be unscrollable (so it matches screen height and I can't scroll it down to see rest of items inside).
它可以滚动嵌套.
最初的问题是它具有嵌套滚动.所以我希望RecyclerViews
成为简单的垂直LinearLayouts
,唯一具有滚动效果的是根ScrollView.
Originally problem is that it has nested scrolling. So I want RecyclerViews
to be as simple vertical LinearLayouts
and the only thing that has to have scrolling effect is root ScrollView.
我试图做什么?
-
扩展
GridLayoutManager
并覆盖canScrollVertically().
方法:
布尔布尔值canScrollVertically(){ 返回false; }
public boolean canScrollVertically(){ return false; }
扩展RecyclerView
类并覆盖
@Override 公共布尔onInterceptTouchEvent(MotionEvent事件){ 返回false; }
@Override public boolean onInterceptTouchEvent(MotionEvent event){ return false; }
@Override 公共布尔onTouchEvent(MotionEvent事件){ 返回false; }
@Override public boolean onTouchEvent(MotionEvent event){ return false; }
在可能的任何地方通过xml
禁用NestedScrolling
.
Disable NestedScrolling
via xml
everywhere, where it is possible.
使用以下解决方案覆盖GridLayoutManager
:解决方案
Override GridLayoutManager
using this solution: SOLUTION
推荐答案
请勿在ScrollView
内使用RecyclerView
或ListView
.对于嵌套滚动,应使用NestedScrollView
.
Don't use RecyclerView
or ListView
inside ScrollView
. For nested scrolling you should use NestedScrollView
.
NestedScrollView
就像ScrollView
一样,但是它支持充当 在新版本和旧版本上均使用nested
滚动父级和子级 Android.默认情况下,嵌套滚动处于启用状态.
NestedScrollView
is just likeScrollView
, but it supports acting as both anested
scrolling parent and child on both new and old versions of Android. Nested scrolling is enabled by default.
解决方案:
1..不是使用ScrollView
,而是将NestedScrollView
用作Section
零件(RecyclerView
和其他Views
)的容器.
1. Instead of using ScrollView
, use NestedScrollView
as container of your Section
part(RecyclerView
and other Views
).
<android.support.v4.widget.NestedScrollView
android:id="@+id/main_scrollview"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/sections_layout"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<!-- Your Section:: RecyclerView and other Views -->
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
2..对于滚动问题,请在您的RecyclerView
上使用setNestedScrollingEnabled(false)
.
2. For scrolling issue use setNestedScrollingEnabled(false)
to your RecyclerView
.
这篇关于RecyclerView不是真正在ScrollView上wrap_content的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!