0
我有一个可变高度单元格的GridView。我希望该行与行中所有最大的单元一样高。我可以调整单元格高度以便在一行上保持一致,但我无法设置GridView的高度并使其实际发生更改。Android设置GridView高度
另一个问题是,这个GridView是在ScrollView中,所以有一个滚动条是不可能的。
这是一个问题,因为GridView决定整个Grid的高度的方法是取第一个单元格并乘以行数。如果行可以有不同的高度,这是一个明显的问题。例如:
我已经试过许多方法来更新它,但我相信,我失去了一些东西简单。我正在尝试在ViewTreeObserver中进行更新,所以我知道GridView已经呈现,所以我的计算是正确的(而且是)。代码:
ViewTreeObserver treeListener = mGridView.getViewTreeObserver();
treeListener.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
@SuppressLint("NewApi")
@SuppressWarnings("deprecation")
@Override
public void onGlobalLayout() {
if(Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
mGridView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
}
else {
mGridView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
}
// Calculate the new height we want for the GridView
int newHeight = determineCellHeight(mGridView, mNumberOfColumns, mRows);
ViewGroup.LayoutParams params = mGridView.getLayoutParams();
params.height = newHeight;
mGridView.setLayoutParams(params);
// Have tried all of these too!!!
// mAdapter.notifyDataSetChanged();
// mGridView.requestLayout();
// mGridView.invalidateViews();
// mGridView.refreshDrawableState();
// mGridView.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, newHeight + 10));
// mGridView.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, newHeight + 10));
// View lastChild = mGridView.getChildAt(mGridView.getChildCount() - 1);
// mGridView.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, lastChild.getBottom()));
// mAdapter.notifyDataSetChanged();
// mGridView.invalidateViews();
// mGridView.setMinimumHeight(newHeight);
// mGridView.requestLayout();
// mGridView.refreshDrawableState();
}
});
我开始不知道这甚至有可能,虽然众多Stackflows似乎表明它是...