Search in sources :

Example 1 with RefreshFootAdapter

use of com.chinaztt.fda.adapter.RefreshFootAdapter in project FastDev4Android by jiangqqlmj.

the class RecyclerFootActivity method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.recycler_footview_layout);
    top_bar_linear_back = (LinearLayout) this.findViewById(R.id.top_bar_linear_back);
    top_bar_linear_back.setOnClickListener(new CustomOnClickListener());
    top_bar_title = (TextView) this.findViewById(R.id.top_bar_title);
    top_bar_title.setText("RecyclerView使用FootView进行上拉加载更多...");
    demo_swiperefreshlayout = (SwipeRefreshLayout) this.findViewById(R.id.demo_swiperefreshlayout);
    demo_recycler = (RecyclerView) this.findViewById(R.id.demo_recycler);
    //设置刷新时动画的颜色,可以设置4个
    demo_swiperefreshlayout.setProgressBackgroundColorSchemeResource(android.R.color.white);
    demo_swiperefreshlayout.setColorSchemeResources(android.R.color.holo_blue_light, android.R.color.holo_red_light, android.R.color.holo_orange_light, android.R.color.holo_green_light);
    demo_swiperefreshlayout.setProgressViewOffset(false, 0, (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 24, getResources().getDisplayMetrics()));
    linearLayoutManager = new LinearLayoutManager(this);
    linearLayoutManager.setOrientation(OrientationHelper.VERTICAL);
    demo_recycler.setLayoutManager(linearLayoutManager);
    //添加分隔线
    demo_recycler.addItemDecoration(new AdvanceDecoration(this, OrientationHelper.VERTICAL));
    demo_recycler.setAdapter(adapter = new RefreshFootAdapter(this));
    demo_swiperefreshlayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {

        @Override
        public void onRefresh() {
            Log.d("zttjiangqq", "invoke onRefresh...");
            new Handler().postDelayed(new Runnable() {

                @Override
                public void run() {
                    List<String> newDatas = new ArrayList<String>();
                    for (int i = 0; i < 5; i++) {
                        int index = i + 1;
                        newDatas.add("new item" + index);
                    }
                    adapter.addItem(newDatas);
                    demo_swiperefreshlayout.setRefreshing(false);
                    Toast.makeText(RecyclerFootActivity.this, "更新了五条数据...", Toast.LENGTH_SHORT).show();
                }
            }, 5000);
        }
    });
    //RecyclerView滑动监听
    demo_recycler.setOnScrollListener(new RecyclerView.OnScrollListener() {

        @Override
        public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
            super.onScrollStateChanged(recyclerView, newState);
            if (newState == RecyclerView.SCROLL_STATE_IDLE && lastVisibleItem + 1 == adapter.getItemCount()) {
                adapter.changeMoreStatus(RefreshFootAdapter.LOADING_MORE);
                new Handler().postDelayed(new Runnable() {

                    @Override
                    public void run() {
                        List<String> newDatas = new ArrayList<String>();
                        for (int i = 0; i < 5; i++) {
                            int index = i + 1;
                            newDatas.add("more item" + index);
                        }
                        adapter.addMoreItem(newDatas);
                        adapter.changeMoreStatus(RefreshFootAdapter.PULLUP_LOAD_MORE);
                    }
                }, 2500);
            }
        }

        @Override
        public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
            super.onScrolled(recyclerView, dx, dy);
            lastVisibleItem = linearLayoutManager.findLastVisibleItemPosition();
        }
    });
}
Also used : ArrayList(java.util.ArrayList) Handler(android.os.Handler) LinearLayoutManager(android.support.v7.widget.LinearLayoutManager) SwipeRefreshLayout(android.support.v4.widget.SwipeRefreshLayout) AdvanceDecoration(com.chinaztt.fda.widget.AdvanceDecoration) RefreshFootAdapter(com.chinaztt.fda.adapter.RefreshFootAdapter) RecyclerView(android.support.v7.widget.RecyclerView)

Aggregations

Handler (android.os.Handler)1 SwipeRefreshLayout (android.support.v4.widget.SwipeRefreshLayout)1 LinearLayoutManager (android.support.v7.widget.LinearLayoutManager)1 RecyclerView (android.support.v7.widget.RecyclerView)1 RefreshFootAdapter (com.chinaztt.fda.adapter.RefreshFootAdapter)1 AdvanceDecoration (com.chinaztt.fda.widget.AdvanceDecoration)1 ArrayList (java.util.ArrayList)1