Search in sources :

Example 1 with OnRefreshLoadMoreListener

use of com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener in project SmartRefreshLayout by scwang90.

the class RepastPracticeActivity method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_practice_repast);
    final Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    toolbar.setNavigationOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            finish();
        }
    });
    final RefreshLayout refreshLayout = (RefreshLayout) findViewById(R.id.refreshLayout);
    refreshLayout.setEnableFooterFollowWhenLoadFinished(true);
    // 第一次进入演示刷新
    if (isFirstEnter) {
        isFirstEnter = false;
        refreshLayout.autoRefresh();
    }
    // 初始化列表和监听
    View view = findViewById(R.id.recyclerView);
    if (view instanceof RecyclerView) {
        RecyclerView recyclerView = (RecyclerView) view;
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        recyclerView.setAdapter(mAdapter = new BaseRecyclerAdapter<Model>(loadModels(), R.layout.listitem_practice_repast) {

            @Override
            protected void onBindViewHolder(SmartViewHolder holder, Model model, int position) {
                holder.text(R.id.name, model.name);
                holder.text(R.id.nickname, model.nickname);
                holder.image(R.id.image, model.imageId);
                holder.image(R.id.avatar, model.avatarId);
            }
        });
        refreshLayout.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {

            @Override
            public void onRefresh(@NonNull final RefreshLayout refreshLayout) {
                refreshLayout.getLayout().postDelayed(new Runnable() {

                    @Override
                    public void run() {
                        refreshLayout.finishRefresh();
                        // 恢复上拉状态
                        refreshLayout.setNoMoreData(false);
                    }
                }, 2000);
            }

            @Override
            public void onLoadMore(@NonNull final RefreshLayout refreshLayout) {
                refreshLayout.getLayout().postDelayed(new Runnable() {

                    @Override
                    public void run() {
                        if (mAdapter.getCount() > 12) {
                            Toast.makeText(getBaseContext(), "数据全部加载完毕", Toast.LENGTH_SHORT).show();
                            // 设置之后,将不会再触发加载事件
                            refreshLayout.finishLoadMoreWithNoMoreData();
                        } else {
                            mAdapter.loadMore(loadModels());
                            refreshLayout.finishLoadMore();
                        }
                    }
                }, 1000);
            }
        });
        refreshLayout.getLayout().postDelayed(new Runnable() {

            @Override
            public void run() {
                refreshLayout.setHeaderInsetStart(DensityUtil.px2dp(toolbar.getHeight()));
            }
        }, 500);
    }
    // 状态栏透明和间距处理
    StatusBarUtil.darkMode(this);
    StatusBarUtil.setPaddingSmart(this, view);
    StatusBarUtil.setPaddingSmart(this, toolbar);
    StatusBarUtil.setPaddingSmart(this, findViewById(R.id.blurView));
}
Also used : OnRefreshLoadMoreListener(com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener) SmartViewHolder(com.scwang.refreshlayout.adapter.SmartViewHolder) LinearLayoutManager(android.support.v7.widget.LinearLayoutManager) RecyclerView(android.support.v7.widget.RecyclerView) View(android.view.View) BaseRecyclerAdapter(com.scwang.refreshlayout.adapter.BaseRecyclerAdapter) DefaultItemAnimator(android.support.v7.widget.DefaultItemAnimator) RefreshLayout(com.scwang.smartrefresh.layout.api.RefreshLayout) RecyclerView(android.support.v7.widget.RecyclerView) Toolbar(android.support.v7.widget.Toolbar)

Aggregations

DefaultItemAnimator (android.support.v7.widget.DefaultItemAnimator)1 LinearLayoutManager (android.support.v7.widget.LinearLayoutManager)1 RecyclerView (android.support.v7.widget.RecyclerView)1 Toolbar (android.support.v7.widget.Toolbar)1 View (android.view.View)1 BaseRecyclerAdapter (com.scwang.refreshlayout.adapter.BaseRecyclerAdapter)1 SmartViewHolder (com.scwang.refreshlayout.adapter.SmartViewHolder)1 RefreshLayout (com.scwang.smartrefresh.layout.api.RefreshLayout)1 OnRefreshLoadMoreListener (com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener)1