SwipeRefreshLayout实质上是一个ViewGroup,所以将其作为根布局
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.swiperefreshdemo.MainActivity">
<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/swipeLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent">
</ListView>
</android.support.v4.widget.SwipeRefreshLayout>
</LinearLayout>
经过这个步骤,在页面上就已经能够下拉看到控件
public classMainActivity extends AppCompatActivity implementsSwipeRefreshLayout.OnRefreshListener{
private static final int REFRESH_COMPLETE=0x01;
private SwipeRefreshLayout refreshLayout;
private Handler handler=new Handler(){
public void handleMessage(android.os.Message msg){
switch(msg.what){
case REFRESH_COMPLETE:
refreshLayout.setRefreshng(false);
break;
}
}
};
@SuppressLint("ResourceAsColor")
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
refreshLayout=(SwipeRefreshLayout)findViewById(R.id.swipeLayout);
refreshLayout.setColorSchemeColors(Color.BLUE,Color.GREEN,Color.RED);
refreshLayout.setSize(SwipeRefreshLayout.DEFAULT);
refreshLayout.setOnRefreshListener(this);
}
public void onRefresh(){
//执行操作的更新等操作
//刷新成功
handler.sendEmptyMessageDelayed(REFRESH_COMPLETE,2000);
}
}
SwipeRefreshLayout方法介绍
- setColorSchemeResources(int…args):设置刷新时圆圈的颜色变化,为int数组
- setSize(int size):设置刷新时圆圈的大小,有DEFAULT(小)和LARGE两个值,默认是DEFAULT
- setOnRefreshListener(OnRefreshListener):设置刷新时回调监听事件,刷新时调用
- setRefreshing(boolean refreshing):设置是否继续正在刷新