Search in sources :

Example 1 with BaseRecyclerViewAdapter

use of com.hpu.baserecyclerviewadapter.BaseRecyclerViewAdapter in project BaseRecyclerViewAdapter by donghan0912.

the class SingleFragment method onViewCreated.

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    mRefreshLayout = (SwipeRefreshLayout) view.findViewById(R.id.swip_refresh);
    mRefreshLayout.setColorSchemeResources(R.color.blue_bright, R.color.green_light, R.color.red_light);
    mRefreshLayout.setOnRefreshListener(this);
    mRefreshLayout.setEnabled(false);
    final RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.recycler_view);
    recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
    // 测试数据
    final List<BaseItem> testData = new ArrayList<>();
    for (int i = 1; i < 20; i++) {
        testData.add(new SingleItem(getAvatar()));
    }
    adapter = new BaseRecyclerViewAdapter<>();
    recyclerView.setAdapter(adapter);
    // 添加头布局
    adapter.addHeader(new SimpleItem(R.layout.layout_head) {

        @Override
        public void onBindViewHolder(final BaseViewHolder holder, final int position) {
        // 绑定数据
        }
    });
    // 添加正在加载布局
    adapter.setStatusItem(new SimpleItem(R.layout.layout_loading));
    // 模拟网络环境,3秒后加载数据
    new Handler().postDelayed(new Runnable() {

        @Override
        public void run() {
            // 数据加载成功之后,方可下拉刷新
            mRefreshLayout.setEnabled(true);
            // adapter加载数据
            adapter.setData(testData);
            // 如果有更多数据,设置加载更多布局
            adapter.setExtraItem(new SimpleItem(R.layout.layout_loadmore));
        }
    }, 3000);
    // 添加点击事件
    adapter.setOnItemClickListener(new BaseRecyclerViewAdapter.OnItemClickListener() {

        @Override
        public void onItemClick(View view, int position) {
            Toast.makeText(getContext(), position + "", Toast.LENGTH_SHORT).show();
        }
    });
    // 上拉加载更多监听
    recyclerView.addOnScrollListener(new EndlessRecyclerOnScrollListener() {

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

                @Override
                public void run() {
                    // 设置加载更多失败
                    adapter.setExtraItem(new SimpleItem(R.layout.layout_loadmore_error) {

                        @Override
                        public void onBindViewHolder(BaseViewHolder holder, int position) {
                            // 点击重试加载更多事件
                            holder.itemView.setOnClickListener(new View.OnClickListener() {

                                @Override
                                public void onClick(View v) {
                                    adapter.setExtraItem(new SimpleItem(R.layout.layout_loadmore));
                                    new Handler().postDelayed(new Runnable() {

                                        @Override
                                        public void run() {
                                            List<BaseItem> list2 = new ArrayList<>();
                                            for (int i = 20; i < 40; i++) {
                                                list2.add(new SingleItem(getAvatar()));
                                            }
                                            list.addAll(list2);
                                            adapter.setExtraItem(new SimpleItem(R.layout.layout_loadmore_complete));
                                            loadMore = false;
                                            adapter.addData(list);
                                        }
                                    }, 3000);
                                }
                            });
                        }
                    });
                }
            }, 3000);
        }
    });
}
Also used : BaseViewHolder(com.hpu.baserecyclerviewadapter.BaseViewHolder) EndlessRecyclerOnScrollListener(com.hpu.baserecyclerviewadapter.EndlessRecyclerOnScrollListener) ArrayList(java.util.ArrayList) Handler(android.os.Handler) LinearLayoutManager(android.support.v7.widget.LinearLayoutManager) RecyclerView(android.support.v7.widget.RecyclerView) View(android.view.View) RecyclerView(android.support.v7.widget.RecyclerView) SingleItem(com.hpu.baserecyclerviewadapter.sample.item.SingleItem) ArrayList(java.util.ArrayList) List(java.util.List) BaseRecyclerViewAdapter(com.hpu.baserecyclerviewadapter.BaseRecyclerViewAdapter) BaseItem(com.hpu.baserecyclerviewadapter.BaseItem) SimpleItem(com.hpu.baserecyclerviewadapter.SimpleItem)

Example 2 with BaseRecyclerViewAdapter

use of com.hpu.baserecyclerviewadapter.BaseRecyclerViewAdapter in project BaseRecyclerViewAdapter by donghan0912.

the class GlidFragment 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);
    GridLayoutManager manager = new GridLayoutManager(getActivity(), 2);
    recyclerView.setLayoutManager(manager);
    for (int i = 0; i < 20; i++) {
        list.add(new SingleItem(getAvatar()));
    }
    final BaseRecyclerViewAdapter adapter = new BaseRecyclerViewAdapter(list);
    recyclerView.setAdapter(adapter);
    adapter.addHeader(new SimpleItem(R.layout.layout_head));
    adapter.setExtraItem(new SimpleItem(R.layout.layout_loadmore));
    manager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {

        @Override
        public int getSpanSize(int position) {
            return adapter.isExtra(position) || adapter.isHeader(position) || adapter.isFooter(position) || adapter.isStatus(position) ? 2 : 1;
        }
    });
    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<BaseItem> list2 = new ArrayList<>();
                            for (int i = 0; i < 20; i++) {
                                list2.add(new SingleItem(getAvatar()));
                            }
                            list.addAll(list2);
                            if (list.size() > 40) {
                                adapter.removeExtraItem();
                                loadMore = false;
                            }
                            adapter.addData(list2);
                        }
                    }, 3000);
                }
            }, 3000);
        }
    });
}
Also used : EndlessRecyclerOnScrollListener(com.hpu.baserecyclerviewadapter.EndlessRecyclerOnScrollListener) Handler(android.os.Handler) SwipeRefreshLayout(android.support.v4.widget.SwipeRefreshLayout) GridLayoutManager(android.support.v7.widget.GridLayoutManager) RecyclerView(android.support.v7.widget.RecyclerView) SingleItem(com.hpu.baserecyclerviewadapter.sample.item.SingleItem) ArrayList(java.util.ArrayList) List(java.util.List) BaseRecyclerViewAdapter(com.hpu.baserecyclerviewadapter.BaseRecyclerViewAdapter) SimpleItem(com.hpu.baserecyclerviewadapter.SimpleItem)

Example 3 with BaseRecyclerViewAdapter

use of com.hpu.baserecyclerviewadapter.BaseRecyclerViewAdapter in project BaseRecyclerViewAdapter by donghan0912.

the class MultiTypeFragment 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);
    recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
    list = new ArrayList<>();
    for (int i = 0; i < 6; i++) {
        list.add(new FirstItem("第" + i + "条数据"));
        list.add(new SecondItem("第" + i + "条数据"));
        if (i == 0) {
            list.add(new ThirdItem(R.drawable.luoluo_1));
        } else if (i == 1) {
            list.add(new ThirdItem(R.drawable.luoluo_2));
        } else if (i == 2) {
            list.add(new ThirdItem(R.drawable.luoluo_3));
        } else if (i == 3) {
            list.add(new ThirdItem(R.drawable.luoluo_4));
        } else if (i == 4) {
            list.add(new ThirdItem(R.drawable.luoluo_5));
        } else if (i == 5) {
            list.add(new ThirdItem(R.drawable.luoluo_6));
        }
    }
    final BaseRecyclerViewAdapter baseRecyclerViewAdapter = new BaseRecyclerViewAdapter();
    recyclerView.setAdapter(baseRecyclerViewAdapter);
    baseRecyclerViewAdapter.setOnItemClickListener(new BaseRecyclerViewAdapter.OnItemClickListener() {

        @Override
        public void onItemClick(View view, int position) {
            Toast.makeText(getContext(), position + "", Toast.LENGTH_SHORT).show();
        }
    });
    baseRecyclerViewAdapter.setOnItemLongClickListener(new BaseRecyclerViewAdapter.OnItemLongClickListener() {

        @Override
        public boolean onItemLongClick(View view, int position) {
            Toast.makeText(getContext(), position + "", Toast.LENGTH_SHORT).show();
            return false;
        }
    });
    baseRecyclerViewAdapter.setStatusItem(new SimpleItem(R.layout.layout_loading));
    new Handler().postDelayed(new Runnable() {

        @Override
        public void run() {
            baseRecyclerViewAdapter.setStatusItem(new SimpleItem(R.layout.layout_error) {

                @Override
                public void onBindViewHolder(BaseViewHolder holder, int position) {
                    holder.setOnClickListener(R.id.retry, new View.OnClickListener() {

                        @Override
                        public void onClick(View v) {
                            baseRecyclerViewAdapter.setStatusItem(new SimpleItem(R.layout.layout_loading));
                            new Handler().postDelayed(new Runnable() {

                                @Override
                                public void run() {
                                    baseRecyclerViewAdapter.setData(list);
                                }
                            }, 3000);
                        }
                    });
                }
            });
        }
    }, 3000);
}
Also used : BaseViewHolder(com.hpu.baserecyclerviewadapter.BaseViewHolder) Handler(android.os.Handler) LinearLayoutManager(android.support.v7.widget.LinearLayoutManager) SwipeRefreshLayout(android.support.v4.widget.SwipeRefreshLayout) RecyclerView(android.support.v7.widget.RecyclerView) View(android.view.View) ThirdItem(com.hpu.baserecyclerviewadapter.sample.multi.ThirdItem) SecondItem(com.hpu.baserecyclerviewadapter.sample.multi.SecondItem) FirstItem(com.hpu.baserecyclerviewadapter.sample.multi.FirstItem) RecyclerView(android.support.v7.widget.RecyclerView) BaseRecyclerViewAdapter(com.hpu.baserecyclerviewadapter.BaseRecyclerViewAdapter) SimpleItem(com.hpu.baserecyclerviewadapter.SimpleItem)

Example 4 with BaseRecyclerViewAdapter

use of com.hpu.baserecyclerviewadapter.BaseRecyclerViewAdapter 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)4 RecyclerView (android.support.v7.widget.RecyclerView)4 BaseRecyclerViewAdapter (com.hpu.baserecyclerviewadapter.BaseRecyclerViewAdapter)4 SimpleItem (com.hpu.baserecyclerviewadapter.SimpleItem)4 SwipeRefreshLayout (android.support.v4.widget.SwipeRefreshLayout)3 BaseViewHolder (com.hpu.baserecyclerviewadapter.BaseViewHolder)3 EndlessRecyclerOnScrollListener (com.hpu.baserecyclerviewadapter.EndlessRecyclerOnScrollListener)3 ArrayList (java.util.ArrayList)3 List (java.util.List)3 LinearLayoutManager (android.support.v7.widget.LinearLayoutManager)2 View (android.view.View)2 SingleItem (com.hpu.baserecyclerviewadapter.sample.item.SingleItem)2 GridLayoutManager (android.support.v7.widget.GridLayoutManager)1 StaggeredGridLayoutManager (android.support.v7.widget.StaggeredGridLayoutManager)1 BaseItem (com.hpu.baserecyclerviewadapter.BaseItem)1 WQ (com.hpu.baserecyclerviewadapter.sample.bean.WQ)1 FirstItem (com.hpu.baserecyclerviewadapter.sample.multi.FirstItem)1 FourthItem (com.hpu.baserecyclerviewadapter.sample.multi.FourthItem)1 SecondItem (com.hpu.baserecyclerviewadapter.sample.multi.SecondItem)1 ThirdItem (com.hpu.baserecyclerviewadapter.sample.multi.ThirdItem)1