[Android错误及解决方案]RecyclerView中的it

2020-08-29  本文已影响0人  黑森林中的小木屋

参考文章RecyclerView Item 布局宽高无效问题探究
通过对比,发现宽高失效与不失效的区别在与Adapter中创建ViewHolder是加载布局的方式不同:
LayoutInflater.from(parent.getContext()).inflate(R.layout.inflate_test_item,null)
以上这种加载方式Item宽高失效。
LayoutInflater.from(parent.getContext()).inflate(R.layout.inflate_test_item,parent,false)
以上这种方式加载布局item不会出现宽高失效。

如果使用第一个布局加载方式,会使用系统默认布局参数(线性布局)

LinearLayoutManager
 /**
     * {@inheritDoc}
     */
    @Override
    public LayoutParams generateDefaultLayoutParams() {
        return new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);
    }

默认全是wrap_content

如果使用第二种布局加载方式,会使用parent的参数来加载布局参数

上一篇 下一篇

猜你喜欢

热点阅读