SwipeRefreshLayout是谷歌官方的下拉刷新控件,代码在V4包中。
使用SwipeRefreshLayout有几个要注意的:
SwipeRefreshLayout和ScrollView一样只能有一个字控件。
setOnRefreshListener设置监听刷新。
setProgressBackgroundColor设置刷新时圆形进度条的背景色
setColorSchemeResources设置刷新时进度条颜色
setRefreshing设置刷新状态
setSize设置大小,现在只有SwipeRefreshLayout.DEFAULT,SwipeRefreshLayout.LARGE
如果在SwipeRefreshLayout的子控件中嵌套了其他可滚动控件例如
<android.support.v4.widget.SwipeRefreshLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<ScrollView
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:id="@id/svCar"
android:fadingEdge="none" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</ScrollView>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
</android.support.v4.widget.SwipeRefreshLayout>
当ScrollView滚动到下方后,向下拉ScrollView不会滚动而会出现下拉刷新动作,这时需要将ScrollView设置onTouch事件
OnTouchListener onTouchListener=new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
case MotionEvent.ACTION_MOVE:
case MotionEvent.ACTION_UP:
if (svCar.getScrollY() > 0) {
srlCar.setEnabled(false);
} else {
srlCar.setEnabled(true);
}
break;
}
return false;
}
};