How to set the height of an item row in GridLayoutManager

后端 未结 8 619
有刺的猬
有刺的猬 2021-01-31 03:18

My Recycler Item which inflate in onCreateViewHolder




        
8条回答
  •  时光说笑
    2021-01-31 03:52

    As of support library 25.1.0, ABOVE ANSWER DOESN'T WORK. I suggest below modifications:

        public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item, parent, false);
            GridLayoutManager.LayoutParams lp = (GridLayoutManager.LayoutParams) v.getLayoutParams();
            lp.height = parent.getMeasuredHeight() / 4;
            v.setLayoutParams(lp);
            return new ViewHolder(v);
        }
    

提交回复
热议问题