SwipeRefreshLayout是Google提供的一个官方的下拉刷新控件,比传统的使用起来更简易。
效果图:
主要方法:
OnRefreshListener:刷新监听,下拉触发。
setRefreshing(true):设置刷新动画,取消传false。
setColorSchemeResources:设置刷新时动画的颜色。
1.布局文件
SwipeRefreshLayout是V4包下提供的,使用时需要把要刷新的控件包裹起来。 <android.support.v4.widget.SwipeRefreshLayout android:id="@+id/swipRefreshLayout" 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>
2.Activity
package com.bestyc.swiperefreshlayout; import java.util.ArrayList; import java.util.List; import com.bestyc.swiperefreshlayout.R; import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.support.v4.widget.SwipeRefreshLayout; import android.support.v4.widget.SwipeRefreshLayout.OnRefreshListener; import android.view.Window; import android.widget.ArrayAdapter; import android.widget.ListView; public class MainActivity extends Activity implements OnRefreshListener{ private ListView mListView; private ArrayAdapter<String> adapter; private List<String> mData; private SwipeRefreshLayout mSwipeRefreshLayout; Handler handler = new Handler(){ public void handleMessage(android.os.Message msg) { //关闭刷新 mSwipeRefreshLayout.setRefreshing(false); mListView.setAdapter(adapter); }; }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_main); mSwipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipRefreshLayout); mSwipeRefreshLayout.setOnRefreshListener(this); //设置刷新时的颜色 mSwipeRefreshLayout.setColorSchemeResources( android.R.color.holo_blue_bright, android.R.color.holo_green_light, android.R.color.holo_orange_light); mListView=(ListView) findViewById(R.id.listview); mData = new ArrayList<String>(); for (int i=0;i<=10;i++) { mData.add("加载的数据"+i); } adapter = new ArrayAdapter<String>(this, R.layout.item, R.id.text,mData); } @Override public void onRefresh() {//更新数据 handler.sendEmptyMessageDelayed(1, 3000); } }