一.SwipeRefreshLayout实现下拉刷新
1.方法API:
setOnRefreshListener(OnRefreshListener):添加下拉刷新监听器
setRefreshing(boolean):显示或者隐藏刷新进度条
isRefreshing():检查是否处于刷新状态
setColorSchemeResources():设置进度条的颜色主题,最多设置四种,以前的setColorScheme()方法已经弃用了。
2.简单使用:
<?xmlversionxmlversion="1.0" encoding="utf-8"?> <LinearLayoutxmlns:androidLinearLayoutxmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical"android:layout_width="match_parent" android:layout_height="match_parent"> <includelayoutincludelayout="@layout/common_top_bar_layout"/> <android.support.v4.widget.SwipeRefreshLayout android:id="@+id/demo_swiperefreshlayout" android:layout_width="fill_parent" android:layout_height="fill_parent" android:scrollbars="vertical" > <android.support.v7.widget.RecyclerView android:id="@+id/demo_recycler" android:layout_width="fill_parent" android:layout_height="fill_parent" ></android.support.v7.widget.RecyclerView> </android.support.v4.widget.SwipeRefreshLayout> </LinearLayout>
Java:
原理:是一个刷新布局,来自兼容包v4可以运行在低版本,控件如果想要支持下拉刷新,只要使用当前布局包裹
setColorSchemeColors:修改旋转颜色,可以添加多种颜色
setRefreshing: 是否显示loading状态
setOnRefreshListener:下拉刷新监听
1 public class MainActivity extends AppCompatActivity { 2 private SwipeRefreshLayout swipeRefreshLayout; 3 protected void onCreate(Bundle savedInstanceState) { 4 ····· 5 swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.refresh_layout); 6 //设置旋转的颜色效果 7 swipeRefreshLayout.setColorSchemeColors(Color.GREEN, Color.YELLOW, Color.RED); 8 //设置下拉刷新的监听器 9 swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() { 10 public void onRefresh() { 11 requestData(); 12 } 13 }); 14 } 15 private void requestData() { 16 new Handler().postDelayed(new Runnable() {//延时加载 17 public void run() { 18 String json = "{name:小米}"; 19 TextView text = (TextView) findViewById(R.id.text); 20 text.setText(json); 21 swipeRefreshLayout.setRefreshing(false);//关闭显示 22 } 23 }, 2000); 24 }
二.加载更多——上滑加载数据
思路:
1.滚动到底部 getItemCount()-2 ==bottomPosition
2.不处理滚动中
OnScrollListener监听滚动加载的监听器
int dy 滚动距离,向上滚动为正
layoutManager.findLastVisibleItemPosition获取处于底部数据的下标
dy>0与isLoading 都是用来控件灵敏度
1 public class HomeFragment extends Fragment { 2 private SwipeRefreshLayout swipeRefreshLayout; 3 private RecyclerView recyclerView; 4 private AppAdapter adapter; 5 @Override 6 public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { 7 //有一个显示网络状态 8 //StateLayout stateLayout = new StateLayout(container.getContext()); 9 swipeRefreshLayout = new SwipeRefreshLayout(container.getContext()); 10 //设置颜色 11 swipeRefreshLayout.setColorSchemeColors(Color.GREEN); 12 //设置下拉监听 13 swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() { 14 @Override 15 public void onRefresh() { 16 HttpConnect.get(ApiUrls.HOME+"?index=0", callback); 17 } 18 }); 19 recyclerView = new RecyclerView(container.getContext()); 20 //设置排列规则 21 recyclerView.setLayoutManager(new LinearLayoutManager(container.getContext())); 22 //预先设置了Noemal视图,但是normal视图没有内容 stateLayout.addNormalView(recyclerView); 23 swipeRefreshLayout.addView(recyclerView); 24 //发送请求给服务器 25 HttpConnect.get(ApiUrls.HOME+"?index=0",callback); 26 return swipeRefreshLayout; 27 } 28 DefaultCallBack callback = new DefaultCallBack() { 29 @Override 30 public void onStart(int what) { 31 swipeRefreshLayout.setRefreshing(true); 32 } 33 @Override 34 public void onFinish(int what) { 35 swipeRefreshLayout.setRefreshing(false); 36 } 37 //拿到数据 38 protected void createView(String json) { 39 HomeWebInfo info = new Gson().fromJson(json, HomeWebInfo.class); 40 //创建控件,设置适配器 41 adapter=new AppAdapter(info.list); 42 recyclerView.setAdapter(adapter); 43 //上滑加载数据 44 addLoadMoreList(); 45 } 46 }; 47 private void addLoadMoreList(){ 48 recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener(){ 49 private boolean isLoading = false; 50 //int dy 上下滑动的距离 +数代表往上滑动 51 public void onScrolled(RecyclerView recyclerView, int dx, int dy) { 52 super.onScrolled(recyclerView, dx, dy); 53 //判断 是否是排列布局 54 RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager(); 55 //判断是否是线性排列,并且是否滑动距离 56 if(layoutManager instanceof LinearLayoutManager && dy>0 ){ 57 //当前是列表 58 int total = adapter.getItemCount(); 59 int lastPosition = total-2; 60 //获取rv 的bottom条目 61 int currLastPosition = ((LinearLayoutManager) layoutManager).findLastVisibleItemPosition(); 62 if(currLastPosition==lastPosition&&!isLoading){ } 63 isLoading=true; 64 Toast.makeText(MyApp.getContext(),"加载更多...",Toast.LENGTH_SHORT).show(); 65 new Handler().postDelayed(new Runnable() { 66 @Override 67 public void run() { 68 Toast.makeText(MyApp.getContext(), "加载成功", Toast.LENGTH_SHORT).show(); 69 isLoading=false; 70 } 71 },5000); 72 } 73 } 74 }); 75 } 76 }