Reputation: 421
There are several related posts here, but none are quite the same situation/none of the solutions have worked for me.
I have a RecyclerView that loads in some data from Firebase. I'm using a custom adapter. The issue is that when the view loads in, it loads in at the middle of the RecyclerView. I then would have to scroll up to the top manually.
My layout is very simple:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@color/grey"
android:layout_marginBottom="50dp"
>
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/recycler_view_feed"
android:layout_marginTop="10dp"
>
</android.support.v7.widget.RecyclerView>
</LinearLayout>
After loading in my custom objects from Firebase, I set up the RecyclerView like this (this is in a method being called from onCreateView (it's a fragment)):
CompleteWorkoutRecyclerAdapter adapter = new CompleteWorkoutRecyclerAdapter(list, getContext(),
getActivity());
mRecyclerView.setAdapter(adapter);
mRecyclerView.setHasFixedSize(false);
linearLayoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, true);
mRecyclerView.setLayoutManager(linearLayoutManager);
I've tried these things to fix the issue, with no luck:
linearLayoutManager.scrollToPosition(0);
and
mRecyclerView.scrollToPosition(0);
and
android:focusableInTouchMode="true"
Thanks for reading. If you guys need any more code I can post whatever you need
Upvotes: 4
Views: 4319
Reputation: 145
For me, I found that the layout for a row in my recycler view has layout_height set as match_parent
, causing it to fill up the whole parent view. I fixed it by using wrap_content
instead (or maybe set a fixed height would work too)
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content">
...
</androidx.constraintlayout.widget.ConstraintLayout>
Upvotes: 0
Reputation: 4510
First change your xml to this
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/recycler_view_feed"
android:layout_marginTop="10dp">
then the code to
mRecyclerView.setHasFixedSize(false);
linearLayoutManager = new LinearLayoutManager(getActivity());
mRecyclerView.setLayoutManager(linearLayoutManager);
CompleteWorkoutRecyclerAdapter adapter = new CompleteWorkoutRecyclerAdapter(list, getContext(),
getActivity());
mRecyclerView.setAdapter(adapter);
Change height of your row layout in your adapter to wrap_content
Upvotes: 1
Reputation: 2301
Use relative layout
instead of linear layout
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/grey"
android:layout_marginBottom="50dp"
>
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/recycler_view_feed"
android:layout_marginTop="10dp"
android:layout_alignParentTop="true">
</android.support.v7.widget.RecyclerView>
</RelativeLayout>
Upvotes: 0
Reputation: 549
Use this code:
CompleteWorkoutRecyclerAdapter adapter = new CompleteWorkoutRecyclerAdapter(list, getContext(),
getActivity());
linearLayoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false);
mRecyclerView.setLayoutManager(linearLayoutManager);
mRecyclerView.setAdapter(adapter);
Hope this will help you out..
Upvotes: 1