Matthew Steinhardt
Matthew Steinhardt

Reputation: 332

Scroll past end of list

I currently have a RecyclerView which allows the user to scroll through a long list. However, the view stops scrolling right when it reaches the last item in the data-set.

Question: How can I allow the user to scroll further so that the last item in the RecyvlerView is able to scroll further up the screen?

Upvotes: 4

Views: 1903

Answers (2)

aLL
aLL

Reputation: 1726

On your recyclerViewAdapter's onBindViewHolder, catch the last item with a condition on the position matching your list.size() - 1 and then programatically set the bottom margin, thusly:


@Override
public void onBindViewHolder(@NonNull NewsItemViewHolder holder, int position) {
    if(position == getCurrentList().size() - 1){
        RecyclerView.LayoutParams layoutParams = 
                 (RecyclerView.LayoutParams) itemHolderRootView.getLayoutParams();
        layoutParams.setMargins(layoutParams.leftMargin,layoutParams.topMargin,layoutParams.rightMargin, 300);
    }
}

You may want to multiply 300 bottom margin with your screen density through

DisplayMetrics metrics = getResources().getDisplayMetrics();
int bottomMargin = 300 * metrics.densityDpi;

since it'll vary among screens.

Upvotes: 2

Alex K
Alex K

Reputation: 8338

This is called "overscrolling." This behavior was introduced in Gingerbread.

To turn it on, you use

public void setOverScrollMode(int mode);

From the Android docs:

public void setOverScrollMode (int mode)

Added in API level 9 Set the over-scroll mode for this view. Valid over-scroll modes are OVER_SCROLL_ALWAYS (default), OVER_SCROLL_IF_CONTENT_SCROLLS (allow over-scrolling only if the view content is larger than the container), or OVER_SCROLL_NEVER. Setting the over-scroll mode of a view will have an effect only if the view is capable of scrolling.

Parameters mode The new over-scroll mode for this view.

You can read more about this here.

Upvotes: 1

Related Questions