RecyclerView 在 NestedScrollView 中窃取焦点 [英] RecyclerView steals focus when inside a NestedScrollView

查看:62
本文介绍了RecyclerView 在 NestedScrollView 中窃取焦点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我将 RecyclerView 放入嵌套滚动视图时,屏幕总是跳转到 RecyclerView 的顶部而不是页面顶部.这是一个简单的例子.

When I put a RecyclerView inside a nested scrollview, the screen always jumps to the top of the RecyclerView instead of the top of the page. Here is a simple example.

布局xml:

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:tools="http://schemas.android.com/tools"
    xmlns:android="http://schemas.android.com/apk/res/android">
<android.support.v4.widget.NestedScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="350dp"
            android:background="@android:color/holo_blue_dark"/>
        <android.support.v7.widget.RecyclerView
            android:id="@+id/recycleView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </LinearLayout>
</android.support.v4.widget.NestedScrollView>
</layout>

使用虚拟适配器的活动:

Activity with dummy adapter:

public class RecycleViewTestActivity extends AppCompatActivity {

public static class ExampleAdapter extends RecyclerView.Adapter<ExampleViewHolder> {

    private Context context;

    public ExampleAdapter(Context context) {
        this.context = context;
    }

    @Override
    public ExampleViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        TextView view = new TextView(context);
        view.setText("Test");
        return new ExampleViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ExampleViewHolder holder, int position) {

    }

    @Override
    public int getItemCount() {
        return 100;
    }
}

public static class ExampleViewHolder extends RecyclerView.ViewHolder {

    public ExampleViewHolder(View itemView) {
        super(itemView);
    }
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_rectest);
    RecyclerView view = (RecyclerView) findViewById(R.id.recycleView);
    view.setNestedScrollingEnabled(false);
    view.setLayoutManager(new LinearLayoutManager(this));
    ExampleAdapter adapter = new ExampleAdapter(this);
    view.setAdapter(adapter);
}

}

在这个例子中,我在 recycleview 上有一个 350dp 高的空视图,因为你需要在 RecycleView 上有一些内容才能明显地显示出来.RecycleView 本身包含 100 个虚拟文本视图.

In this example I have a 350dp tall empty view over the recycleview because you need to have some content over the RecycleView for this to show up obviously. The RecycleView iteself contains 100 dummy textviews.

开始活动后,滚动条位于 RecycleView 的顶部,而不是页面顶部.它一定是 LinearLayoutManager 内部的东西,但还没有真正看过.

After you start the activity, the scroll is at the top of the RecycleView instead of the top of the page. It must be something inside the LinearLayoutManager, but havent really looked yet.

任何想法如何解决这个问题?

Any ideas how to solve this?

推荐答案

使您的顶视图可聚焦.RecyclerView 已将focusableOnTouchMode"设置为 true 以在布局期间处理其子项的焦点变化."相关问题讨论.

Make your top view focusable. "RecyclerView has "focusableOnTouchMode" set to true to handle its childrens' focus changes during layout." Relevant discussion of the issue.

示例:

<android.support.v4.widget.NestedScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:focusableInTouchMode="true"
        android:orientation="vertical">

        <View
            android:id="@+id/someView"
            android:layout_width="wrap_content"
            android:layout_height="350dp"/>

        <android.support.v7.widget.RecyclerView
            android:id="@+id/recyclerView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>

    </LinearLayout>
</android.support.v4.widget.NestedScrollView>

这篇关于RecyclerView 在 NestedScrollView 中窃取焦点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆