下拉刷新控件(3)系统自带的下拉刷新控件SwipeRefreshLayout(推荐*)

1.简介

  The SwipeRefreshLayout should be used whenever the user can refresh the contents of a view via a vertical swipe gesture. The activity that instantiates this view should add an OnRefreshListener to be notified whenever the swipe to refresh gesture is completed. The SwipeRefreshLayout will notify the listener each and every time the gesture is completed again; the listener is responsible for correctly determining when to actually initiate a refresh of its content. If the listener determines there should not be a refresh, it must call setRefreshing(false) to cancel any visual indication of a refresh. If an activity wishes to show just the progress animation, it should call setRefreshing(true). To disable the gesture and progress animation, call setEnabled(false) on the view.

  This layout should be made the parent of the view that will be refreshed as a result of the gesture and can only support one direct child. This view will also be made the target of the gesture and will be forced to match both the width and the height supplied in this layout. The SwipeRefreshLayout does not provide accessibility events; instead, a menu item must be provided to allow refresh of the content wherever this gesture is used.

SwipeRefreshLayout 是支援包里的一个布局类。通过识别垂直下拉手势显示刷新动画,它要作为要刷新内容的父控件,并且只能有一个直接子view。推荐使用它,不要自写了。刷新效果如下图中的圆圈:

下拉刷新控件(3)系统自带的下拉刷新控件SwipeRefreshLayout(推荐*)

它是一个layout,里面只能放:ListView,GridView.但经测试,也可放ScrollView。官方原文如下:

  To add the swipe to refresh widget to an existing app, add SwipeRefreshLayout as the parent of a single ListView or GridView. Remember that SwipeRefreshLayout only supports a single ListView or GridView child.

2.listview示例

xml

 <?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.SwipeRefreshLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/swipe">
<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent" /> </android.support.v4.widget.SwipeRefreshLayout>

java代码

 import java.util.ArrayList;

 import android.app.Fragment;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v4.widget.SwipeRefreshLayout.OnRefreshListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView.OnScrollListener;
import android.widget.AbsListView;
import android.widget.ArrayAdapter;
import android.widget.ListView; import com.example.pullrefreshview.R; public class SwipeRefreshListViewFrgmt extends Fragment implements OnRefreshListener{
SwipeRefreshLayout refreshLayout;
ListView listview;
ArrayList<String> datas;
ArrayAdapter<String> adapter; void initAdapter(){
datas = new ArrayList<String>();
for (int i = ; i < ; i++) {
datas.add("item " + i);
}
adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1,android.R.id.text1,datas);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
initAdapter();
refreshLayout = (SwipeRefreshLayout) inflater.inflate(R.layout.frgmt_srl_listview,container,false);
refreshLayout.setEnabled(false); //默认先关闭 下拉刷新功能
refreshLayout.setOnRefreshListener(this);
listview = (ListView) refreshLayout.findViewById(android.R.id.list);
listview.setAdapter(adapter);
listview.setOnScrollListener(new OnScrollListener() {
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
if (firstVisibleItem == )
refreshLayout.setEnabled(true);
else
refreshLayout.setEnabled(false);
}
});
return refreshLayout;
}
@Override
public void onRefresh() {
//layout正在刷新时回调,
( new Handler()).postDelayed(new Runnable() {//3秒后关闭刷新。
@Override
public void run() {
refreshLayout.setRefreshing(false);
}
}, );
}
}

基本流程:  

  • 在xml中 添加
  • 在代码中找到它
  • 设置它的 OnRefreshListener
  • 在onRefresh中写更新代码
  • 关闭刷新

注意第36行,先把刷新功能关闭。当满足某条件时再打开它(第48行)。

3.scrollview示例

 <?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.SwipeRefreshLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/swipe">
<ScrollView
android:id="@+id/scrollview"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/rndNum"
android:hint="random number "
/>
</LinearLayout>
</ScrollView>
</android.support.v4.widget.SwipeRefreshLayout>

java代码

 import android.app.Fragment;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v4.widget.SwipeRefreshLayout.OnRefreshListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView; import com.example.pullrefreshview.R; public class SwipeRefreshScrollViewFrgmt extends Fragment implements OnRefreshListener {
SwipeRefreshLayout refreshView;
TextView rndNum ;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
refreshView = (SwipeRefreshLayout) inflater.inflate(R.layout.frgmt_srl_scrollview, container,false);
refreshView.setColorSchemeResources(android.R.color.holo_blue_dark, android.R.color.holo_blue_light, android.R.color.holo_green_light, android.R.color.holo_green_light);
refreshView.setOnRefreshListener(this);
View v = refreshView.findViewById(R.id.scrollview);
rndNum = (TextView) v.findViewById(R.id.rndNum);
return refreshView;
}
@Override
public void onRefresh() {
refreshView.setRefreshing(true);
( new Handler()).postDelayed(new Runnable() {
@Override
public void run() {
refreshView.setRefreshing(false);
double f = Math.random();
rndNum.setText(String.valueOf(f));
}
}, );
}
}

4.常用方法

  • public void setEnabled(boolean enabled)  是否关闭下拉刷新功能(关闭下拉手势和刷新界面)。
  • public void setRefreshing(boolean refreshing) ; true显示刷新界面,false关闭刷新界面。
  • public void setColorSchemeResources(@ColorRes int... colorResIds) 设置刷新条的颜色。
  • ...
上一篇:【LDAP安装】在已编译安装的PHP环境下安装LDAP模块


下一篇:在DCOM 中不存在WORD、EXCEL等OFFICE组件