首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在RecyclerView中设置scrollbars属性会导致应用程序崩溃

在RecyclerView中设置scrollbars属性会导致应用程序崩溃
EN

Stack Overflow用户
提问于 2015-05-04 13:01:47
回答 1查看 1.1K关注 0票数 4

我最近开始用RecyclerView取代旧的ListView。但每当我在android.support.v7.widget.RecyclerView元素中使用android:scrollbars="vertical"时,我的应用程序就会崩溃,并显示以下错误:Attempt to invoke virtual method 'boolean android.support.v7.widget.RecyclerView$LayoutManager.canScrollVertically()' on a null object reference

这就是我使用RecyclerView的方式:

代码语言:javascript
复制
View rootView;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    rootView = inflater.inflate(R.layout.my_fragment, container, false);
    return rootView;
}
protected void onPostExecute(Boolean result) {
    RecyclerView rv = (RecyclerView) rootView.findViewById(R.id.recyclerview);
    LinearLayoutManager llm = new LinearLayoutManager(context);
    rv.setLayoutManager(llm);
    RecycleViewAdapter adapter = new RecycleViewAdapter(myRecyclerViewAdapter);
    rv.setHasFixedSize(true);
    rv.setAdapter(adapter);
}

这是my_fragment.xml文件:

代码语言:javascript
复制
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

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

    </android.support.v7.widget.RecyclerView>

</LinearLayout>

如果我不使用android:scrollbars="vertical",一切都可以正常工作。谢谢你的帮助。

EN

回答 1

Stack Overflow用户

发布于 2015-05-04 13:08:04

在使用RecyclerView时,您实际上不需要指定scrollbars显示;这是LayoutManager任务,您可以指定它是垂直还是水平方向,当然,滚动条显示将自动更改您选择的任何方向。

代码语言:javascript
复制
//for vertical scrollbar and orientation
LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false);  

//for horizontal scrollbar and orientation 
LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false); 
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30022913

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档