-2

I have a horizontal RecyclerView and an ImageView that is located under that RecyclerView

Here's the relevant xml code:

<android.support.v7.widget.RecyclerView
    android:id="@+id/my_rv"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_above="@+id/upArrow">

</android.support.v7.widget.RecyclerView>

<!-- TODO: This view should be part of the recyclerview scroll, we should find a solution for this -->
<ImageView
    android:id="@+id/upArrow"
    android:layout_marginTop="6dp"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/ic_my_up_triangle_vector"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true" />

I would like to achieve the following scrolling effect:

When there is an attempt to scroll the RecyclerView and/or the ImageView below it, the RecyclerView will scroll accordingly, and the imageview will stay at its original place. Basically what I'm trying to achieve is expanding the RecyclerView scrolling boundaries to include all the Imageview's height

I was thinking about setting a ScrollView that wraps both of these views but I'm not really sure what to do then.

idish
  • 3,190
  • 12
  • 53
  • 85

3 Answers3

1

In your xml file

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_width="match_parent">

<!-- TODO: This view should be part of the recyclerview scroll, we should find a solution for this -->
<ImageView
    android:id="@+id/upArrow"
    android:layout_width="wrap_content"
    android:layout_height="40dp"
    android:src="@drawable/ic_my_up_triangle_vector"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true" />

<android.support.v7.widget.RecyclerView
    android:id="@+id/my_rv"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentTop="true"
    android:paddingBottom="40dp">

</android.support.v7.widget.RecyclerView>

</RelativeLayout>

What this code does is it places your RecyclerView on top of your ImageView and the padding at bottom is used to show the image at bottom. Change Height of the ImageView and Padding of the RecyclerView accordingly.

0

You already have got some down-votes in your question I see. I suppose those are for your question which is not actually clear and the solution you're trying to achieve is not the proper way of doing so too.

From your code of the layout, I have come to understand that you need an up arrow button which will scroll to the top of the RecyclerView when clicked on it and you want the button to stay in the bottom of the page you're in. You want your RecyclerView to scroll as it is and the arrow button to stay where it is. If this is your case, then you need to rethink about the layout and the Activity flow design. You need to make the RecyclerView and the arrow button completely disjoint.

So, based on my assumption on your question, you need an Activity which contains a floating action button and holds a Fragment. The Fragment will have the RecyclerView making the floating action button (for the up arrow) and the RecyclerView completely disjoint.

So you need to have an Activity and the layout of the Activity should look like this.

<RelativeLayout 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">

    <RelativeLayout
        android:id="@+id/fragment_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab_up"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_margin="@dimen/activity_horizontal_margin"
        android:src="@drawable/ic_up_arrow"
        app:backgroundTint="@color/colorPrimary"
        app:borderWidth="0dp"
        app:elevation="4dp"
        app:fabSize="normal" />

</RelativeLayout>

And the onCreate function of the Activity should look like this.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_layout);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    mFabUpArrow = (FloatingActionButton) findViewById(R.id.fab_up);
    mFabUpArrow.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            jumpToTopOfTheRecyclerView();
        }
    });

    // Now launch the Fragment from here. 
    getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new RecyclerViewFragment()).commit();
}

Now the RecyclerViewFragment should have the following layout holding the RecyclerView

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/your_list"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</RelativeLayout>

And I think you already know how to populate your RecyclerView and do that thing in your Fragment.

You might be asking how to implement the jumpToTopOfTheRecyclerView() function in the Activity. Its simple and you've several option. One approach might be having a BroadcastReceiver in your Fragment and sending the broadcast from your Activity which is not so good approach. You might find something very easy to communicate between your Activity and your Fragment. Best of luck.

Reaz Murshed
  • 23,691
  • 13
  • 78
  • 98
0

You can just place your header and your RecyclerView in a NestedScrollView:

    <android.support.v4.widget.NestedScrollView
    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.support.v7.widget.RecyclerView
            android:id="@+id/my_rv"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_above="@+id/upArrow">

        </android.support.v7.widget.RecyclerView>
        <ImageView
            android:id="@+id/upArrow"
            android:layout_marginTop="6dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_my_up_triangle_vector"
            android:layout_alignParentBottom="true"
            android:layout_centerHorizontal="true" />

    </LinearLayout>

</android.support.v4.widget.NestedScrollView>

and in Java code make mRecyclerView.setNestedScrollingEnabled(false); to scroll correctly credits check this anser

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
Ramakanth Putta
  • 676
  • 4
  • 15