Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/203.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 有没有办法在代码中为RecyclerView启用滚动条?_Android_Scrollbars_Android Recyclerview - Fatal编程技术网

Android 有没有办法在代码中为RecyclerView启用滚动条?

Android 有没有办法在代码中为RecyclerView启用滚动条?,android,scrollbars,android-recyclerview,Android,Scrollbars,Android Recyclerview,正如我们所看到的,RecyclerView比ListView更有效,所以我更喜欢在我的项目中使用它。但最近我在将其放入自定义视图组时遇到了一个问题。RecyclerView很容易在xml中设置滚动条,如下所示: <android.support.v7.widget.RecyclerView android:id="@+id/recycler_view" android:scrollbars="vertical" android:layout_width="match

正如我们所看到的,RecyclerView比ListView更有效,所以我更喜欢在我的项目中使用它。但最近我在将其放入自定义视图组时遇到了一个问题。RecyclerView很容易在xml中设置滚动条,如下所示:

<android.support.v7.widget.RecyclerView
    android:id="@+id/recycler_view"
    android:scrollbars="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />
然后我在Android的文档中看到


因此,我尝试创建自己的LayoutManager并覆盖我认为需要的功能。但最后我失败了。那么,有谁能告诉我,我应该如何制作自己的LayoutManager,还是给我一个其他的解决方案。谢谢大家!

目前,似乎不可能以编程方式启用滚动条。这种行为的原因是Android既不调用
View.initializeScrollbars内部(TypedArray a)
也不调用
View.initializeScrollbars(TypedArray a)
。只有使用AttributeSet实例化RecyclerView时,才会调用这两个方法。
作为一种解决方法,我建议您仅使用RecyclerView创建一个新布局文件:
vertical\u recycler\u 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" />

在xml布局中设置垂直滚动条

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

仅在xml属性中

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

android:scrollbarSize=“5dp”>

您可以在不膨胀XML布局的情况下执行此操作,但需要声明自定义主题属性和样式:

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

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

我更愿意使用
ContextThemeWrapper
来实现这一点

首先在Style.xml中定义:

<style name="ScrollbarRecyclerView" parent="android:Widget">
    <item name="android:scrollbars">vertical</item>
</style>
使用如下代码:

<androidx.recyclerview.widget.RecyclerView
 android:id="@+id/categoryRecyclerLayout"
 android:layout_width="414dp"
 android:layout_height="652dp"
 android:scrollbars="vertical" .... />


我现在使用这种方式,但非常感谢您告诉我原因!现在你可以轻松添加滚动条了!检查这个答案:这足够了吗,或者我们还必须设置android:scrollbarStyle=“outsideOverlay”?@IgorGanapolsky不需要设置它,除非您想指定滚动条是覆盖还是插入。这并不能真正回答问题,因为OP根本没有使用XML布局,因此,希望以编程方式实现此功能。并且您可以选择一些属性,例如
android:fadeScrollbars=“true”
如何通过滚动设置颜色?比如recyclerview.scrollview.color=R.color.some_oneThanks,Ayaz abim
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">

    <item name="verticalRecyclerViewStyle">@style/VerticalRecyclerView</item>
</style>
RecyclerView recyclerView = new RecyclerView(context, null, R.attr.verticalRecyclerViewStyle);
<style name="ScrollbarRecyclerView" parent="android:Widget">
    <item name="android:scrollbars">vertical</item>
</style>
RecyclerView recyclerView = new RecyclerView(new ContextThemeWrapper(context, R.style.ScrollbarRecyclerView));
<androidx.recyclerview.widget.RecyclerView
 android:id="@+id/categoryRecyclerLayout"
 android:layout_width="414dp"
 android:layout_height="652dp"
 android:scrollbars="vertical" .... />