package com.mayi.demo.page;

import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.coordinatorlayout.widget.CoordinatorLayout;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.android.material.appbar.AppBarLayout;
import com.mayi.demo.R;
import com.mayi.fastdevelop.base.BaseActivity;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

//滑动置顶,加载更多，下拉刷新
public class SlidingRoofPlacementActivity extends BaseActivity {

    private CoordinatorLayout coordinatorLayout;
    private AppBarLayout appBarLayout;
    private Handler handler;
    private int size = 100;
    private LinearLayoutManager manager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_slidingroofplacement);
        RecyclerView rvlist = findViewById(R.id.rv_list);
        coordinatorLayout = findViewById(R.id.scrollView);
        appBarLayout = findViewById(R.id.appBarLayout);
        manager = new LinearLayoutManager(this, RecyclerView.VERTICAL, false);
        rvlist.setLayoutManager(manager);
        rvlist.setAdapter(new RecyclerView.Adapter() {
            @NonNull
            @Override
            public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
                return new RecyclerView.ViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_slidingroofplacement
                        , parent, false)) {
                };
            }

            @Override
            public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
//                Log.i("coordinatorLayout", "  Adapter position=" + position);
                ((TextView) holder.itemView).setText("" + position);
            }

            @Override
            public int getItemCount() {
                return size;
            }
        });
        handler = new Handler();
        final SmartRefreshLayout layout = findViewById(R.id.layout);
        layout.setEnableRefresh(true);
        layout.setEnableLoadmore(false);
        layout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        size = 100;
                        rvlist.getAdapter().notifyDataSetChanged();
                        layout.finishRefresh();//完成刷新
                    }
                }, 1000);
            }
        });
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                if (rvlist.getScrollState() == RecyclerView.SCROLL_STATE_IDLE) {
                    if (manager.findLastVisibleItemPosition() + 10 > size) {
                        Log.i("coordinatorLayout", "  notifyDataSetChanged size=" + size);
                        size = size + 10;
                        rvlist.getAdapter().notifyDataSetChanged();
                    }
                }
                handler.postDelayed(this, 1000);
            }
        }, 1000);
    }
}
