Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any way to enable scrollbars for RecyclerView in code?

As we saw, RecyclerView is more effective than ListView, so I prefer to use it in my project. But recently I have a trouble when put it in my custom ViewGroup. RecyclerView is easy to set scrollbars in xml like this:

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

But I really can't find any method to set the scrollbars in code for RecyclerView, what I have tried is:

mRecyclerView.setVerticalScrollBarEnabled(true);

and then I saw this in Android's document.

So I tried to make my own LayoutManager and override the functions which I thought I need. But finally I failed. So can anyone tell me how should I make my own LayoutManager or just show me an other solution. Thank you!

like image 294
Kevin Liu Avatar asked Nov 21 '14 07:11

Kevin Liu


People also ask

How do I keep ScrollBars visible?

In the Settings window, click the “Ease of Access” category. On the left side of the Ease of Access screen, click the “Display” option. On the right, turn off the “Automatically Hide Scroll Bars In Windows” toggle to make sure your scrollbars don't disappear anymore.

How do I add a ScrollBar to an element?

You need to add style="overflow-y:scroll;" to the div tag. (This will force a scrollbar on the vertical).


4 Answers

At the moment it seems to be impossible to enable scroll bars programmatically. The reason for that behaviour is that Android does not call either View.initializeScrollbarsInternal(TypedArray a) or View.initializeScrollbars(TypedArray a). Both methods are only called if you instantiate your RecyclerView with an AttributeSet.
As a workaround I would suggest, that you create a new layout file with your RecyclerView only: vertical_recycler_view.xml

<android.support.v7.widget.RecyclerView
xmlns:android="http://schemas.android.com/apk/res/android"
android:scrollbars="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent" />

Now you can inflate and add the RecyclerView with scrollbars everywhere you want: MyCustomViewGroup.java

public class MyCustomViewGroup extends FrameLayout
{
    public MyCustomViewGroup(Context context)
    {
        super(context);

        RecyclerView verticalRecyclerView = (RecyclerView) LayoutInflater.from(context).inflate(R.layout.vertical_recycler_view, null);
        verticalRecyclerView.setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false));
        addView(verticalRecyclerView);
    }
}
like image 121
user1185087 Avatar answered Oct 13 '22 17:10

user1185087


Set the vertical scrollbar in the xml layout

<android.support.v7.widget.RecyclerView
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:scrollbars="vertical" />
like image 34
Lilo Avatar answered Oct 13 '22 16:10

Lilo


Just in xml properties

<android.support.v7.widget.RecyclerView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/recyclerView"
    android:scrollbars="vertical" <!-- type of scrollbar -->
    android:scrollbarThumbVertical="@android:color/darker_gray"  <!--color of scroll bar-->
    android:scrollbarSize="5dp"> <!--width of scroll bar-->

</android.support.v7.widget.RecyclerView>
like image 45
Dinesh Sunny Avatar answered Oct 13 '22 17:10

Dinesh Sunny


I would prefer to use ContextThemeWrapper for that, because it can be used dynamically from code.

First define in Style.xml:

<style name="ScrollbarRecyclerView" parent="android:Widget">
    <item name="android:scrollbars">vertical</item>
</style>

And then whenever you initialize your RecyclerView use ContextThemeWrapper:

RecyclerView recyclerView = new RecyclerView(new ContextThemeWrapper(context, R.style.ScrollbarRecyclerView));
like image 19
Ayaz Alifov Avatar answered Oct 13 '22 18:10

Ayaz Alifov