Reputation: 3299
My recyclerView is placed between two 'View' in a LinearLayout and it fills this place with weight property as below code.
My problem: When I set its height with other values but a constant number, it loads all data at first.
I test this method in the onCreateView
method of the fragment. But it didn't work!
recyclerView.setNestedScrollingEnabled(false);
So when I put this code in my recyclerView, it works fine and loads data by scrolling:
<android.support.v7.widget.RecyclerView
...
android:layout_height="500dp"
...
/>
But when I change to other types of value it loads all data at once. like this:
<android.support.v7.widget.RecyclerView
...
android:layout_height="0dp" <!--OR wrap_content , ...-->
android:layout_weight="1"
...
/>
I want to make a structure like this:
How can I prevent RecyclerView from this behavior?
<?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>
Upvotes: 2
Views: 1358
Reputation: 3299
Finally, I found a reason for the problem. However, I don't know why it happens! The problem begins with root Linear layout and it does not depend on dynamic RecyclerView height(Or maybe they related together!)
So I change my root layout and the problem fixed:
<?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>
Upvotes: 1
Reputation: 407
You can use RelativeLayout for layouts with one widget lying below, above, on right and on left of another widget. Here is what I would do for a similar problem.
<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>
Similarly you can use android android:layout_toRightOf and android:layout_toLeftOf.
First create a RelativeLayout for whole screen Than make the Right most LinearLayout and align it right. Now create the Left most LinearLayout and use attribute android:layout_toLeftOf="@+id/[ID OF RIGHT MOST LinearLayout]" Now add three widgets as you want one below the other inside this LinearLayout.
Upvotes: 0
Reputation: 2764
You can try to add views dynamically in your Java code. Or create separate ViewTypes for your header/footer.
Upvotes: 0