本文整理汇总了Java中android.widget.GridView.getNumColumns方法的典型用法代码示例。如果您正苦于以下问题:Java GridView.getNumColumns方法的具体用法?Java GridView.getNumColumns怎么用?Java GridView.getNumColumns使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类android.widget.GridView
的用法示例。
在下文中一共展示了GridView.getNumColumns方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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: changeGridViewHeight
import android.widget.GridView; //导入方法依赖的package包/类
/**
* Change the height of a gridview according to the elements contained.
* @param gridview gridview
* @param items number of items
* @param gridViewResized boolean array
*/
public static void changeGridViewHeight(final GridView gridview, final int items,
final boolean gridViewResized[]) {
/**
* THIS IS A HACK!
*
* Problem: GridView inside a scrollView only shows one row.
* Solution: http://stackoverflow.com/questions/8481844/gridview-height-gets-cut
* Calculate the height for one row and then calculate many rows you have
* and resize the GridView height.
*/
final int columns = gridview.getNumColumns();
gridview.getViewTreeObserver().addOnGlobalLayoutListener(
new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
if (!gridViewResized[0]) {
gridViewResized[0] = true;
ViewGroup.LayoutParams params = gridview.getLayoutParams();
int oneRowHeight = gridview.getHeight();
int rows = items / columns;
if (items % columns != 0) {
rows++;
}
params.height = oneRowHeight * rows;
gridview.setLayoutParams(params);
}
}
});
}