处理ScrollView嵌套GridView显示不全问题

public class FixedViewUtil {

    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
    public static void setListViewHeightBasedOnChildren(GridView listView, int col) {
        // 获取listview的adapter
        ListAdapter listAdapter = listView.getAdapter();
        if (listAdapter == null) {
            return;
        }
        // 固定列宽,有多少列
        int totalHeight = 0;
        // i每次加4,相当于listAdapter.getCount()小于等于4时 循环一次,计算一次item的高度,
        // listAdapter.getCount()小于等于8时计算两次高度相加
        for (int i = 0; i < listAdapter.getCount(); i += col) {
            // 获取listview的每一个item
            View listItem = listAdapter.getView(i, null, listView);
            listItem.measure(0, 0);
            // 获取item的高度和
            totalHeight += listItem.getMeasuredHeight();
            totalHeight += listView.getVerticalSpacing();
            if (i==listAdapter.getCount()-1) {
                totalHeight += listView.getVerticalSpacing();
            }
        }
        // 获取listview的布局参数
        LayoutParams params = listView.getLayoutParams();
        // 设置高度
        params.height = totalHeight;
        // 设置margin
        ((MarginLayoutParams) params).setMargins(10, 10, 10, 10);
        // 设置参数
        listView.setLayoutParams(params);
    }

    public static void setListViewHeightBasedOnChildren(ListView lv){  
        ListAdapter listAdapter = lv.getAdapter();
        int listViewHeight = 0;  
        int adaptCount = listAdapter.getCount();  
        for(int i=0;i<adaptCount;i++){  
            View temp = listAdapter.getView(i,null,lv);  
            temp.measure(0,0);  
            listViewHeight += temp.getMeasuredHeight();  
        }  
        LayoutParams layoutParams = lv.getLayoutParams();  
        layoutParams.width = LayoutParams.MATCH_PARENT;  
        layoutParams.height = listViewHeight;  
        lv.setLayoutParams(layoutParams);  
    }
}

在代码中使用即可

FixedViewUtil.setListViewHeightBasedOnChildren(mRecommendGv, 2);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值