欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

RecycleView瀑布流没有数据

程序员文章站 2022-07-12 12:58:26
...
package com.jiuxi.marriage.module.goods.manager;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.View;
import android.view.ViewGroup;

import com.jiuxi.marriage.utils.AppUtil;
import com.socks.library.KLog;

/**
 * Created by jiuxi on 17/5/22.
 */

public class NewStaggeredGridLayoutManager extends StaggeredGridLayoutManager {

    private boolean isScrollEnabled = true;

    public NewStaggeredGridLayoutManager(int spanCount, int orientation, Context mContext) {
        super(spanCount, orientation);
        mHeightArray = new int[spanCount];
        this.mContext = mContext;
        for (int i = 0; i < spanCount; i++)
            mHeightArray[i] = 0;
    }

    private int[] mMeasuredDimension = new int[2];
    private int[] mHeightArray;
    private Context mContext;


    @Override
    public void onLayoutChildren(RecyclerView.Recycler recycler, RecyclerView.State state) {
        try {
            super.onLayoutChildren(recycler, state);
        } catch (IndexOutOfBoundsException e) {
            //手动catch住
            e.printStackTrace();
            KLog.e("WrapContentGridLayoutManager", "手动捕获异常:" + e.getMessage());
        }
    }

    //设置recycleview是否能滑动
    public void setScrollEnabled(boolean flag) {
        this.isScrollEnabled = flag;
    }

    @Override
    public boolean canScrollVertically() {
        //Similarly you can customize "canScrollHorizontally()" for managing horizontal scroll
        return isScrollEnabled && super.canScrollVertically();
    }

    @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 int widthSize = View.MeasureSpec.getSize(widthSpec);
        final int heightSize = View.MeasureSpec.getSize(heightSpec);

        int width = 0;
        int height = 0;
        int count = getItemCount();
        int span = getSpanCount();
        for (int i = 0; i < span; i++)//防止多次调用onMeasure方法造成数据叠加
            mHeightArray[i] = 0;

        for (int i = 0; i < count; i++) {
            measureScrapChild(recycler, i,
                    View.MeasureSpec.makeMeasureSpec(i, View.MeasureSpec.UNSPECIFIED),
                    View.MeasureSpec.makeMeasureSpec(i, View.MeasureSpec.UNSPECIFIED),
                    mMeasuredDimension);
            if (getOrientation() == HORIZONTAL)
                calculatorStaggeredHeight(mMeasuredDimension[0]);
            else
                calculatorStaggeredHeight(mMeasuredDimension[1]);
        }
        if (getOrientation() == HORIZONTAL) {
            width = sort(mHeightArray);
//            height = ScreenUtils.getScreenHeight(mContext);//获取屏幕高度
            height = AppUtil.getScreenHeight(mContext);
        } else {
            height = sort(mHeightArray);
//            width = ScreenUtils.getScreenHeight(mContext);//获取屏幕宽度
            width = AppUtil.getScreenHeight(mContext);
        }
        switch (widthMode) {
            case View.MeasureSpec.EXACTLY:
                width = widthSize;
            case View.MeasureSpec.AT_MOST:
            case View.MeasureSpec.UNSPECIFIED:
        }

        switch (heightMode) {
            case View.MeasureSpec.EXACTLY:
                height = heightSize;
            case View.MeasureSpec.AT_MOST:
            case View.MeasureSpec.UNSPECIFIED:
        }
        setMeasuredDimension(width, height);
    }

    /**
     * 冒泡排序返回数组最大值
     *
     * @param unsorted
     * @return
     */
    private int sort(int[] unsorted) {
        for (int i = 0; i < unsorted.length; i++) {
            for (int j = i; j < unsorted.length; j++) {
                if (unsorted[i] < unsorted[j]) {
                    int temp = unsorted[i];
                    unsorted[i] = unsorted[j];
                    unsorted[j] = temp;
                }
            }
        }
        return unsorted[0];
    }

    /**
     * 将传入的item高度值赋给当前数组中最小的元素
     *
     * @param singleViewHeight 传入的item高度
     */
    private void calculatorStaggeredHeight(int singleViewHeight) {
        int index = 0;
        int minValue = mHeightArray[0];
        for (int i = 1; i < mHeightArray.length; i++) {
            if (minValue > mHeightArray[i]) {
                minValue = mHeightArray[i];
                index = i;
            }
        }
        mHeightArray[index] += singleViewHeight;
    }

    private void measureScrapChild(RecyclerView.Recycler recycler, int position, int widthSpec,
                                   int heightSpec, int[] measuredDimension) {
        if (position < getItemCount()) {
            try {
                View view = recycler.getViewForPosition(position);//fix 动态添加时报IndexOutOfBoundsException
                if (view != null) {
                    RecyclerView.LayoutParams p = (RecyclerView.LayoutParams) view.getLayoutParams();
                    int childWidthSpec = ViewGroup.getChildMeasureSpec(widthSpec,
                            getPaddingLeft() + getPaddingRight(), p.width);
                    int childHeightSpec = ViewGroup.getChildMeasureSpec(heightSpec,
                            getPaddingTop() + getPaddingBottom(), p.height);
                    view.measure(childWidthSpec, childHeightSpec);
                    measuredDimension[0] = view.getMeasuredWidth() + p.leftMargin + p.rightMargin;
                    measuredDimension[1] = view.getMeasuredHeight() + p.bottomMargin + p.topMargin;

                    KLog.v("p.height", p.height + "");
                    KLog.v("measuredDimension[1]", measuredDimension[1] + "");

                    recycler.recycleView(view);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}