content: package com.yl.recyclerview.activity;
  
  import android.graphics.Color;
  import android.os.Bundle;
  import android.support.v4.widget.SwipeRefreshLayout;
  import android.support.v7.app.AppCompatActivity;
  import android.support.v7.widget.GridLayoutManager;
  import android.support.v7.widget.LinearLayoutManager;
  import android.support.v7.widget.RecyclerView;
  import android.support.v7.widget.Toolbar;
  import android.view.Menu;
  import android.view.MenuInflater;
  import android.view.MenuItem;
  
  import com.yl.recyclerview.R;
  import com.yl.recyclerview.adapter.LoadMoreAdapter;
  import com.yl.recyclerview.listener.EndlessRecyclerOnScrollListener;
  
  import java.util.ArrayList;
  import java.util.List;
  import java.util.Timer;
  import java.util.TimerTask;
  
  /**
   * RecyclerView下拉刷新、上拉加载更多
   */
  
  public class LoadMoreActivity extends AppCompatActivity {
  
      private Toolbar toolbar;
      private SwipeRefreshLayout swipeRefreshLayout;
      private RecyclerView recyclerView;
      private LoadMoreAdapter loadMoreAdapter;
      private List<String> dataList = new ArrayList<>();
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_recyclerview);
  
          init();
      }
  
      private void init() {
          toolbar = (Toolbar) findViewById(R.id.toolbar);
          swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipe_refresh_layout);
          recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
  
          // 使用Toolbar替换ActionBar
          setSupportActionBar(toolbar);
  
          // 设置刷新控件颜色
          swipeRefreshLayout.setColorSchemeColors(Color.parseColor("#4DB6AC"));
  
          // 模拟获取数据
          getData();
          loadMoreAdapter = new LoadMoreAdapter(dataList);
          recyclerView.setLayoutManager(new LinearLayoutManager(this));
          recyclerView.setAdapter(loadMoreAdapter);
  
          // 设置下拉刷新
          swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
              @Override
              public void onRefresh() {
                  // 刷新数据
                  dataList.clear();
                  getData();
                  loadMoreAdapter.notifyDataSetChanged();
  
                  // 延时1s关闭下拉刷新
                  swipeRefreshLayout.postDelayed(new Runnable() {
                      @Override
                      public void run() {
                          if (swipeRefreshLayout != null && swipeRefreshLayout.isRefreshing()) {
                              swipeRefreshLayout.setRefreshing(false);
                          }
                      }
                  }, 1000);
              }
          });
  
          // 设置加载更多监听
          recyclerView.addOnScrollListener(new EndlessRecyclerOnScrollListener() {
              @Override
              public void onLoadMore() {
                  loadMoreAdapter.setLoadState(loadMoreAdapter.LOADING);
  
                  if (dataList.size() < 52) {
                      // 模拟获取网络数据，延时1s
                      new Timer().schedule(new TimerTask() {
                          @Override
                          public void run() {
                              runOnUiThread(new Runnable() {
                                  @Override
                                  public void run() {
                                      getData();
                                      loadMoreAdapter.setLoadState(loadMoreAdapter.LOADING_COMPLETE);
                                  }
                              });
                          }
                      }, 1000);
                  } else {
                      // 显示加载到底的提示
                      loadMoreAdapter.setLoadState(loadMoreAdapter.LOADING_END);
                  }
              }
          });
      }
  
      private void getData() {
          char letter = 'A';
          for (int i = 0; i < 26; i++) {
              dataList.add(String.valueOf(letter));
              letter++;
          }
      }
  
      @Override
      public boolean onCreateOptionsMenu(Menu menu) {
          MenuInflater menuInflater = getMenuInflater();
          menuInflater.inflate(R.menu.layout_menu, menu);
          return true;
      }
  
      @Override
      public boolean onOptionsItemSelected(MenuItem item) {
          switch (item.getItemId()) {
              case R.id.liner:
                  recyclerView.setLayoutManager(new LinearLayoutManager(this));
                  break;
  
              case R.id.grid:
                  recyclerView.setLayoutManager(new GridLayoutManager(this, 2));
                  break;
          }
          dataList.clear();
          getData();
          recyclerView.setAdapter(loadMoreAdapter);
          return super.onOptionsItemSelected(item);
      }
  }
  