1.概述
SwipeRefreshLayout是用于实现下拉刷新功能的核心类,由support-v4库提供。使用的话只需要将要实现下拉刷新的功能的控件放置到 SwipeRefreshLayout中即可。
2.案例
2.1 效果展示
效果如下所示:下拉即可出现下拉进度条,运行指定时间后,刷新结束,进度条隐藏
2.2 创建布局
布局如下所示:将要进行刷新的控件放入SwipeRefreshLayout即可
<?xml version="1.0" encoding="utf-8"?> <android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent"> <android.support.design.widget.AppBarLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <android.support.v7.widget.Toolbar android:scrollbars="vertical" android:id="@+id/toolbar" android:layout_height="?attr/actionBarSize" android:layout_width="match_parent" android:background="#3c3b3b" android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" app:popupTheme="@style/ThemeOverlay.AppCompat.Light" /> </android.support.design.widget.AppBarLayout> <!-- SwipeRefreshLayout --> <android.support.v4.widget.SwipeRefreshLayout android:id="@+id/swipe_refresh" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginTop="50dp"> <android.support.v4.widget.NestedScrollView android:layout_width="match_parent" android:layout_height="wrap_content" app:layout_behavior="@string/appbar_scrolling_view_behavior"> <LinearLayout android:layout_width="match_parent" android:layout_height="1200dp" android:orientation="vertical" android:background="#892f8a"> <TextView android:layout_width="match_parent" android:layout_height="200dp" android:text="nihaop" android:textColor="#ec0436" android:background="#7b7338" android:layout_marginTop="400dp"/> </LinearLayout> </android.support.v4.widget.NestedScrollView> </android.support.v4.widget.SwipeRefreshLayout> </android.support.design.widget.CoordinatorLayout>
2.3 业务处理
在MainActivity.java中处理刷新监听事件:
public class MainActivity_3 extends AppCompatActivity { private SwipeRefreshLayout swipeRefreshLayout; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_3_main); Toolbar toolbar = findViewById(R.id.toolbar); //设置支持的ActionBar setSupportActionBar(toolbar); swipeRefreshLayout = findViewById(R.id.swipe_refresh); //设置下拉刷新进度条的颜色 swipeRefreshLayout.setColorSchemeResources(R.color.colorAccent); //设置下拉刷新的监听器 swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() { @Override public void onRefresh() { Toast.makeText(MainActivity_3.this, "下拉刷新中...", Toast.LENGTH_SHORT).show(); refresh(); } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.toolbar,menu); return true; } private void refresh(){ //开启一个子线程 new Thread(new Runnable() { @Override public void run() { try{ //将线程沉睡2秒钟 Thread.sleep(2000); }catch (InterruptedException e){ e.printStackTrace(); } //将线程切回主线程 runOnUiThread(new Runnable() { @Override public void run() { //调用方法结束刷新事件,隐藏刷新进度条 swipeRefreshLayout.setRefreshing(false); } }); } }).start(); } }
通常来讲,onRefresh()方法中应该是去网络请求数据,然后处理数据,这里只是简单的实现刷新进度条的显示和隐藏