SwipeRefreshLayout可以包裹一些组件,比如recyclerview,listview,webview等,实现下拉刷新,上拉加载,实例:
- 布局文件:
<android.support.v4.widget.SwipeRefreshLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" android:id="@+id/swipe_refresh_widget"> <android.support.v7.widget.RecyclerView android:id="@+id/my_recycler_view"
android:scrollbars="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"/> </android.support.v4.widget.SwipeRefreshLayout></pre>
- activity中,主要是设置刷新监听,刷新结束时,调用setRefreshing(false);隐藏刷新进度条
public class WaterfallActivity extends AppCompatActivity implements SwipeRefreshLayout.OnRefreshListener {
private SwipeRefreshLayout mSwipeRefreshWidget;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
......
mSwipeRefreshWidget = (SwipeRefreshLayout) findViewById(R.id.swipe_refresh_widget);
mSwipeRefreshWidget.setOnRefreshListener(this);
}
private static final int REFRESH_COMPLETE = 0X110; private Handler mHandler = new Handler()
{
public void handleMessage(android.os.Message msg)
{
switch (msg.what)
{
case REFRESH_COMPLETE:
mSwipeRefreshWidget.setRefreshing(false);
break;
}
};
};
@Override
public void onRefresh() {
mHandler.sendEmptyMessageDelayed(REFRESH_COMPLETE, 2000);
}
}
网友评论