SwipeRefreshLayout是谷歌官方的下拉刷新控件,代码在V4包中。java
使用SwipeRefreshLayout有几个要注意的:android
SwipeRefreshLayout和ScrollView同样只能有一个字控件。ide
setOnRefreshListener设置监听刷新。spa
setProgressBackgroundColor设置刷新时圆形进度条的背景色code
setColorSchemeResources设置刷新时进度条颜色xml
setRefreshing设置刷新状态事件
setSize设置大小,如今只有SwipeRefreshLayout.DEFAULT,SwipeRefreshLayout.LARGEip
若是在SwipeRefreshLayout的子控件中嵌套了其余可滚动控件例如get
<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事件
it
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; } };