您的位置:首页 > 其它

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

2016-12-13 13:10 134 查看
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);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  gridview
相关文章推荐