package com.lzx.demo.ui; import android.content.Context; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.Toolbar; import android.view.MenuItem; import android.widget.TextView; import com.github.jdsjlzx.ItemDecoration.DividerDecoration; 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.base.ListBaseAdapter; import com.lzx.demo.base.SuperViewHolder; import com.lzx.demo.bean.ItemModel; import com.lzx.demo.util.NetworkUtils; import java.lang.ref.WeakReference; import java.util.ArrayList; public class CollapsingToolbarLayoutActivity extends AppCompatActivity { /**服务器端一共多少条数据*/ private static final int TOTAL_COUNTER = 34; /**每一页展示多少条数据*/ 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.activity_collapsing_toolbar_layout); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); getSupportActionBar().setDisplayHomeAsUpEnabled(true); mRecyclerView = (LRecyclerView) findViewById(R.id.list); //init data ArrayList<ItemModel> dataList = new ArrayList<>(); for (int i = 0; i < 10; i++) { ItemModel item = new ItemModel(); item.id = i; item.title = "item" + i; dataList.add(item); } mCurrentCounter = dataList.size(); mDataAdapter = new DataAdapter(this); mDataAdapter.addAll(dataList); mLRecyclerViewAdapter = new LRecyclerViewAdapter(mDataAdapter); mRecyclerView.setAdapter(mLRecyclerViewAdapter); DividerDecoration divider = new DividerDecoration.Builder(this) .setHeight(R.dimen.default_divider_height) .setPadding(R.dimen.default_divider_padding) .setColorResource(R.color.split) .build(); mRecyclerView.setHasFixedSize(true); mRecyclerView.addItemDecoration(divider); mRecyclerView.setLayoutManager(new LinearLayoutManager(this)); 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); } } }); } private void notifyDataSetChanged() { mLRecyclerViewAdapter.notifyDataSetChanged(); } private void addItems(ArrayList<ItemModel> list) { mDataAdapter.addAll(list); mCurrentCounter += list.size(); } private class PreviewHandler extends Handler { private WeakReference<CollapsingToolbarLayoutActivity> ref; PreviewHandler(CollapsingToolbarLayoutActivity activity) { ref = new WeakReference<>(activity); } @Override public void handleMessage(Message msg) { final CollapsingToolbarLayoutActivity 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 < 10; 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); activity.notifyDataSetChanged(); 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; default: break; } } } /** * 模拟请求网络 */ private void requestData() { new Thread() { @Override public void run() { super.run(); try { Thread.sleep(800); } catch (InterruptedException e) { e.printStackTrace(); } //模拟一下网络请求失败的情况 if(NetworkUtils.isNetAvailable(CollapsingToolbarLayoutActivity.this)) { mHandler.sendEmptyMessage(-1); } else { mHandler.sendEmptyMessage(-3); } } }.start(); } private class DataAdapter extends ListBaseAdapter<ItemModel> { public DataAdapter(Context context) { super(context); } @Override public int getLayoutId() { return R.layout.list_item_text; } @Override public void onBindItemHolder(SuperViewHolder holder, int position) { ItemModel item = mDataList.get(position); TextView titleText = holder.getView(R.id.info_text); titleText.setText(item.title); } } @Override public boolean onOptionsItemSelected(MenuItem item) { if (item.getItemId() == android.R.id.home) { finish(); } return true; } }