解决swiperefreshlayout与scrollview的冲突问题
1.情况方案一:
在开发中,我们会用到新的控件swipeRefreshLayout,但是我们同时又要用到scrollView。我们知道swipeRefreshLayout和scrollView这两个控件的子控件都必须有且只有一个的,而且这两个控件都会检测用户滑动的动作,由于事件的分派的问题,就出现了我们所遇到的冲突问题。if (scrollView != null) {
scrollView.getViewTreeObserver().addOnScrollChangedListener(new ViewTreeObserver.OnScrollChangedListener() {
@Override
public void onScrollChanged() {
if (swipeRefreshLayout != null) {
swipeRefreshLayout.setEnabled(scrollView.getScrollY() == 0);
CommonLog.infoLog("init scrollView");
}
}
});
}
2.情况方案二:
当swipeRefreshLayout ScrollView 嵌套使用时,你先将ScrollView 向上滑动一部分,然后松手.然后再向下滑动,此时应该是先触发滑动事件,当ScrollView到顶部以后才触发刷新时间.但是此时会出现先触发swipeRefreshLayout的问题。
mScrollView.getViewTreeObserver().addOnScrollChangedListener(new ViewTreeObserver.OnScrollChangedListener() {
@Override
public void onScrollChanged() {
mSwipeRefreshLayout.setEnabled(mScrollView.getScrollY()==0);
}
});
3.情况方案三:
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;
}
};