阻止RecyclerView加载所有数据

时间:2018-01-09 07:04:41

标签: java android android-recyclerview

我的recyclerView位于两个' View'之间。在LinearLayout中,它使用权重属性填充此地点,如下面的代码。

我的问题:当我使用其他值设置其高度但是常数时,它会首先加载所有数据。

我在片段的onCreateView方法中测试此方法。但它没有用!

recyclerView.setNestedScrollingEnabled(false);

因此,当我将此代码放入我的recyclerView时,它可以正常工作并通过滚动来加载数据:

<android.support.v7.widget.RecyclerView
                ...
                android:layout_height="500dp"
                ...
 />

但是当我更改为其他类型的值时,它会立即加载所有数据。像这样:

<android.support.v7.widget.RecyclerView
                ...
                android:layout_height="0dp" <!--OR wrap_content , ...-->
                android:layout_weight="1"
                ...
 />

我想制作一个这样的结构:

enter image description here

如何防止RecyclerView出现此行为?

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="horizontal">
        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:layout_weight="60"
            >

            <include
                android:id="@+id/cardindex_header"
                layout="@layout/item_customer_card_index"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                />

            <android.support.v7.widget.RecyclerView
                android:id="@+id/cardindex_recycler"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                app:layoutManager="LinearLayoutManager"
                tools:listitem="@layout/item_customer_card_index" />

            <include
                android:id="@+id/footer"
                layout="@layout/item_customer_card_index"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"/>
        </LinearLayout>
    <View
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="40">

    </View>
</LinearLayout>

3 个答案:

答案 0 :(得分:1)

最后,我找到了问题的原因。但是,我不知道为什么会这样!问题从根线性布局开始,它不依赖于动态的RecyclerView高度(或者它们可能在一起!)

所以我改变了我的根布局并修复了问题:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="horizontal">

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_marginBottom="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:orientation="vertical"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/card_index_keypad"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <include
            android:id="@+id/card_index_header"
            layout="@layout/item_customer_card_index"
            android:layout_width="match_parent"
            android:layout_height="30dp" />

        <android.support.v7.widget.RecyclerView
            android:id="@+id/card_index_recycler"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            app:layoutManager="LinearLayoutManager"
            tools:listitem="@layout/item_customer_card_index" />

        <include
            android:id="@+id/card_index_footer"
            layout="@layout/item_customer_card_index"
            android:layout_width="match_parent"
            android:layout_height="30dp" />
    </LinearLayout>

    <View
        android:id="@+id/card_index_keypad"
        android:layout_width="221dp"
        android:layout_height="820dp"
        app:layout_constraintEnd_toEndOf="parent"
        tools:layout_editor_absoluteY="3dp">

    </View>
</android.support.constraint.ConstraintLayout>

答案 1 :(得分:0)

您可以尝试在Java代码中动态添加视图。或者为页眉/页脚创建单独的ViewType。

答案 2 :(得分:0)

您可以将RelativeLayout用于布局,其中一个小部件位于另一个小部件的下方,上方,右侧和左侧。以下是我为类似问题所做的事情。

<RelativeLayout
android:id="@+id/main_content"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusableInTouchMode="true">

<TextView
    android:id="@+id/text1"
    android:layout_width="match_parent"
    android:layout_height="50dp"/>

<android.support.v7.widget.RecyclerView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/photo_recycler_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:scrollbars="vertical"
    android:layout_below="@+id/text1"
    android:layout_above="@+id/text2"/>
<TextView
     android:id="@+id/text2"
     android:layout_width="match_parent"
     android:layout_height="50dp"/>
</RelativeLayout>

同样你可以使用android android:layout_toRightOf和android:layout_toLeftOf。

首先为整个屏幕创建一个RelativeLayout 比最正确的LinearLayout并正确对齐。 现在创建最左边的LinearLayout并使用属性android:layout_toLeftOf =“@ + id / [ID of RIGHT MOST LinearLayout]” 现在,在此LinearLayout中,您可以根据需要添加三个小部件。