Android RecyclerView实现下拉刷新和上拉加载

2019-12-10 18:00:17于海丽

布局文件

 

<RelativeLayout xmlns:android="http://www.easck.com/apk/res/android"
  xmlns:tools="http://www.easck.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:background="#ECEDF0"
  >

  <com.aspsine.swipetoloadlayout.SwipeToLoadLayout
    android:id="@+id/swipeToLoadLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.mahao.alex.systemwidgetdemo.recycleView.swipetoloadlayout.CircleRefreshHeaderView
      android:id="@+id/swipe_refresh_header"
      android:layout_width="match_parent"
      android:layout_height="wrap_content" />

    <android.support.v7.widget.RecyclerView
      android:id="@+id/swipe_target"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:scrollbars="vertical" />

    <com.mahao.alex.systemwidgetdemo.recycleView.swipetoloadlayout.LoaderMoreView
      android:id="@+id/swipe_load_more_footer"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:gravity="center"
      android:padding="20dp" />
  </com.aspsine.swipetoloadlayout.SwipeToLoadLayout>


</RelativeLayout>

public class SwipeToLayoutActivity extends AppCompatActivity implements OnRefreshListener, OnLoadMoreListener {



  private RecyclerView mRecycleView;

  SwipeToLoadLayout swipeToLoadLayout;

  private HomeAdapter adapter;


  @Override
  protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_recycle_swipetolayout);


    swipeToLoadLayout = ((SwipeToLoadLayout) findViewById(R.id.swipeToLoadLayout));


    mRecycleView = ((RecyclerView) findViewById(R.id.swipe_target));


    adapter = new HomeAdapter();

    //设置垂直的线性布局管理器,Orientation -->  VERTICAL:垂直  HORIZONTAL:水平
    LinearLayoutManager layoutManager = new LinearLayoutManager(this);
    layoutManager.setOrientation(LinearLayoutManager.VERTICAL);

//    StaggeredGridLayoutManager layoutManager = new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL);

    //添加分割线
    mRecycleView.addItemDecoration(new DividerItemDecoration(getApplicationContext(), DividerItemDecoration.VERTICAL_LIST));

    mRecycleView.setLayoutManager(layoutManager);

    mRecycleView.setItemAnimator(new DefaultItemAnimator());

    mRecycleView.setAdapter(adapter);

    adapter.refresh();

    /**
     * 设置下拉刷新和上拉加载监听
     */
    swipeToLoadLayout.setOnRefreshListener(this);
    swipeToLoadLayout.setOnLoadMoreListener(this);

  }




  @Override
  public void onRefresh() {
    swipeToLoadLayout.postDelayed(new Runnable() {
      @Override
      public void run() {
        adapter.refresh();
        swipeToLoadLayout.setRefreshing(false);
      }
    },2000);
  }

  @Override
  public void onLoadMore() {
    swipeToLoadLayout.postDelayed(new Runnable() {
      @Override
      public void run() {

        adapter.add();
        swipeToLoadLayout.setLoadingMore(false);
      }
    },2000);
  }
}