title: RecyclerView遇到的问题
date: 2020-03-18 15:39:47
blog: www.gitkyne.com
前言
recyclerview的使用不讲,此处只记录下问题,及解答
目录

viewHolder不予许在创建的时候添加到parent里
-
问题
ViewHolder views must not be attached when created. Ensure that you are not passing ‘true’ to the attachToRoot parameter of LayoutInflate
-
解析
遇到这个问题,说明在onCreateViewHolder方法中inflate时有问题。
首先了解下inflate方法中三个参数的意思public View inflate(@LayoutRes int resource, @Nullable ViewGroup root, boolean attachToRoot)
第一个参数是传入的布局参数
第二个参数是这个布局的父布局
第三个就是是否要将这个布局加载到父布局里提示意思表示 viewHolder不予许在创建的时候添加到parent里
在onCreateViewHolder中View view = inflater.inflate(R.layout.item_view, parent, true);或者 View view = inflater.inflate(R.layout.item_view, parent);
就会报错, 只要改成:
View view = inflater.inflate(R.layout.item_view, null); 或者 View view = inflater.inflate(R.layout.item_view, parent, false);
即可
RecyclerView的item不充满的整个宽度的
- 问题
明明设置了match_parent布局,实际去不能充满。 - 解析
一种情况,在adapter中加载inflater时parent传入为null
改成view = View.inflate(mContext, R.layout.item,null); ` return new ClassDayViewHolder(view);
另一种情况,在RecyclerView外随意加一层布局即可充满view = LayoutInflater.from(mContext).inflate(R.layout.item_today_course,parent,false);
setReverseLayout 和 setStackFromEnd 问题
这两个方法属于LayoutManager中的知识范围。
-
setReverseLayout(true)
- 作用:将数据从布局的底部从下往上排列。上滑加载后面的数据。页面定位在布局底部。
- 实验:(屏幕内最多展示7个item)
第一次加载

上滑加载

-
setStackFromEnd(true)
- 作用:从堆底部开始向上展示,数据逆序添加。页面定位布局底部。
- 实验:
第一次加载

上滑加载

-
图释 (条件:充满布局,分两种情况,数据是否超出屏幕展示数量)
- 使用
layoutManager =new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, true);
- 使用


- 使用
```
layoutManager =new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false);
layoutManager.setStackFromEnd(true);
```


- 使用
```
layoutManager =new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, true);
layoutManager.setStackFromEnd(true);
```


网友评论