Pardeep Kumar
Pardeep Kumar

Reputation: 950

How to make recycler view scroll more than the height of the items

I have a recycler view starting from top of screen going to the bottom of screen. At the bottom of screen there is one more view which hides the last item of recycler view.

I want recycler view to scroll a bit more, up to the top of the bottom element. So the last item could be visible.

I know, like i can add some dummy empty item at the end of list in recycler view. But i do not want to disturb the number of items in the recycler view. Should i resize the height of the last item of the recycler view so that its content comes above the bottom element?

Anyone has some better idea. whats the best way to do it?

Upvotes: 5

Views: 3274

Answers (5)

Javier Antón
Javier Antón

Reputation: 665

Make the root view of your layout a ScrollView: Something like:

<ScrollView>
   <LinearLayout>
       <RecyclerView/>
       <TheItemsYouWantToSee/>
   </LinearLayout>
</ScrollView>

You can have different containers instead of the LinearLayout: CoordinatorLayout, Constraint... that wraps the items inside the scrollview

Upvotes: 0

Mohsen
Mohsen

Reputation: 2199

Dummy View works fine. And more importantly it's less messy. Just add as mush dummies you need to be above that View and make them invisible at the start of onBindViewHolder

override fun getItemCount(): Int {
        //two more items (invisible) for end of the list
        return  list.size + 2
    }

and in before populating the items onBindViewHolder

if(position >= list.size) {
            holder.item.visibility = View.INVISIBLE
            return
        } else
            holder.item.visibility = View.VISIBLE

Upvotes: 0

Rajat Mittal
Rajat Mittal

Reputation: 423

Do not set height of recycler view to match_parent. Use Relative Layout and change properties of recycler vie relative to the view (which you want to add at the bottom of screen). for Example. Layout_above

OR You can simple use ScrollView to scroll the whole view.

<ScrollView
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:fillViewport="true"
    xmlns:android="http://schemas.android.com/apk/res/android"
>
<RelativeLayout>
</RelativeLayout>
</ScrollView>

Upvotes: 0

Pardeep Kumar
Pardeep Kumar

Reputation: 950

I got the answer. need to set this in recyclerview

android:clipToPadding="false"
android:paddingBottom="the bottom padding needed"

Upvotes: 12

rupesh
rupesh

Reputation: 129

Add your recyclerview and that bottom view you want to keep inside a RelativeLayout like this -

<RelativeLayout>
   <RecyclerView/>
   <YourAnotherView/>
</RelativeLayout>

And, add the layout_above property to the RecyclerView. This property will keep the recyclerview above your bottom view, always.

Upvotes: 0

Related Questions