本文主要是介绍关于listview,gridview里面数据显示不全的问题,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
listview,girdview这些控件在我们android开发经常用的到,但是,数据显示不全的问题也是我们经常碰到的,下面只要在代码中加入几行代码即可
以下方法仅供参考,转自网络
listview控件显示数据
private void listFitHeight(ListView listView){RepairDetailInfosAdpter listAdapter=(RepairDetailInfosAdpter)listView.getAdapter();if(listAdapter==null){return;}int totalHeight=0;for(int i=0;i<listAdapter.getCount();i++){View listItem=listAdapter.getView(i,null,listView);listItem.measure(0,0);totalHeight+=listItem.getMeasuredHeight();}ViewGroup.LayoutParams params=listView.getLayoutParams();params.height=totalHeight+(listView.getDividerHeight()*(listAdapter.getCount()-1));((ViewGroup.MarginLayoutParams)params).setMargins(0,0,0,0);listView.setLayoutParams(params);}
gridview控件显示数据
private static void setListViewHeightBasedOnChildren(GridView listView, int col) {//获取listview的adapterListAdapter listAdapter = listView.getAdapter();if (listAdapter == null) {return;}//固定列宽,有多少列//intcol=3;//listView.getNumColumns();int totalHeight = 0;//i每次加4,相当于listAdapter.getCount()小于等于4时循环一次,计算一次item的高度,//listAdapter.getCount()小于等于8时计算两次高度相加for (int i = 0; i < listAdapter.getCount(); i += col) {//获取listview的每一个itemView listItem = listAdapter.getView(i, null, listView);listItem.measure(0, 0);//获取item的高度和totalHeight += listItem.getMeasuredHeight();}//获取listview的布局参数ViewGroup.LayoutParams params = listView.getLayoutParams();//设置高度params.height = totalHeight;//设置margin((ViewGroup.MarginLayoutParams) params).setMargins(10, 10, 10, 10);//设置参数listView.setLayoutParams(params);
}
这篇关于关于listview,gridview里面数据显示不全的问题的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!