RecyclerView 在 NestedScrollView 内部窃取焦点

新手上路,请多包涵

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

布局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>

使用虚拟适配器的活动:

 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 个虚拟文本视图。

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

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

原文由 breakline 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 402
2 个回答

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

例子:

 <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>

原文由 Jim Pekarek 发布,翻译遵循 CC BY-SA 3.0 许可协议

对我来说,接受的答案没有用。我通过为父级添加此属性来解决此问题:

android:descendantFocusability="blocksDescendants"

原文由 dr-to-str 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题