How to set the height of an item row in GridLayoutManager

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

My Recycler Item which inflate in onCreateViewHolder




        
8条回答
  •  情歌与酒
    2021-01-31 03:57

    When inflating layout for your views in adapter, you can set their height programmatically. In order to evaluate proper height to use you can rely on parent ViewGroup (that is the RecyclerView itself). Here it is a sample:

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = mLayoutInflater.inflate(R.layout.view_item, parent, false);
        // work here if you need to control height of your items
        // keep in mind that parent is RecyclerView in this case
        int height = parent.getMeasuredHeight() / 4;
        itemView.setMinimumHeight(height);
        return new ItemViewHolder(itemView);        
    }
    

    Hope this could help.

提交回复
热议问题