有没有什么办法,使在code滚动条的RecyclerView? [英] Is there any way to enable scrollbars for RecyclerView in code?

查看:329
本文介绍了有没有什么办法,使在code滚动条的RecyclerView?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正如我们看到的,RecyclerView比ListView的更有效,所以preFER使用它在我的项目。但最近我已经把当它在我的自定义的ViewGroup麻烦。 RecyclerView是很容易设置滚动条在XML中是这样的:

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

不过,我真的找不到任何方法来设置$ C $下RecyclerView滚动条,我曾尝试是:

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

mRecyclerView.setVerticalScrollBarEnabled(true);

然后我看到了<一个href="https://developer.android.com/intl/zh-cn/reference/android/support/v7/widget/RecyclerView.LayoutManager.html#computeHorizontalScrollExtent(android.support.v7.widget.RecyclerView.State)">this在Android的文件。

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!

推荐答案

目前,它似乎是不可能以编程方式启用滚动条。其原因行为是Android不叫也不 View.initializeScrollbarsInternal(TypedArray一) View.initializeScrollbars(TypedArray一)。如果你实例化RecyclerView与AttributeSet中的两种方法只能调用。
作为一种解决办法,我建议,你创建一个只有你RecyclerView一个新的布局文件: vertical_recycler_view.xml

At the moment it seems to be impossible to enable scroll bars programmatically. The reason for that behaviour is that Android does not call neither View.initializeScrollbarsInternal(TypedArray a) nor 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" />

现在你可以膨胀,并添加带有滚动条无处不在,你想要的RecyclerView: MyCustomViewGroup.java

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

这篇关于有没有什么办法,使在code滚动条的RecyclerView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆