How to set the height of an item row in GridLayoutManager

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

My Recycler Item which inflate in onCreateViewHolder




        
相关标签:
8条回答
  • 2021-01-31 04:07
    v.getLayoutParams().width = parent.getMeasuredWidth() / 2;
    
    v.getLayoutParams().height = parent.getMeasuredWidth() / 2;
    
    0 讨论(0)
  • 2021-01-31 04:09

    This is my code for adjusting height of recycle view element based on actual aspect-ration required.

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext()) .inflate(R.layout.adapter_offers, parent, false);
    
        int width =  parent.getMeasuredWidth();
        float height = (float) width / Config.ASPECT_RATIO;//(Width/Height)
        RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) itemView.getLayoutParams();
        params.height = Math.round(height);
        itemView.setLayoutParams(params);
        return new MyViewHolder(itemView);
    }
    
    0 讨论(0)
提交回复
热议问题