0

When the activity is opened, it shows the top of the RecyclerView layout instead of the top of the activity layout.

Activity layout .xml file:

    <ScrollView
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:paddingBottom="20dp"
                android:paddingTop="20dp">

            ...
            <RelativeLayout/>
            <View />
            <LinearLayout/>
            ...

            <android.support.v7.widget.RecyclerView
                android:id="@+id/venue_place_info_gallery_recycler_view"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp"/>

            </LinearLayout>
    </ScrollView>

Activity onCreate:

    RecyclerView galleryRecyclerView = (RecyclerView) findViewById(R.id.venue_place_info_gallery_recycler_view);
    galleryRecyclerView.setLayoutManager(new StaggeredGridLayoutManager(gridLayoutColumns, StaggeredGridLayoutManager.VERTICAL));
    VenueGalleryAdapter venueGalleryAdapter = new VenueGalleryAdapter(VenuePlaceInfoActivity.this, images);
    galleryRecyclerView.setAdapter(venueGalleryAdapter);

The adapter is very simple. It receives the images as an argument in the constructor and the data cannot be changed later on. I've tried to apply all kinds of settings to the RecyclerView layout, but it works the same with or without them. For example:

    galleryRecyclerView.setNestedScrollingEnabled(false);
    galleryRecyclerView.setHasFixedSize(true);
    galleryRecyclerView.setLayoutFrozen(true);
    galleryRecyclerView.setPreserveFocusAfterLayout(false);

UPDATE:

I have found more info on the subject in the answer of another question. It's all because of the ScrollView and RecyclerView not being capable to live together even if you set setNestedScrollingEnabled to true. But it still doesn't give me the resolution of my problem. I need to have some stuff above the gallery RecyclerView and I want to scroll down through all the images (not putting them in a container).

Community
  • 1
  • 1
Galya
  • 5,625
  • 6
  • 24
  • 43
  • Can you attach any screenshot ? – Yasin Kaçmaz Jul 25 '16 at 13:07
  • Remove galleryRecyclerView.setPreserveFocusAfterLayout(false); line – Vinothkumar Nataraj Jul 25 '16 at 13:08
  • I've said that these additional parameters are removed at the moment. I've just tried them before, but they didn't change a thing. – Galya Jul 25 '16 at 13:09
  • @YasinKaçmaz, it should be probably a video, since there is nothing interesting for showing on a screenshot. It's just the gallery starting on top of the screen. – Galya Jul 25 '16 at 13:10
  • @Galya i can't imagine it then i want to some visual. Sorry im in bad day – Yasin Kaçmaz Jul 25 '16 at 13:13
  • Have you tried wrapping the recyclerview and the other things in the scrollview with a LinearLayout? Also, setting the recyclerview's height to wrap_content will make it expand until it doesn't need to scroll. In other words, you need to set your recyclerview to a set height, limited by its parent or by a some other constant. – John Gallagher Jul 25 '16 at 14:04

2 Answers2

0

If you want your layout to look good, I would use a CoordinatorLayout with an AppBarLayout as the first element inside it, and the RecyclerView as the second. Inside the AppBarLayout, you should put whatever it is that you would like to have above the RecyclerView. Something similar to this:

<LinearLayout
    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:orientation="vertical">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

    <android.support.design.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:background="@color/white">

        <android.support.design.widget.AppBarLayout
            android:id="@+id/appbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:elevation="0dp">

            <android.support.design.widget.CollapsingToolbarLayout
                android:id="@+id/collapsing_toolbar"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:layout_scrollFlags="scroll|enterAlways"
                app:titleEnabled="false">

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:clipChildren="false"
                    android:clipToPadding="false"
                    android:orientation="vertical"
                    android:paddingTop="20dp"
                    app:layout_collapseMode="parallax">

                    <!-- stuff you want above your recyclerview -->

                </LinearLayout>
            </android.support.design.widget.CollapsingToolbarLayout>
        </android.support.design.widget.AppBarLayout>

        <android.support.v7.widget.RecyclerView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            app:layout_behavior="@string/appbar_scrolling_view_behavior"/>

    </android.support.design.widget.CoordinatorLayout>
</LinearLayout>

Someone does something similar here, just instead of using a toolbar in the appbarlayout, you may use whatever view layouts you'd like. AppBarLayout is an extended Vertical LinearLayout: http://www.basagee.tk/handling-scrolls-with-coordinatorlayout/

Galya
  • 5,625
  • 6
  • 24
  • 43
John Gallagher
  • 517
  • 4
  • 15
  • This is probably not the lean and simple solution I was looking for, but it does the job, so it's the accepted answer. I've edited it to add the actual code working for me. – Galya Jul 25 '16 at 18:17
0

just add the following tag on your activity/fragment root layout and you are good to go.

android:descendantFocusability="blocksDescendants"