本文整理汇总了Java中android.widget.GridView.getAdapter方法的典型用法代码示例。如果您正苦于以下问题:Java GridView.getAdapter方法的具体用法?Java GridView.getAdapter怎么用?Java GridView.getAdapter使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类android.widget.GridView
的用法示例。
在下文中一共展示了GridView.getAdapter方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: setGridViewHeightByChildren
import android.widget.GridView; //导入方法依赖的package包/类
/**
* 根据子item的高度 动态测量GridView的实际高度
* @param gridView
*/
public static void setGridViewHeightByChildren(GridView gridView) {
ListAdapter listAdapter = gridView.getAdapter();
if (listAdapter == null) {
return;
}
//总高度
int totalHeight = 0;
int lineNum = gridView.getNumColumns(); //得到布局文件中设置的一行显示几个
View item = listAdapter.getView(0,null,gridView);
item.measure(0,0); //计算子item的高度
//得到总高度
totalHeight = item.getMeasuredHeight()*lineNum;
ViewGroup.LayoutParams params = gridView.getLayoutParams();
params.height = totalHeight;
gridView.setLayoutParams(params);
}
示例2: setGridViewHeightBasedOnChildren
import android.widget.GridView; //导入方法依赖的package包/类
public static void setGridViewHeightBasedOnChildren(GridView gridView, int colNum) {
ListAdapter listAdapter = gridView.getAdapter();
if (listAdapter == null) {
// pre-condition
return;
}
int totalHeight = 0;
int lines = (int) Math.ceil((double) listAdapter.getCount() / colNum);
for (int i = 0; i < lines; i++) {
View listItem = listAdapter.getView(i, null, gridView);
listItem.measure(0, 0);
totalHeight += listItem.getMeasuredHeight();
}
ViewGroup.LayoutParams params = gridView.getLayoutParams();
params.height = totalHeight + (gridView.getVerticalSpacing() * (lines - 1));
gridView.setLayoutParams(params);
}
示例3: setGridViewHeightBasedOnChildren
import android.widget.GridView; //导入方法依赖的package包/类
public static void setGridViewHeightBasedOnChildren(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);
}