package com.lcp.arecyclerview;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import android.view.View;
import android.widget.Toast;

import com.lcp.arecyclerview.adapter.RcGridFallAdapter;
import com.lcp.arecyclerview.adapter.RcListAdapter;
import com.lcp.arecyclerview.widget.ARecycleView;
import com.lcp.arecyclerview.widget.ARecyclerBaseAdapter;

import java.util.ArrayList;

/**
 * 瀑布流
 * Created by Aislli on 2016/2/29.
 */
public class RecyclerViewFallActivity extends Activity {
    private ARecycleView mRecyclerView;
    private ArrayList<String> list;
    private RcGridFallAdapter mReListAdapter;
    private RecyclerView.LayoutManager linearLayoutManager;
    Handler handler = new Handler();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.content_list);
        initView();
        initData();
    }

    private void initView() {
        mRecyclerView = (ARecycleView) findViewById(R.id.recyclerview);
    }

    private void initData() {
        list = new ArrayList<>();
        for (int i = 0; i < 60; i++) {
            list.add("item:" + i);
        }
        linearLayoutManager = new StaggeredGridLayoutManager(3, StaggeredGridLayoutManager.VERTICAL);
        mReListAdapter = new RcGridFallAdapter(this, list);
        mRecyclerView.setLayoutManager(linearLayoutManager);
        mRecyclerView.setAdapter(mReListAdapter);
        mRecyclerView.setTotal(list.size() + 20);//设置数据总数，加载完了就禁止掉加载更多事件
        setListener();
    }

    int moreNum = 1;
    int refreshNum = 1;

    private void setListener() {
        mRecyclerView.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        mReListAdapter.insert(refreshNum++ + "  Refresh things", 0);
                        mRecyclerView.setRefreshing(false);
                    }
                }, 1500);
            }
        });

        mRecyclerView.setOnLoadMoreListener(new ARecycleView.OnLoadMoreListener() {
            @Override
            public void loadMore() {
                handler.postDelayed(new Runnable() {
                    public void run() {
                        mReListAdapter.insert("More " + moreNum++, mReListAdapter.getItemCount());
                        mRecyclerView.hideMoreProgress();
                    }
                }, 1500);
            }
        });
        mReListAdapter.setOnItemClickListener(new RcListAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                Toast.makeText(getApplicationContext(), "position:" + position, Toast.LENGTH_SHORT).show();
            }
        });
        mReListAdapter.setOnItemLongClickListener(new ARecyclerBaseAdapter.OnItemLongClickListener() {
            @Override
            public void onItemLongClick(View view, int position) {
                Toast.makeText(getApplicationContext(), "long position:" + position, Toast.LENGTH_SHORT).show();
                mReListAdapter.remove(position);
            }
        });
    }
}
