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


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


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

<android.support.v4.widget.SwipeRefreshLayout
    android:id="@+id/srlDressfreshLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/white"
    android:scrollbars="vertical">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/rvSelectiveContent"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="@dimen/margin_ten"
        android:paddingTop="@dimen/margin_ten" />
</android.support.v4.widget.SwipeRefreshLayout>

然后就是 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<GoodsInfo> mGoodsInfos;
    private String mCatId;
    private IBasePresenter.ICatItemPresenter mCatFragmentPresenter;
    private List<GoodsInfo> 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的间距

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值