【问题标题】:Is there any way to enable scrollbars for RecyclerView in code?有没有办法在代码中为 RecyclerView 启用滚动条?
【发布时间】:2015-01-19 07:19:01
【问题描述】:

正如我们所见,RecyclerView 比 ListView 更有效,所以我更喜欢在我的项目中使用它。但是最近我把它放在我的自定义 ViewGroup 中时遇到了麻烦。 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" />

但我真的找不到在 RecyclerView 的代码中设置滚动条的任何方法,我尝试过的是:

mRecyclerView.setVerticalScrollBarEnabled(true);

然后我在 Android 的文档中看到了this

所以我尝试制作自己的 LayoutManager 并覆盖我认为我需要的功能。但最后我失败了。所以谁能告诉我应该如何制作自己的 LayoutManager 或者只是向我展示其他解决方案。谢谢!

【问题讨论】:

    标签: android scrollbars android-recyclerview


    【解决方案1】:

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

    现在你可以在任何你想要的地方膨胀和添加带有滚动条的 RecyclerView: 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);
        }
    }
    

    【讨论】:

    【解决方案2】:

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

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

    【讨论】:

    • 这样够了吗,还是我们还要设置android:scrollbarStyle="outsideOverlay"
    • @IgorGanapolsky 不需要设置,除非你想指定滚动条是覆盖还是嵌入
    • 这并不能真正回答问题,因为 OP 根本没有使用 XML 布局,因此希望以编程方式拥有它。
    • 还有android:fadeScrollbars="true"等可选属性
    【解决方案3】:

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

    【讨论】:

    • 如何通过滚动设置颜色?比如 recyclerview.scrollview.color = R.color.some_one
    【解决方案4】:

    您可以在不扩展 XML 布局的情况下做到这一点,但您需要声明自定义主题属性和样式:

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

    然后将属性的值设置为主题中的样式:

    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    
        <item name="verticalRecyclerViewStyle">@style/VerticalRecyclerView</item>
    </style>
    

    现在您可以使用垂直滚动条以编程方式创建 RecyclerView:

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

    【讨论】:

      【解决方案5】:

      我更愿意为此使用ContextThemeWrapper

      首先在 Style.xml 中定义:

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

      然后每当你初始化你的 RecyclerView 时使用ContextThemeWrapper:

      RecyclerView recyclerView = new RecyclerView(new ContextThemeWrapper(context, R.style.ScrollbarRecyclerView));
      

      【讨论】:

      • 谢谢,阿亚兹阿比姆
      【解决方案6】:

      使用如下代码:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-05-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-09-17
        • 2020-04-28
        相关资源
        最近更新 更多