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似乎表明它是...