ScrollView与RecyclerView嵌套问题

今天开发时,遇到了几个ScrollView与RecyclerView嵌套导致的问题。

1. 进入页面时,layout没有置顶

进入页面.jpeg

root cause: 由于在ScrollView内嵌套了RecyclerView,导致RecyclerView获取到了焦点
solution: 使ScrollView包裹的View获取到焦点
如下所示,在LinearLayout里增加 android:focusable="true"和 android:focusableInTouchMode="true"这两个属性
reference: https://blog.csdn.net/suwenlai/article/details/72902684

<?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:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:orientation="vertical">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="35sp"
            android:text="乘机人"/>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:paddingLeft="15dp">

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

        </LinearLayout>
    </LinearLayout>
</ScrollView>

2.RecyclerView显示不全

root cause: 可能是RecyclerView的onMeasure问题
solution1: 重写LinearLayoutManager,可能可以解决
solution2: 使用NestScrollView,而不是ScrollView
reference: https://blog.csdn.net/ThugKd/article/details/78196970

<?xml version="1.0" encoding="utf-8"?>
<!-- 使用NestedScrollView 而不是ScrollView, 解决ScrollView嵌套RecyclerView,导致RecyclerView显示不全问题 -->
<android.support.v4.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    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="wrap_content"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:orientation="vertical">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="35sp"
            android:text="乘机人"/>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:paddingLeft="15dp">

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

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

3.NestedScrollView与RecyclerView滑动冲突

root cause: NestedScrollView与RecyclerView嵌套
solution: recyclerView.setNestedScrollingEnabled(false);
reference: https://www.jianshu.com/p/791c0a4acc1c

    @Override
    protected void initView() {
        mAdapter = new PassengerInfoAdapter();
        RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycler_flight_order_detail);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.addItemDecoration(new ListDivider(this, ListDivider.HORIZONTAL_LIST, R.drawable.passenger_info_divider));

        // 修复NestedScrollView与RecyclerView滑动冲突
        recyclerView.setNestedScrollingEnabled(false);
        recyclerView.setAdapter(mAdapter);
    }
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,952评论 25 709
  • afinalAfinal是一个android的ioc,orm框架 https://github.com/yangf...
    passiontim阅读 15,601评论 2 45
  • afinalAfinal是一个android的ioc,orm框架 https://github.com/yangf...
    wgl0419阅读 6,370评论 1 9
  • 这大概是我这些年来相处的最low的一段关系了,付出的太多,对方已经理所应当地接受了我的好,从来没有觉得感动过。。。...
    扁桃体阅读 379评论 1 1
  • 贝贝从大班开始自己睡,细数起分床的历史,每个自己单睡的时光都像—粒粒小珍珠在记忆的长河中熠熠生辉。 贝贝睡过...
    JC贾阅读 249评论 2 1