Search in sources :

Example 1 with RefreshRecyclerAdapter

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

the class RecyclerRefreshActivity method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.recycler_refresh_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下拉刷新,下拉加载更多...");
    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 RefreshRecyclerAdapter(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(RecyclerRefreshActivity.this, "更新了五条数据...", Toast.LENGTH_SHORT).show();
                }
            }, 1500);
        }
    });
    // 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()) {
                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);
                    }
                }, 1000);
            }
        }

        @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) RecyclerView(android.support.v7.widget.RecyclerView) RefreshRecyclerAdapter(com.chinaztt.fda.adapter.RefreshRecyclerAdapter)

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 RefreshRecyclerAdapter (com.chinaztt.fda.adapter.RefreshRecyclerAdapter)1 AdvanceDecoration (com.chinaztt.fda.widget.AdvanceDecoration)1 ArrayList (java.util.ArrayList)1