android recylerview 瀑布流显示异常终极解决方案

1.正常使用瀑布流流程

定义StaggeredGridLayoutManager

fullyStaggeredGridLayoutManager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
        fullyStaggeredGridLayoutManager.setGapStrategy(StaggeredGridLayoutManager.GAP_HANDLING_NONE);
        recyclerView.setLayoutManager(fullyStaggeredGridLayoutManager);
        newTopicAdapter = new NewTopicAdapter(NewTopicDetailActivity.this, datas);
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        recyclerView.setAdapter(newTopicAdapter);

2.如果发现布局显示异常

可以使用网上的一个自定义的StaggeredGridLayoutManager

package com.unice.longqihair.manager;

import android.content.Context;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import com.unice.longqihair.BuildConfig;

import java.lang.reflect.Field;

/**
 * @author Created by yyj on 2018/8/8
 * @descride 解决Scrollview中嵌套RecyclerView实现瀑布流时无法显示的问题,同时修复了子View显示时底部多出空白区域的问题
 */
public class FullyStaggeredGridLayoutManager extends StaggeredGridLayoutManager {
  private static boolean canMakeInsetsDirty = true;
  private static Field insetsDirtyField = null;

  private static final int CHILD_WIDTH = 0;
  private static final int CHILD_HEIGHT = 1;
  private static final int DEFAULT_CHILD_SIZE = 100;
  private int spanCount = 0;

  private final int[] childDimensions = new int[2];
  private int[] childColumnDimensions;

  private int childSize = DEFAULT_CHILD_SIZE;
  private boolean hasChildSize;
  private final Rect tmpRect = new Rect();

  public FullyStaggeredGridLayoutManager(Context context, AttributeSet attrs, int defStyleAttr,
      int defStyleRes) {
    super(context, attrs, defStyleAttr, defStyleRes);
  }

  public FullyStaggeredGridLayoutManager(int spanCount, int orientation) {
    super(spanCount, orientation);
    this.spanCount = spanCount;
  }

  public static int makeUnspecifiedSpec() {
    return View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
  }

  @Override
  public void onMeasure(RecyclerView.Recycler recycler, RecyclerView.State state, int widthSpec,
                        int heightSpec) {
    final int widthMode = View.MeasureSpec.getMode(widthSpec);
    final int heightMode = View.MeasureSpec.getMode(heightSpec);

    final i
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值