scrollview嵌套gridview显示不全的问题
程序员文章站
2022-07-01 20:31:46
...
在ScrollView里布局GridView时,GridView会出现显示不完全的问题。这是由于GridView和ScrollView里都有滑动属性,ScrollView不清楚GridView需要多大的高度,从源码的角度说,是因为ScrollView的MeasureSpec的mode为UNSPECIFIED,于是GridView的onMeasure方法拿到的heightMeasureSpec的mode也为UNSPECIFIED。我们看一下GridView中onMeasure方法
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// Sets up mListPadding
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int widthMode = MeasureSpec.getMode(widthMeasureSpec);
int heightMode = MeasureSpec.getMode(heightMeasureSpec);
int widthSize = MeasureSpec.getSize(widthMeasureSpec);
int heightSize = MeasureSpec.getSize(heightMeasureSpec);
if (widthMode == MeasureSpec.UNSPECIFIED) {
if (mColumnWidth > 0) {
widthSize = mColumnWidth + mListPadding.left + mListPadding.right;
} else {
widthSize = mListPadding.left + mListPadding.right;
}
widthSize += getVerticalScrollbarWidth();
}
int childWidth = widthSize - mListPadding.left - mListPadding.right;
boolean didNotInitiallyFit = determineColumns(childWidth);
int childHeight = 0;
int childState = 0;
mItemCount = mAdapter == null ? 0 : mAdapter.getCount();
final int count = mItemCount;
if (count > 0) {
final View child = obtainView(0, mIsScrap);
AbsListView.LayoutParams p = (AbsListView.LayoutParams) child.getLayoutParams();
if (p == null) {
p = (AbsListView.LayoutParams) generateDefaultLayoutParams();
child.setLayoutParams(p);
}
p.viewType = mAdapter.getItemViewType(0);
p.isEnabled = mAdapter.isEnabled(0);
p.forceAdd = true;
int childHeightSpec = getChildMeasureSpec(
MeasureSpec.makeSafeMeasureSpec(MeasureSpec.getSize(heightMeasureSpec),
MeasureSpec.UNSPECIFIED), 0, p.height);
int childWidthSpec = getChildMeasureSpec(
MeasureSpec.makeMeasureSpec(mColumnWidth, MeasureSpec.EXACTLY), 0, p.width);
child.measure(childWidthSpec, childHeightSpec);
childHeight = child.getMeasuredHeight();
childState = combineMeasuredStates(childState, child.getMeasuredState());
if (mRecycler.shouldRecycleViewType(p.viewType)) {
mRecycler.addScrapView(child, -1);
}
}
if (heightMode == MeasureSpec.UNSPECIFIED) {
heightSize = mListPadding.top + mListPadding.bottom + childHeight +
getVerticalFadingEdgeLength() * 2;
}
if (heightMode == MeasureSpec.AT_MOST) {
int ourSize = mListPadding.top + mListPadding.bottom;
final int numColumns = mNumColumns;
for (int i = 0; i < count; i += numColumns) {
ourSize += childHeight;
if (i + numColumns < count) {
ourSize += mVerticalSpacing;
}
if (ourSize >= heightSize) {
ourSize = heightSize;
break;
}
}
heightSize = ourSize;
}
if (widthMode == MeasureSpec.AT_MOST && mRequestedNumColumns != AUTO_FIT) {
int ourSize = (mRequestedNumColumns*mColumnWidth)
+ ((mRequestedNumColumns-1)*mHorizontalSpacing)
+ mListPadding.left + mListPadding.right;
if (ourSize > widthSize || didNotInitiallyFit) {
widthSize |= MEASURED_STATE_TOO_SMALL;
}
}
setMeasuredDimension(widthSize, heightSize);
mWidthMeasureSpec = widthMeasureSpec;
}
要解决的这个问题需要重新测量GridView的高度,自定义一个GridView,并重写onMeasure方法,如下
public class CustomGridView extends GridView {
public CustomGridView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomGridView(Context context) {
super(context);
}
public CustomGridView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,
MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, expandSpec);
}
}
转载于:https://www.jianshu.com/p/b09078830c4d
上一篇: TCP零窗口探测
推荐阅读
-
Android中ScrollView嵌套GridView显示不全解决方法
-
Android解决ScrollView下嵌套ListView和GridView中内容显示不全的问题
-
Android ScrollView 下嵌套 ListView 或 GridView出现问题解决办法
-
Android之ScrollView嵌套ListView和GridView冲突的解决方法
-
div嵌套时,背景图片不显示问题的解决方法
-
react-native android textinput显示不全的问题
-
ScrollView镶嵌listview显示不全的原因
-
U盘里的东西无法读取文件显示不全问题如何解决
-
GridView中图片显示出现上下间距过大,左右图片显示类似瀑布流的问题
-
ScrollView嵌套ListView显示不全的问题