美文网首页问题日常
ScrollView与RecyclerView嵌套问题

ScrollView与RecyclerView嵌套问题

作者: JeremySun0823 | 来源:发表于2018-06-14 13:54 被阅读0次

今天开发时,遇到了几个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);
    }

相关文章

网友评论

    本文标题:ScrollView与RecyclerView嵌套问题

    本文链接:https://www.haomeiwen.com/subject/seaeeftx.html