Android 瀑布流StaggeredGridView 部分手机只显示第一行item问题



最近做一个瀑布流的demo,很奇怪的一个之前其他手机测都没有问题,新买了个小米4 (^H^),新手机上瀑布流的效果, 只显示了第一行,! 郁闷!

看看是什么原因导致!









在看一下listview的count 值,count>2,很奇怪,为什么不调用 getview 继续生产item 呢!!

baidu 没有找到类似的问题,难道是miui的问题,貌似其他手机也有这个问题,不能怪人家了,突然灵光一闪! 难道是显示的前两个item的高度无限高,

导致系统没有调用getView方法,修改item的布局文件的layout,高度由wrapcontent 改为指定大小,果然奏效!!







  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
针对这个问题,可能是由于瀑布流布局中的子 View 没有填满每一列导致的。可以尝试以下几种方法来解决这个问题: 1. 确保每一列的高度都是一致的,这样才能保证子 View 能够填满每一列。 2. 确保每一个子 View 的高度都是确定的,如果高度不确定,可以在代码中动态计算出每一个子 View 的高度。 3. 确保在添加子 View 的时候,使用的是 addViewInLayout() 方法,而不是 addView() 方法。addViewInLayout() 方法可以确保子 View 能够正确地添加到布局中。 4. 确保在添加子 View 的时候,使用的是 makeAndAddView() 方法,而不是 addView() 方法。makeAndAddView() 方法可以确保子 View 能够正确地添加到布局中,并且能够正确地计算出每一个子 View 的位置和大小。 以下是一个简单的瀑布流布局的示例代码,可以参考一下: ```xml <com.example.waterfalllayout.WaterfallLayout android:id="@+id/waterfall_layout" android:layout_width="match_parent" android:layout_height="match_parent" /> ``` ```java public class WaterfallLayout extends ViewGroup { private int mColumnCount = 3; // 列数 private int mColumnGap = 10; // 列之间的间隔 private int mRowGap = 10; // 行之间的间隔 private int[] mColumnHeight; // 每一列的高度 private List<View> mChildViews = new ArrayList<>(); // 所有子 View public WaterfallLayout(Context context) { super(context); init(); } public WaterfallLayout(Context context, AttributeSet attrs) { super(context, attrs); init(); } public WaterfallLayout(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); init(); } private void init() { mColumnHeight = new int[mColumnCount]; } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int width = MeasureSpec.getSize(widthMeasureSpec); int height = MeasureSpec.getSize(heightMeasureSpec); int childWidth = (width - mColumnGap * (mColumnCount - 1)) / mColumnCount; int childCount = getChildCount(); for (int i = 0; i < childCount; i++) { View child = getChildAt(i); measureChild(child, MeasureSpec.makeMeasureSpec(childWidth, MeasureSpec.EXACTLY), heightMeasureSpec); } int maxHeight = 0; for (int i = 0; i < mColumnCount; i++) { mColumnHeight[i] = 0; } for (int i = 0; i < childCount; i++) { View child = getChildAt(i); int columnIndex = getMinHeightColumnIndex(); int left = columnIndex * (childWidth + mColumnGap); int top = mColumnHeight[columnIndex]; int right = left + childWidth; int bottom = top + child.getMeasuredHeight(); child.layout(left, top, right, bottom); mColumnHeight[columnIndex] = bottom + mRowGap; maxHeight = Math.max(maxHeight, mColumnHeight[columnIndex]); } setMeasuredDimension(width, maxHeight); } @Override protected void onLayout(boolean changed, int l, int t, int r, int b) { // do nothing } private int getMinHeightColumnIndex() { int index = 0; int minHeight = mColumnHeight[0]; for (int i = 1; i < mColumnCount; i++) { if (mColumnHeight[i] < minHeight) { index = i; minHeight = mColumnHeight[i]; } } return index; } public void addChildView(View child) { mChildViews.add(child); addViewInLayout(child, -1, generateDefaultLayoutParams(), true); requestLayout(); } } ```
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值