How to set the height of an item row in GridLayoutManager

You don't need to set the height of an item. The problem here is that image tries to fill all the space. Just add android:adjustViewBounds="true" to your ImageView and it will not add blank spaces


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.


v.getLayoutParams().width = parent.getMeasuredWidth() / 2;

v.getLayoutParams().height = parent.getMeasuredWidth() / 2;

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);
    }