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

跟風遠走 提交于 2019-11-29 20:47:31
user1185087

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);
    }
}

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

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>

I would prefer to use ContextThemeWrapper for that.

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));

You can do it without inflating an XML layout, but you'll need to declare a custom theme attribute and a style:

<resources>
    <attr name="verticalRecyclerViewStyle" format="reference"/>

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

Then set the value of the attribute to the style in your theme:

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">

    <item name="verticalRecyclerViewStyle">@style/VerticalRecyclerView</item>
</style>

Now you can create the RecyclerView programmatically with a vertical scrollbar:

RecyclerView recyclerView = new RecyclerView(context, null, R.attr.verticalRecyclerViewStyle);
Achintha Isuru

Use a code like:

<androidx.recyclerview.widget.RecyclerView
 android:id="@+id/categoryRecyclerLayout"
 android:layout_width="414dp"
 android:layout_height="652dp"
 android:scrollbars="vertical" .... />
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!