Search in sources :

Example 1 with SingleItem

use of com.hpu.baserecyclerviewadapter.sample.item.SingleItem 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 SingleItem

use of com.hpu.baserecyclerviewadapter.sample.item.SingleItem in project BaseRecyclerViewAdapter by donghan0912.

the class SingleFragment method onRefresh.

@Override
public void onRefresh() {
    if (!isRefresh) {
        isRefresh = true;
        new Handler().postDelayed(new Runnable() {

            public void run() {
                mRefreshLayout.setRefreshing(false);
                isRefresh = false;
                List<BaseItem> list = new ArrayList<>();
                for (int i = 0; i < 10; i++) {
                    list.add(new SingleItem(R.drawable.actor));
                }
                adapter.insertData(list, 0);
            }
        }, 3000);
    }
}
Also used : Handler(android.os.Handler) ArrayList(java.util.ArrayList) List(java.util.List) SingleItem(com.hpu.baserecyclerviewadapter.sample.item.SingleItem)

Example 3 with SingleItem

use of com.hpu.baserecyclerviewadapter.sample.item.SingleItem 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)

Aggregations

Handler (android.os.Handler)3 SingleItem (com.hpu.baserecyclerviewadapter.sample.item.SingleItem)3 ArrayList (java.util.ArrayList)3 List (java.util.List)3 RecyclerView (android.support.v7.widget.RecyclerView)2 BaseRecyclerViewAdapter (com.hpu.baserecyclerviewadapter.BaseRecyclerViewAdapter)2 EndlessRecyclerOnScrollListener (com.hpu.baserecyclerviewadapter.EndlessRecyclerOnScrollListener)2 SimpleItem (com.hpu.baserecyclerviewadapter.SimpleItem)2 SwipeRefreshLayout (android.support.v4.widget.SwipeRefreshLayout)1 GridLayoutManager (android.support.v7.widget.GridLayoutManager)1 LinearLayoutManager (android.support.v7.widget.LinearLayoutManager)1 View (android.view.View)1 BaseItem (com.hpu.baserecyclerviewadapter.BaseItem)1 BaseViewHolder (com.hpu.baserecyclerviewadapter.BaseViewHolder)1