package com.lzx.demo.ui; import android.graphics.Color; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.GridLayoutManager; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.Toolbar; import android.view.Menu; import android.view.MenuItem; import com.github.jdsjlzx.ItemDecoration.GridItemDecoration; import com.github.jdsjlzx.ItemDecoration.SpacesItemDecoration; import com.github.jdsjlzx.interfaces.OnLoadMoreListener; import com.github.jdsjlzx.interfaces.OnNetWorkErrorListener; import com.github.jdsjlzx.interfaces.OnRefreshListener; import com.github.jdsjlzx.recyclerview.LRecyclerView; import com.github.jdsjlzx.recyclerview.LRecyclerViewAdapter; import com.lzx.demo.R; import com.lzx.demo.adapter.DataAdapter; import com.lzx.demo.bean.ItemModel; import com.lzx.demo.util.NetworkUtils; import com.lzx.demo.view.SampleHeader; import java.lang.ref.WeakReference; import java.util.ArrayList; /** * 带HeaderView的分页加载GridLayout RecyclerView */ public class EndlessGridLayoutActivity extends AppCompatActivity { /**服务器端一共多少条数据*/ private static final int TOTAL_COUNTER = 24; /**每一页展示多少条数据*/ private static final int REQUEST_COUNT = 10; /**已经获取到多少条数据了*/ private static int mCurrentCounter = 0; private LRecyclerView mRecyclerView = null; private DataAdapter mDataAdapter = null; private PreviewHandler mHandler = new PreviewHandler(this); private LRecyclerViewAdapter mLRecyclerViewAdapter = null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.sample_ll_activity); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); getSupportActionBar().setDisplayHomeAsUpEnabled(true); mRecyclerView = (LRecyclerView) findViewById(R.id.list); //setLayoutManager must before setAdapter GridLayoutManager manager = new GridLayoutManager(this, 2); mRecyclerView.setLayoutManager(manager); mDataAdapter = new DataAdapter(this); mLRecyclerViewAdapter = new LRecyclerViewAdapter(mDataAdapter); mRecyclerView.setAdapter(mLRecyclerViewAdapter); //设置头部加载颜色 mRecyclerView.setHeaderViewColor(R.color.colorAccent, R.color.dark ,android.R.color.white); //设置底部加载颜色 mRecyclerView.setFooterViewColor(R.color.colorAccent, R.color.dark ,android.R.color.white); //设置底部加载文字提示 mRecyclerView.setFooterViewHint("拼命加载中","已经全部为你呈现了","网络不给力啊,点击再试一次吧"); int spacing = getResources().getDimensionPixelSize(R.dimen.dp_4); mRecyclerView.addItemDecoration(SpacesItemDecoration.newInstance(spacing, spacing, manager.getSpanCount(), Color.GRAY)); //根据需要选择使用GridItemDecoration还是SpacesItemDecoration GridItemDecoration divider = new GridItemDecoration.Builder(this) .setHorizontal(R.dimen.default_divider_padding) .setVertical(R.dimen.default_divider_padding) .setColorResource(R.color.split) .build(); //mRecyclerView.addItemDecoration(divider); mRecyclerView.setHasFixedSize(true); mLRecyclerViewAdapter.addHeaderView(new SampleHeader(this)); //设置span,自己可以体验效果 /*mLRecyclerViewAdapter.setSpanSizeLookup(new LRecyclerViewAdapter.SpanSizeLookup() { @Override public int getSpanSize(GridLayoutManager gridLayoutManager, int position) { if (position % 4 == 0) { return gridLayoutManager.getSpanCount(); } else { return 1; } } });*/ mRecyclerView.setOnRefreshListener(new OnRefreshListener() { @Override public void onRefresh() { mCurrentCounter = 0; mDataAdapter.clear(); requestData(); } }); mRecyclerView.setOnLoadMoreListener(new OnLoadMoreListener() { @Override public void onLoadMore() { if (mCurrentCounter < TOTAL_COUNTER) { // loading more requestData(); } else { //the end mRecyclerView.setNoMore(true); } } }); mRecyclerView.refresh(); } private void notifyDataSetChanged() { mLRecyclerViewAdapter.notifyDataSetChanged(); } private void addItems(ArrayList<ItemModel> list) { mDataAdapter.addAll(list); mCurrentCounter += list.size(); } private class PreviewHandler extends Handler { private WeakReference<EndlessGridLayoutActivity> ref; PreviewHandler(EndlessGridLayoutActivity activity) { ref = new WeakReference<>(activity); } @Override public void handleMessage(Message msg) { final EndlessGridLayoutActivity activity = ref.get(); if (activity == null || activity.isFinishing()) { return; } switch (msg.what) { case -1: int currentSize = activity.mDataAdapter.getItemCount(); //模拟组装10个数据 ArrayList<ItemModel> newList = new ArrayList<>(); for (int i = 0; i < 40; i++) { if (newList.size() + currentSize >= TOTAL_COUNTER) { break; } ItemModel item = new ItemModel(); item.id = currentSize + i; item.title = "item" + (item.id); newList.add(item); } activity.addItems(newList); activity.mRecyclerView.refreshComplete(10); break; case -2: activity.notifyDataSetChanged(); break; case -3: activity.mRecyclerView.refreshComplete(10); activity.notifyDataSetChanged(); activity.mRecyclerView.setOnNetWorkErrorListener(new OnNetWorkErrorListener() { @Override public void reload() { requestData(); } }); break; } } } /** * 模拟请求网络 */ private void requestData() { new Thread() { @Override public void run() { super.run(); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } //模拟一下网络请求失败的情况 if(NetworkUtils.isNetAvailable(EndlessGridLayoutActivity.this)) { mHandler.sendEmptyMessage(-1); } else { mHandler.sendEmptyMessage(-3); } } }.start(); } @Override public boolean onPrepareOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.menu_list_drag, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { if (item.getItemId() == android.R.id.home) { finish(); } else if (item.getItemId() == R.id.menu_grid) { mRecyclerView.setLayoutManager(new GridLayoutManager(this, 2)); mRecyclerView.setAdapter(mLRecyclerViewAdapter);//必须重新setAdapter }else if (item.getItemId() == R.id.menu_linear) { mRecyclerView.setLayoutManager(new LinearLayoutManager(this)); } return true; } }