垂直 ScrollView 内的水平 RecyclerView
Horizontal RecyclerView inside vertical ScrollView
所以我在垂直 ScrollView 中有一个水平 RecyclerView。我的布局中的所有内容都显示良好,并且都按我想要的方向滚动,并且运行顺利。
我遇到的唯一问题是,RecyclerView 在 ScrollView 中的一些其他内容下方,当 RecyclerView 部分可见时,它会在启动时将 RecyclerView 的底部与屏幕底部对齐。这意味着 RecyclerView 上面的内容被推离了屏幕。
有谁知道为什么会这样,我该如何解决?
这是一个简单的布局,可以完成我刚才描述的内容。您甚至不需要填充 RecyclerView,它仍然会这样做。
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="500dp"
android:background="#fff"/>
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="200dp"
android:background="#000"/>
</LinearLayout>
</ScrollView>
原来这个问题已报告给 Google 这里 Issue - 81854
根据 Google,它正在按预期工作。问题在于 RecyclerView 已将 focusableInTouchMode
设置为 true。为了解决这个问题,我在 ScrollView 的最顶层视图上将 focusableInTouchMode
和 focusable
设置为 true。
以下是我在原始问题中提供的代码示例的修复:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="500dp"
android:background="#fff"
android:focusableInTouchMode="true"
android:focusable="true"/>
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="200dp"
android:background="#000"/>
</LinearLayout>
</ScrollView>
我一直在寻找解决方案。最后,我偶然决定了。
在你的 activity 中,找到 ScrollView 并为 neo 添加一个触摸监听器。一切都会正常运作
ScrollView scroll = findViewById(R.id.scroll);
scroll.setOnTouchListener((view, motionEvent) -> {
return false;
});
所以我在垂直 ScrollView 中有一个水平 RecyclerView。我的布局中的所有内容都显示良好,并且都按我想要的方向滚动,并且运行顺利。
我遇到的唯一问题是,RecyclerView 在 ScrollView 中的一些其他内容下方,当 RecyclerView 部分可见时,它会在启动时将 RecyclerView 的底部与屏幕底部对齐。这意味着 RecyclerView 上面的内容被推离了屏幕。
有谁知道为什么会这样,我该如何解决?
这是一个简单的布局,可以完成我刚才描述的内容。您甚至不需要填充 RecyclerView,它仍然会这样做。
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="500dp"
android:background="#fff"/>
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="200dp"
android:background="#000"/>
</LinearLayout>
</ScrollView>
原来这个问题已报告给 Google 这里 Issue - 81854
根据 Google,它正在按预期工作。问题在于 RecyclerView 已将 focusableInTouchMode
设置为 true。为了解决这个问题,我在 ScrollView 的最顶层视图上将 focusableInTouchMode
和 focusable
设置为 true。
以下是我在原始问题中提供的代码示例的修复:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="500dp"
android:background="#fff"
android:focusableInTouchMode="true"
android:focusable="true"/>
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="200dp"
android:background="#000"/>
</LinearLayout>
</ScrollView>
我一直在寻找解决方案。最后,我偶然决定了。 在你的 activity 中,找到 ScrollView 并为 neo 添加一个触摸监听器。一切都会正常运作
ScrollView scroll = findViewById(R.id.scroll);
scroll.setOnTouchListener((view, motionEvent) -> {
return false;
});