[英]Recycler view inside NestedScrollView causes scroll to start in the middle
当我在 NestedScrollView 中添加 RecyclerView 时,出现奇怪的滚动行为。
发生的情况是,只要滚动视图的行数超过屏幕上可以显示的行数,一旦启动活动,NestedScrollView 就会从顶部开始偏移(图 1)。 如果滚动视图中的项目很少,以便可以一次显示它们,则不会发生这种情况(图 2)。
我正在使用支持库的 23.2.0 版。
图 1 :错误 - 从顶部偏移开始
图 2 :正确 - 回收商视图中的项目很少
我粘贴在我的布局代码下面:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="fill_vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="10dp">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Title:"
style="@style/TextAppearance.AppCompat.Caption"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="@dimen/bodyPadding"
style="@style/TextAppearance.AppCompat.Body1"
android:text="Neque porro quisquam est qui dolorem ipsum"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Subtitle:"
style="@style/TextAppearance.AppCompat.Caption"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="@style/TextAppearance.AppCompat.Body1"
android:padding="@dimen/bodyPadding"
android:text="Neque porro quisquam est qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit..."/>
</LinearLayout>
<android.support.v7.widget.RecyclerView
android:id="@+id/rv"
android:focusable="false"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
我错过了什么吗? 有谁知道如何解决这个问题?
更新 1
如果我在初始化我的活动时放置以下代码,它可以正常工作:
sv.post(new Runnable() {
@Override
public void run() {
sv.scrollTo(0,0);
}
});
其中 sv 是对 NestedScrollView 的引用,但它看起来像是一个黑客。
更新 2
根据要求,这是我的适配器代码:
public abstract class ArrayAdapter<T, VH extends RecyclerView.ViewHolder>
extends RecyclerView.Adapter<VH> {
private List<T> mObjects;
public ArrayAdapter(final List<T> objects) {
mObjects = objects;
}
/**
* Adds the specified object at the end of the array.
*
* @param object The object to add at the end of the array.
*/
public void add(final T object) {
mObjects.add(object);
notifyItemInserted(getItemCount() - 1);
}
/**
* Remove all elements from the list.
*/
public void clear() {
final int size = getItemCount();
mObjects.clear();
notifyItemRangeRemoved(0, size);
}
@Override
public int getItemCount() {
return mObjects.size();
}
public T getItem(final int position) {
return mObjects.get(position);
}
public long getItemId(final int position) {
return position;
}
/**
* Returns the position of the specified item in the array.
*
* @param item The item to retrieve the position of.
* @return The position of the specified item.
*/
public int getPosition(final T item) {
return mObjects.indexOf(item);
}
/**
* Inserts the specified object at the specified index in the array.
*
* @param object The object to insert into the array.
* @param index The index at which the object must be inserted.
*/
public void insert(final T object, int index) {
mObjects.add(index, object);
notifyItemInserted(index);
}
/**
* Removes the specified object from the array.
*
* @param object The object to remove.
*/
public void remove(T object) {
final int position = getPosition(object);
mObjects.remove(object);
notifyItemRemoved(position);
}
/**
* Sorts the content of this adapter using the specified comparator.
*
* @param comparator The comparator used to sort the objects contained in this adapter.
*/
public void sort(Comparator<? super T> comparator) {
Collections.sort(mObjects, comparator);
notifyItemRangeChanged(0, getItemCount());
}
}
这是我的 ViewHolder:
public class ViewHolder extends RecyclerView.ViewHolder {
private TextView txt;
public ViewHolder(View itemView) {
super(itemView);
txt = (TextView) itemView;
}
public void render(String text) {
txt.setText(text);
}
}
这是 RecyclerView 中每个项目的布局(它只是android.R.layout.simple_spinner_item
- 此屏幕仅用于显示此错误的示例):
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
style="?android:attr/spinnerItemStyle"
android:singleLine="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ellipsize="marquee"
android:textAlignment="inherit"/>
我通过设置解决了这个问题:
<ImageView ...
android:focusableInTouchMode="true"/>
到我在 RecyclerView 上方的视图(在不需要的滚动后隐藏)。 尝试将此属性设置为 RecyclerView 上方的 LinearLayout 或作为 RecyclerView 容器的 LinearLayout (在另一种情况下帮助我)。
正如我在 NestedScrollView 源中看到的那样,它试图将第一个可能的孩子集中在 onRequestFocusInDescendants 中,如果只有 RecyclerView 是可聚焦的,它就会获胜。
编辑(感谢 Waran):为了平滑滚动不要忘记设置yourRecyclerView.setNestedScrollingEnabled(false);
在NestedScrollView
之后的LinearLayout
中,按以下方式使用android:descendantFocusability
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="10dp"
android:descendantFocusability="blocksDescendants">
编辑
由于他们中的许多人得到这个答案很有用,因此也会提供解释。
此处给出了descendantFocusability
的使用。 和这里的focusableInTouchMode
。 因此,在descendantFocusability
使用blocksDescendants
不允许它的孩子在触摸时获得焦点,因此可以停止计划外的行为。
对于focusInTouchMode
, AbsListView
和RecyclerView
调用了setFocusableInTouchMode(true);
方法setFocusableInTouchMode(true);
默认情况下在它们的构造函数中,因此不需要在您的 XML 布局中使用该属性。
对于NestedScrollView
以下方法:
private void initScrollView() {
mScroller = ScrollerCompat.create(getContext(), null);
setFocusable(true);
setDescendantFocusability(FOCUS_AFTER_DESCENDANTS);
setWillNotDraw(false);
final ViewConfiguration configuration = ViewConfiguration.get(getContext());
mTouchSlop = configuration.getScaledTouchSlop();
mMinimumVelocity = configuration.getScaledMinimumFlingVelocity();
mMaximumVelocity = configuration.getScaledMaximumFlingVelocity();
}
在这里,使用setFocusable()
方法代替setFocusableInTouchMode()
。 但根据这一职位, focusableInTouchMode
应避免,除非某些条件,它打破了Android正常行为的一致性。 游戏是一个很好的应用程序示例,它可以很好地利用可聚焦的触摸模式属性。 MapView,如果在谷歌地图中全屏使用,是另一个很好的例子,你可以在触摸模式下正确使用focusable。
android:descendantFocusability="blocksDescendants"
里面 LinearLayout 为我工作。
我遇到了同样的问题,并通过扩展 NestedScrollView 和禁用聚焦孩子来解决。 出于某种原因,即使我刚刚打开和关闭抽屉,RecyclerView 也总是请求焦点。
public class DummyNestedScrollView extends NestedScrollView {
public DummyNestedScrollView(Context context) {
super(context);
}
public DummyNestedScrollView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
}
public DummyNestedScrollView(Context context, @Nullable AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
/**
* Fixind problem with recyclerView in nested scrollview requesting focus
* http://stackoverflow.com/questions/36314836/recycler-view-inside-nestedscrollview-causes-scroll-to-start-in-the-middle
* @param child
* @param focused
*/
@Override
public void requestChildFocus(View child, View focused) {
Log.d(getClass().getSimpleName(), "Request focus");
//super.requestChildFocus(child, focused);
}
/**
* http://stackoverflow.com/questions/36314836/recycler-view-inside-nestedscrollview-causes-scroll-to-start-in-the-middle
* @param direction
* @param previouslyFocusedRect
* @return
*/
@Override
protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) {
Log.d(getClass().getSimpleName(), "Request focus descendants");
//return super.onRequestFocusInDescendants(direction, previouslyFocusedRect);
return false;
}
}
就我而言,此代码解决了我的问题
RecyclerView recyclerView = findViewById(R.id.recyclerView);
NestedScrollView nestedScrollView= findViewById(R.id.nestedScrollView);
recyclerView.setFocusable(false);
nestedScrollView.requestFocus();
//populate recyclerview here
我的布局包含一个作为 NestedScrollView 的父布局,它有一个子 LinearLayout。 LinearLayout 具有“垂直”方向和子 RecyclerView 和 EditText。 参考
只需在 NestedScrollView 内的 ViewGroup 上添加android:descendantFocusability="blocksDescendants"
即可。
我有两个猜测。
第一:尝试将此行放在您的 NestedScrollView 上
app:layout_behavior="@string/appbar_scrolling_view_behavior"
第二:使用
<android.support.design.widget.CoordinatorLayout
作为你的父母认为像这样
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="fill_vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="10dp">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="16dp">
<TextView
style="@style/TextAppearance.AppCompat.Caption"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Title:"/>
<TextView
style="@style/TextAppearance.AppCompat.Body1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="@dimen/bodyPadding"
android:text="Neque porro quisquam est qui dolorem ipsum"/>
<TextView
style="@style/TextAppearance.AppCompat.Caption"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Subtitle:"/>
<TextView
style="@style/TextAppearance.AppCompat.Body1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="@dimen/bodyPadding"
android:text="Neque porro quisquam est qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit..."/>
</LinearLayout>
<android.support.v7.widget.RecyclerView
android:id="@+id/rv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:focusable="false"/>
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
我最后一个可能的解决方案。 我发誓:)
由于回收视图焦点而出现此问题。
如果其大小扩展了屏幕的大小,则所有焦点都会自动转到回收视图。
将android:focusableInTouchMode="true"
到第一个 ChildView 像TextView
、 Button
等等(而不是像Linear
、 Relative
等的ViewGroup
上)对解决问题很有意义,但 API 级别 25 及以上的解决方案不起作用。
只需在您的 ChildView 中添加这 2 行,如TextView
、 Button
等(而不是像Linear
、 Relative
等在ViewGroup
上)
android:focusableInTouchMode="true"
android:focusable="true"
我刚刚在 API 级别 25 上遇到了这个问题。我希望其他人不要在这上面浪费时间。
为了在 RecycleView 上平滑滚动,添加这一行
android:nestedScrollingEnabled="false"
但添加此属性仅适用于 API 级别 21 或更高级别。 如果您希望在 API 级别 25 以下进行平滑滚动工作,请在您的课程中添加这一行
mList = findViewById(R.id.recycle_list);
ViewCompat.setNestedScrollingEnabled(mList, false);
在 Java 代码中,在初始化 recyclerView 并设置适配器后,添加以下行:
recyclerView.setNestedScrollingEnabled(false)
您还可以尝试使用 relativeLayout 包装布局,以便视图保持在相同的位置,但 recyclerView(滚动)在 xml 层次结构中是第一个。 最后的建议是绝望的尝试:p
由于我回复晚了,但可能可以帮助其他人。 只需在您的应用级别 build.gradle 中使用以下或更高版本,问题就会被删除。
compile com.android.support:recyclerview-v7:23.2.1
要滚动到顶部,只需在setcontentview
调用它:
scrollView.SmoothScrollTo(0, 0);
请不要使用
android:descendantFocusability="blocksDescendants"
因为 ViewGroup 将阻止其后代接收焦点。 这是一个可访问性问题。
如果您想避免nestedscrollview
接收焦点,您可以添加一个虚拟布局,该布局可在nestedscrollview
上方nestedscrollview
。
<View
android:layout_width="0dp"
android:layout_height="0dp"
android:focusable="true"
android:focusableInTouchMode="true" />
在我的情况下,由于我在父线性布局中添加了android:layout_gravity="center" ,它正在滚动并聚焦到 recyclerview。 删除后它可以正常工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.