Search in sources :

Example 1 with FourthItem

use of com.hpu.baserecyclerviewadapter.sample.multi.FourthItem in project BaseRecyclerViewAdapter by donghan0912.

the class StaggeredFragment method onViewCreated.

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    SwipeRefreshLayout refreshLayout = (SwipeRefreshLayout) view.findViewById(R.id.swip_refresh);
    refreshLayout.setEnabled(false);
    RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.recycler_view);
    StaggeredGridLayoutManager manager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
    recyclerView.setLayoutManager(manager);
    for (int i = 0; i < 20; i++) {
        WQ wq = new WQ(getRandomHeight(), getDrawable());
        list.add(new FourthItem(wq));
    }
    final BaseRecyclerViewAdapter adapter = new BaseRecyclerViewAdapter(list);
    recyclerView.setAdapter(adapter);
    adapter.addHeader(new SimpleItem(R.layout.layout_staggered_head) {

        @Override
        public void onBindViewHolder(BaseViewHolder holder, int position) {
            StaggeredGridLayoutManager.LayoutParams p = (StaggeredGridLayoutManager.LayoutParams) holder.itemView.getLayoutParams();
            p.setFullSpan(true);
        }
    });
    // 设置加载更多布局,并占满整行
    adapter.setExtraItem(new SimpleItem(R.layout.layout_loadmore) {

        @Override
        public void onBindViewHolder(BaseViewHolder holder, int position) {
            StaggeredGridLayoutManager.LayoutParams p = (StaggeredGridLayoutManager.LayoutParams) holder.itemView.getLayoutParams();
            p.setFullSpan(true);
        }
    });
    recyclerView.addOnScrollListener(new EndlessRecyclerOnScrollListener() {

        @Override
        public void onLoadMore() {
            if (!loadMore) {
                return;
            }
            new Handler().postDelayed(new Runnable() {

                @Override
                public void run() {
                    new Handler().postDelayed(new Runnable() {

                        @Override
                        public void run() {
                            List<FourthItem> list2 = new ArrayList<>();
                            for (int i = 0; i < 21; i++) {
                                WQ wq = new WQ(getRandomHeight(), getDrawable());
                                list2.add(new FourthItem(wq));
                            }
                            list.addAll(list2);
                            if (list.size() > 70) {
                                adapter.setExtraItem(new SimpleItem(R.layout.layout_loadmore_complete) {

                                    @Override
                                    public void onBindViewHolder(BaseViewHolder holder, int position) {
                                        StaggeredGridLayoutManager.LayoutParams p = (StaggeredGridLayoutManager.LayoutParams) holder.itemView.getLayoutParams();
                                        p.setFullSpan(true);
                                    }
                                });
                                loadMore = false;
                            }
                            adapter.addData(list2);
                        }
                    }, 3000);
                }
            }, 3000);
        }
    });
}
Also used : BaseViewHolder(com.hpu.baserecyclerviewadapter.BaseViewHolder) EndlessRecyclerOnScrollListener(com.hpu.baserecyclerviewadapter.EndlessRecyclerOnScrollListener) Handler(android.os.Handler) StaggeredGridLayoutManager(android.support.v7.widget.StaggeredGridLayoutManager) SwipeRefreshLayout(android.support.v4.widget.SwipeRefreshLayout) FourthItem(com.hpu.baserecyclerviewadapter.sample.multi.FourthItem) RecyclerView(android.support.v7.widget.RecyclerView) WQ(com.hpu.baserecyclerviewadapter.sample.bean.WQ) ArrayList(java.util.ArrayList) List(java.util.List) BaseRecyclerViewAdapter(com.hpu.baserecyclerviewadapter.BaseRecyclerViewAdapter) SimpleItem(com.hpu.baserecyclerviewadapter.SimpleItem)

Aggregations

Handler (android.os.Handler)1 SwipeRefreshLayout (android.support.v4.widget.SwipeRefreshLayout)1 RecyclerView (android.support.v7.widget.RecyclerView)1 StaggeredGridLayoutManager (android.support.v7.widget.StaggeredGridLayoutManager)1 BaseRecyclerViewAdapter (com.hpu.baserecyclerviewadapter.BaseRecyclerViewAdapter)1 BaseViewHolder (com.hpu.baserecyclerviewadapter.BaseViewHolder)1 EndlessRecyclerOnScrollListener (com.hpu.baserecyclerviewadapter.EndlessRecyclerOnScrollListener)1 SimpleItem (com.hpu.baserecyclerviewadapter.SimpleItem)1 WQ (com.hpu.baserecyclerviewadapter.sample.bean.WQ)1 FourthItem (com.hpu.baserecyclerviewadapter.sample.multi.FourthItem)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1