package com.mayi.demo;

import android.os.Bundle;
import android.os.Handler;

import com.mayi.fastdevelop.base.BaseActivity;
import com.mayi.fastdevelop.view.lazyLoading.ListLazyLoadingView;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

public class ListLazyLoadingActivity2 extends BaseActivity implements ListLazyLoadingView.ListLazyLoadingViewListener {

    private ListLazyLoadingView view;
    private MyListLazyLoadingAdapter adapter;
    private Handler handler;
    private List<Integer> list = new ArrayList<>();
    private int addItemCount, maxItemCount;
    private SmartRefreshLayout layout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_2);
        addItemCount = getIntent().getIntExtra("addItemCount", 10);
        maxItemCount = getIntent().getIntExtra("maxItemCount", 50);
        for (int i = 0; i < 20; i++) {
            list.add(0);
        }
        handler = new Handler();
        layout = findViewById(R.id.layout);
        view = findViewById(R.id.view);
        view.setListLazyLoadingViewListener(this);
        adapter = new MyListLazyLoadingAdapter(list);
        view.setListLazyLoadingAdapter(adapter);
        layout.setEnableRefresh(true);
        layout.setEnableLoadmore(false);
        layout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                list.clear();
                for (int i = 0; i < 20; i++) {
                    list.add(0);
                }
                layout.finishRefresh();//完成刷新
                view.setLoadingAll(false);
                adapter.notifyDataSetChanged();
            }
        });
    }

    @Override
    public void onLoadMore() {
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < addItemCount; i++) {
                    list.add(0);
                }
                if (list.size() >= maxItemCount) {
                    view.setLoadingAll(true);
                }
                view.finishLoadMore();
                adapter.notifyDataSetChanged();
            }
        }, 1000);
    }

}
