Search in sources :

Example 1 with BaseItem

use of com.hpu.baserecyclerviewadapter.BaseItem 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)

Aggregations

Handler (android.os.Handler)1 LinearLayoutManager (android.support.v7.widget.LinearLayoutManager)1 RecyclerView (android.support.v7.widget.RecyclerView)1 View (android.view.View)1 BaseItem (com.hpu.baserecyclerviewadapter.BaseItem)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 SingleItem (com.hpu.baserecyclerviewadapter.sample.item.SingleItem)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1