下拉刷新采用系统自带的SwipeRefreshLayout控件,先来看看SwipeRefreshLayout的设置。mWrlTest是SwipeRefreshLayout缩写,拿到实例,设置几个基本的属性,下拉时进度的颜色,下拉时背景圈的颜色(此处未设置,采用默认),设置圈圈大小,设置下拉刷新事件。
@Override protected void onInit() { /* 设置下拉颜色 */ mWrlTest.setColorSchemeResources( R.color.google_blue, R.color.google_green, R.color.google_red, R.color.google_yellow); /* 监听下拉事件 */ mWrlTest.setOnRefreshListener(this); mWrlTest.setSize(SwipeRefreshLayout.DEFAULT); LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this); /* 设置布局管理器 */ mRclTest.setLayoutManager(linearLayoutManager); /* 如果Item高度固定 增加该属性能够提高效率 */ mRclTest.setHasFixedSize(true); /* 设置adapter */ mRclTest.setAdapter(mAdapter); /* mRclTest.setAdapter(new TestAdapter(this,R.layout.activity_test_one,datas)); */ /* 设置Item增加、移除动画 */ mRclTest.setItemAnimator(new DefaultItemAnimator()); /* 关闭自动刷新 */ mRclTest.setAotoLoadMore(false); /* 添加分割线 */ mRclTest.addItemDecoration(new RecycleViewDivider( this, LinearLayoutManager.VERTICAL)); mRclTest.setLoadMoreListener(new LoadMoreRecyclerView.LoadMoreListener() { @Override public void onLoadMore(int currentPage) { new Handler().postDelayed(new Runnable() { @Override public void run() { if(mAdapter.getItemCount() > 0 && (mAdapter.getDatas().get(mAdapter.getItemCount()-1).getType() == Constants.ENTITY.LOADING_MORE)){ mAdapter.removeData((mAdapter.getItemCount()-1)); } mAdapter.addDataAll(loadMore()); mAdapter.notifyDataSetChanged(); mRclTest.setAotoLoadMore(false); } },2000); } }); }
@Override public void onRefresh() { new Handler().postDelayed(new Runnable() { @Override public void run() { mRclTest.setAotoLoadMore(true); mAdapter.clearData(); mAdapter.addDataAll(getDatas()); mAdapter.notifyDataSetChanged(); mWrlTest.setRefreshing(false); mRclTest.setCurrentPage(1); } },2000); }
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:background="@color/background"> <View android:id="@+id/status_bar" android:layout_width="match_parent" android:layout_height="0dip" /> <shjysoft.com.cloudseal.common.widgets.custom.TitleLayout android:id="@+id/lyt_title_bar" android:layout_width="match_parent" android:layout_height="@dimen/shjysoft_48dp" android:background="@color/google_yellow"/> <android.support.v4.widget.SwipeRefreshLayout android:id="@+id/wrl_test" android:layout_width="match_parent" android:layout_height="match_parent"> <shjysoft.com.cloudseal.common.widgets.recyclerview.LoadMoreRecyclerView android:id="@+id/rcl_test" android:layout_width="match_parent" android:layout_height="match_parent" /> </android.support.v4.widget.SwipeRefreshLayout> </LinearLayout>