美文网首页Android
RecycleView嵌套gridView 里面的gridVie

RecycleView嵌套gridView 里面的gridVie

作者: deast | 来源:发表于2020-07-02 13:26 被阅读0次

    //重写gridview的onMeasure方法
    public class MyGridView extends GridView{
    public MyGridView(Context context) {
    super(context);
    }

    public MyGridView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
    
    public MyGridView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }
    
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int expandspec=MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE>>2, MeasureSpec.AT_MOST);
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }
    

    }

    //重新计算高度
    private void setGrideViewHeight(GridView gridView){
    ListAdapter adapter = gridView.getAdapter();
    if(adapter==null)
    return;

    int totalHeight=0;
    int lineNum=4;
    View item = adapter.getView(0, null, gridView);
    
    item.measure(0,0);
    totalHeight= item.getMeasuredHeight()*lineNum;
    
    ViewGroup.LayoutParams layoutParams = gridView.getLayoutParams();
    layoutParams.height=totalHeight;
    ((ViewGroup.MarginLayoutParams)layoutParams).setMargins(10,10,10,10);
    gridView.setLayoutParams(layoutParams);
    

    }

    相关文章

      网友评论

        本文标题:RecycleView嵌套gridView 里面的gridVie

        本文链接:https://www.haomeiwen.com/subject/qntrqktx.html