參考文章RecyclerView Item 布局寬高無效問題探究
通過對(duì)比,發(fā)現(xiàn)寬高失效與不失效的區(qū)別在與Adapter中創(chuàng)建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不會(huì)出現(xiàn)寬高失效矫限。
如果使用第一個(gè)布局加載方式站宗,會(huì)使用系統(tǒng)默認(rèn)布局參數(shù)(線性布局)
LinearLayoutManager
/**
* {@inheritDoc}
*/
@Override
public LayoutParams generateDefaultLayoutParams() {
return new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
}
默認(rèn)全是wrap_content
如果使用第二種布局加載方式奴迅,會(huì)使用parent
的參數(shù)來加載布局參數(shù)