1.自定义VerticalSwipeRefreshLayout 继承SwipeRefreshLayout
public class VerticalSwipeRefreshLayout extends SwipeRefreshLayout {
private int scaleTouchSlop;
private float preX;
public VerticalSwipeRefreshLayout(Context context, AttributeSet attrs) {
super(context, attrs);
scaleTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
switch (ev.getAction()) {
case MotionEvent.ACTION_DOWN:
preX = ev.getX();
break;
case MotionEvent.ACTION_MOVE:
float moveX = ev.getX();
float instanceX = Math.abs(moveX - preX);
Log.i("refresh...","move: instanceX:" + instanceX + "=(moveX:" + moveX + " - preX:" + preX + ") , scaleTouchSlop:" + scaleTouchSlop);
// 容差值大概是24,再加上60
if(instanceX > scaleTouchSlop + 60){
return false;
}
break;
}
return super.onInterceptTouchEvent(ev);
}
}
2.xml布局文件
<cn.yt.itbt.client.view.VerticalSwipeRefreshLayout
android:id="@+id/swipeLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="@+id/dListView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:divider="@color/Cffffff"
android:fadingEdge="none"
android:visibility="visible"></ListView>
</cn.yt.itbt.client.view.VerticalSwipeRefreshLayout>
3.java代码使用
3.1设置下拉刷新监听器SwipeRefreshLayout.OnRefreshListener
3.2.关键代码
swipeLayout = (VerticalSwipeRefreshLayout) indexLayout.findViewById(R.id.swipeLayout);
swipeLayout.setOnRefreshListener(this);
swipeLayout.setColorScheme(android.R.color.holo_blue_bright,
android.R.color.holo_green_light,
android.R.color.holo_orange_light,
android.R.color.holo_red_light);
swipeLayout.setRefreshing(false);//初始状态禁用下拉刷新
@Override
public void onRefresh() {
getResult();
swipeLayout.setRefreshing(false);
}