我创建了一个这样的垂直viewpager:
/**
* Swaps the X and Y coordinates of your touch event.
*/
private MotionEvent swapXY(MotionEvent ev) {
float width = getWidth();
float height = getHeight();
float newX = (ev.getY() / height) * width;
float newY = (ev.getX() / width) * height;
ev.setLocation(newX, newY);
return ev;
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev){
boolean intercepted = super.onInterceptTouchEvent(swapXY(ev));
swapXY(ev);
return intercepted;
}
@Override
public boolean onTouchEvent(MotionEvent ev) {
return super.onTouchEvent(swapXY(ev));
}
并在第二个片段中创建一个包含4个片段的公共viewpager,到目前为止一切顺利,没有冲突。 好吧,我将滚动视图放入其中的3个,这次当我尝试滚动到我的垂直viewpager的顶部片段时,它不能在我的第一次触摸滚动视图中工作(如果我触摸此片段的其他部分它不会发生。)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/transparent"
android:orientation="horizontal"
android:layout_marginBottom="120dp"
android:paddingLeft="5dp"
android:paddingRight="5dp" >
<com.nangua.xiaomanjflc.widget.SmartScrollView
android:id="@+id/sv"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/ll_project_profile"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white"
android:orientation="vertical"
android:paddingBottom="@dimen/page_bottom"
android:paddingTop="@dimen/page_small"
android:visibility="visible" >
<TextView
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="@dimen/page_line"
android:lineSpacingMultiplier="1.2"
android:textColor="@color/font_gray"
android:textSize="@dimen/font_hint" />
</LinearLayout>
</com.nangua.xiaomanjflc.widget.SmartScrollView>
</LinearLayout>
我想要的是当我滚动顶部的滚动视图时,它可以像我的垂直viewpager一样轻柔地移动。一旦我重写了我的垂直viewpager的onTouchEvent,常见的viewpager和scrollview发现垂直的MotionEvent动作被捕获了普通的viewpager已经消失,因此垂直viewpager一无所获。 现在,这是我的错误解决方案,当我滚动到顶部时,我在scrollview中放置一个回调,外部句柄将注意到我的垂直viewpager,并通过调用setCurrentItem(0)方法滚动到顶部片段。但实际上它看起来很奇怪。 那么在重写onInterceptTouchEvent,onTouchEvent或冲突的3个部分的一些其他有用方法时,我该怎么办?
scrollview的代码:
public SmartScrollView(Context context, AttributeSet attrs) {
super(context, attrs);
}
private ISmartScrollChangedListener mSmartScrollChangedListener;
public interface ISmartScrollChangedListener {
void onScrolledToBottom();
void onScrolledToTop();
}
public void setScanScrollChangedListener(
ISmartScrollChangedListener smartScrollChangedListener) {
mSmartScrollChangedListener = smartScrollChangedListener;
}
@Override
protected void onOverScrolled(int scrollX, int scrollY, boolean clampedX,
boolean clampedY) {
super.onOverScrolled(scrollX, scrollY, clampedX, clampedY);
if (scrollY == 0) {
isScrolledToTop = clampedY;
isScrolledToBottom = false;
}
else {
isScrolledToTop = false;
isScrolledToBottom = clampedY;
}
notifyScrollChangedListeners();
}
@Override
protected void onScrollChanged(int l, int t, int oldl, int oldt) {
super.onScrollChanged(l, t, oldl, oldt);
if (getScrollY() == 0) {
isScrolledToTop = true;
isScrolledToBottom = false;
}
else if (getScrollY() + getHeight() - getPaddingTop()
- getPaddingBottom() == getChildAt(0).getHeight()) {
isScrolledToBottom = true;
isScrolledToTop = false;
}
else {
isScrolledToTop = false;
isScrolledToBottom = false;
}
notifyScrollChangedListeners();
}
}
private void notifyScrollChangedListeners() {
if (isScrolledToTop) {
if (mSmartScrollChangedListener != null) {
mSmartScrollChangedListener.onScrolledToTop();
}
}
else if (isScrolledToBottom) {
if (mSmartScrollChangedListener != null) {
mSmartScrollChangedListener.onScrolledToBottom();
}
}
}
public boolean isScrolledToTop() {
return isScrolledToTop;
}
public boolean isScrolledToBottom() {
return isScrolledToBottom;
}
public boolean canScroll() {
View child = getChildAt(0);
if (child != null) {
int childHeight = child.getHeight();
return getHeight() < childHeight;
}
return false;
}