How to set the height of an item row in GridLayoutManager

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

My Recycler Item which inflate in onCreateViewHolder




        
8条回答
  •  说谎
    说谎 (楼主)
    2021-01-31 03:46

    On a recent API Level currently 25 the height from the recycler in onCreateViewHolder is always empty. This snippet is to set the hight after the recycler view's onMeasure is invoked and set the correct height to the inflated list view.

    @Override
    public DataBindingViewHolder onCreateViewHolder(final ViewGroup parent, int viewType) {
        // You may inflate your view here.
        parent.post(new Runnable() {
    
            @Override
            public void run() {
                int height = parent.getMeasuredHeight() / rows;
                View view = holder.getBinding().getRoot();
                view.getLayoutParams().height = height;
            }
        });
        return holder;
    }
    

提交回复
热议问题