package com.mayi.demo.view;

import android.os.Bundle;
import android.view.ViewGroup;
import android.widget.TextView;

import com.mayi.demo.ItemBean;
import com.mayi.fastdevelop.base.BaseListActivity;
import com.mayi.fastdevelop.util.DateUtil;

/**
 * git 地址：https://github.com/scwang90/SmartRefreshLayout
 */
public class SmartRefreshLayoutActivity extends BaseListActivity<ItemBean, SmartRefreshLayoutVH> {

    int max = 80;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        title.setTextCenter("列表");
    }

    @Override
    protected SmartRefreshLayoutVH createVH(ViewGroup parent, int viewType) {
        SmartRefreshLayoutVH vh = new SmartRefreshLayoutVH(new TextView(parent.getContext()));
        vh.setLayoutVHListener(new SmartRefreshLayoutVH.SmartRefreshLayoutVHListener() {
            @Override
            public void onItemClick(ItemBean itemBean) {
                showToast(itemBean.getText());
            }
        });
        return vh;
    }

    @Override
    protected void onRefreshData() {
        dismssLoadMsg();
        stopLoadMoreData();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                list.clear();
                addData();
                startLoadMoreData();
                notifyData();
                finishRefresh();
            }
        }, 1000);
    }

    private void addData() {
        for (int i = 0; i < 20; i++) {
            list.add(new ItemBean(list.size() + "   " + DateUtil.getCurrentDate(), null));
        }
    }

    @Override
    protected void loadMoreData(int pageNumber) {
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                addData();
                notifyData();
                isLoadMore = false;
                if (list.size() >= max) {
                    stopLoadMoreData();
                    showLoadMsg("数据加载完成");
                } else {
                    dismssLoadMsg();
                }
            }
        }, 1000);
    }

}
