Android开发之--Recyclerview实现瀑布流


首先说明,本文章瀑布流实现中,并没有拟定高度的实现,因为获取的数据就是不规则高度图片


首先,布局代码,就是一个recyclerview

    android:id="@+id/srlDressfreshLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/white"
    android:scrollbars="vertical">

            android:id="@+id/rvSelectiveContent"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="@dimen/margin_ten"
        android:paddingTop="@dimen/margin_ten" />

然后就是 Java代码

首先找到这个recyclkerview控件,然后为recyclerview添加 layoutManager、ItemDecoration、adapter

public class MoisturiseFragment extends BaseFragment implements IBaseView.ICatFragmentView {//护肤口类

    @Bind(R.id.rvSelectiveContent)
    RecyclerView rvSelectiveContent;//穿搭与化妆品类
    @Bind(R.id.srlDressfreshLayout)
    SwipeRefreshLayout srlMoisturise;
    private BaseActivity mActivity;
    // 标志位,标志已经初始化完成。
    private boolean isPrepared;
    private MoisturiseAdapter mMoisturiseAdapter;
    //private MoisturiseAdapterNew mMoisturiseAdapter;

    private ArrayList mGoodsInfos;
    private String mCatId;
    private IBasePresenter.ICatItemPresenter mCatFragmentPresenter;
    private List footerDatas = new ArrayList<>();

    private boolean isloading = false;
    private boolean needLoad = true;

    private int startPage = 0;

    public static MoisturiseFragment newInstance(String catId) {
        MoisturiseFragment fragment = new MoisturiseFragment();
        Bundle args = new Bundle();
        args.putString("catId", catId);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mActivity = (BaseActivity) getActivity();
        Bundle bundle = getArguments();
        if (bundle != null && bundle.containsKey("catId")) {
            mCatId = getArguments().getString("catId");
        }
    }

    @Override
    protected void lazyLoad() {
        if (!isPrepared || !isVisible) {
            return;
        }
    }

    @Override
    public int getContentViewId() {
        return R.layout.fragment_dress_outfit;
    }

    @Override
    protected void initAllView(Bundle savedInstanceState) {
        //rvSelectiveContent.setHasFixedSize(true);
        RecyclerView.LayoutManager layoutManager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
        rvSelectiveContent.setLayoutManager(layoutManager);
        int padding = DensityUtil.dip2px(mActivity, 5);
        rvSelectiveContent.addItemDecoration(new SpaceItemDecoration(padding, Constant.HUFU_TYPE));
        mGoodsInfos = new ArrayList<>();
        mCatFragmentPresenter = new CatFragmentPresenterCompl(this);
        //mMoisturiseAdapter = new MoisturiseAdapter(mActivity, mGoodsInfos);
        mMoisturiseAdapter = new MoisturiseAdapter(mActivity, mGoodsInfos);
        rvSelectiveContent.setAdapter(mMoisturiseAdapter);
        mCatFragmentPresenter.getCatArticles(mActivity, mCatId, startPage, 9, mActivity.lifecycleSubject);
        srlMoisturise.setColorSchemeResources(R.color.colorAccent,
                R.color.colorAccent,
                R.color.colorAccent,
                R.color.colorAccent);
        srlMoisturise.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        mCatFragmentPresenter.getCatArticles(mActivity, mCatId, startPage, 9, mActivity.lifecycleSubject);
                        if (srlMoisturise != null) {
                            srlMoisturise.setRefreshing(false);
                        }
                    }
                }, 2000);
            }
        });
        //上拉加载方法
        initLoadMoreListener();
    }

    private void initLoadMoreListener() {
        rvSelectiveContent.setOnScrollListener(new RecyclerView.OnScrollListener() {
            int lastVisibleItem;

            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                //判断RecyclerView的状态,是空闲时,同时,是最后一个可见的item时才加载
                if (newState == RecyclerView.SCROLL_STATE_IDLE && lastVisibleItem + 1 == mMoisturiseAdapter.getItemCount()) {
                    //设置正在加载
                    // mMoisturiseAdapter.changeMoreStatus(mMoisturiseAdapter.LOADING_MORE);

                    //每次page+1 请求数据
                    if (!isloading && needLoad) {
                        mCatFragmentPresenter.getCatArticles(mActivity, mCatId, ++startPage, 9, mActivity.lifecycleSubject);
                        isloading = true;
                    }
                }
            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                // LinearLayoutManager layoutManager = (LinearLayoutManager) recyclerView.getLayoutManager();
                //layoutManager.findViewByPosition(lastVisibleItem);
                super.onScrolled(recyclerView, dx, dy);
                StaggeredGridLayoutManager staggeredManager = (StaggeredGridLayoutManager) recyclerView.getLayoutManager();
                int[] lastPosition = new int[staggeredManager.getSpanCount()];
                staggeredManager.findLastVisibleItemPositions(lastPosition);
                lastVisibleItem = LoadmoreUtil.findMax(lastPosition);
            }
        });

    }

    @Override
    public void getCatArticles(int retCode, CategoryArticleData categoryArticleData) {
        isloading = false;
        if (categoryArticleData != null && categoryArticleData.articles != null) {
            if (startPage == 0) {
                mGoodsInfos.clear();
                mGoodsInfos.addAll(categoryArticleData.articles);
                Log.i("biaoqian", mGoodsInfos.get(2).thumb_image + "969");
                mMoisturiseAdapter.notifyDataSetChanged();
            } else {
                mGoodsInfos.addAll(categoryArticleData.articles);
                mMoisturiseAdapter.notifyDataSetChanged();
                //设置回到上拉加载更多
                //btnn.setVisibility(View.GONE);
                //adapterNew.changeMoreStatus(adapterNew.PULLUP_LOAD_MORE);
            }
        } else {
            if (startPage > 0) {
                //btnn.setVisibility(View.GONE);
                //adapterNew.changeMoreStatus(adapterNew.NO_LOAD_MORE);
                needLoad = false;
            }
        }
    }


然后是 代码中的 SpaceItemDecoration类

public class SpaceItemDecoration extends RecyclerView.ItemDecoration {

    private int space;
    private int type;
    int ss = 2;

    public SpaceItemDecoration(int space, int type) {
        this.space = space;
        this.type = type;
    }

    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        //不是第一个的格子都设一个左边和底部的间距
        int position = parent.getChildLayoutPosition(view);
        if (this.type == Constant.CHUANDA_TYPE) {
            outRect.left = space;
            outRect.bottom = ss;
            //由于每行都只有3个,所以第一个都是3的倍数,把右边距设为space
            if ((position + 1) % 3 == 0) {
                outRect.right = space;
            }
            if (position > 2) {
                outRect.top = space;
            }
        } else if (this.type == Constant.JING_XUAN_TYPE) {
            if (position > 0) {
                // outRect.left = space;
                outRect.bottom = space;
                //由于每行都只有3个,所以第一个都是3的倍数,把左边距设为space
                if (position % 3 == 0) {
                    outRect.right = space;
                }
            } else {
                outRect.bottom = space;
            }
 
  
            //瀑布流样式
        } else if (this.type == Constant.HUFU_TYPE) {
            outRect.left = space;
            if ((position + 1) % 2 == 0) {
                outRect.right = space;
            }
            outRect.bottom = space;
        } else if (this.type == Constant.VIDEO_TYPE) {
            outRect.left = space;
            outRect.bottom = space * 2;
            if ((position + 1) % 2 == 0) {
                outRect.right = space;
            }
        }
    }
}

在此 只看代码中 瀑布流样式中的设置,为了更好的设置recyclerview的间距

你可能感兴趣的:(Android)