首先布局:
<?xml version="1.0" encoding="utf-8"?> <android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:id="@+id/refresh" android:layout_height="match_parent" > <ScrollView android:layout_width="wrap_content" android:layout_height="wrap_content"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="我是系统自带的下拉刷新控件" android:gravity="center"/> </ScrollView> </android.support.v4.widget.SwipeRefreshLayout>Activity中调用
public class MainActivity extends AppCompatActivity implements SwipeRefreshLayout.OnRefreshListener { private SwipeRefreshLayout refresh; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); refresh = (SwipeRefreshLayout) findViewById(R.id.refresh); //设置下拉刷新的箭头颜色 refresh.setColorSchemeResources(android.R.color.holo_red_light); //设置下拉刷新的背景颜色为白色 refresh.setProgressBackgroundColorSchemeResource(android.R.color.white); refresh.setOnRefreshListener(this); refresh.setOnRefreshListener(this); } @Override public void onRefresh() { Toast.makeText(this, "下拉刷新成功", Toast.LENGTH_SHORT).show(); if (refresh.isRefreshing()) {//如果正在刷新 refresh.setRefreshing(false);//取消刷新 } } }