2016-03-21 115 views
0

我在里面使用带有TouchImageView的ViewPager,它的效果很好,(我在很多Android应用程序中使用过这个解决方案)。 但是我有一个应用程序,在同一个屏幕上有很多其他控件,所以它们都在scrollview控件中。 在这种情况下,我看到滚动视图播放不好,我无法在缩放的图像内平移。当我用手指向上或向下平移时,整个页面将滚动而不是图像平移。如何在滚动视图中缩放/平移图像

所以这里是我想要做的...... 在TouchImageView中,我检测到Zoom Begin和Zoom End,并创建了一个接口来对我的Activity onZoomBegin()和onZoomEnd()方法进行回调。 在onZoomBegin()方法中,我想禁用scrollview来响应任何触摸事件,并在onZoomEnd()中重新启用它。 到目前为止,这里是我试图在其中没有正在使用的onZoomBegin()方法做的事情....

scrollView.setEnabled(false); 
scrollView.requestDisallowInterceptTouchEvent(true); 

也是我试图回答一个类似的问题,这是接管onTouchListener像例如:

scrollView.setOnTouchListener(new View.OnTouchListener() { 
     @Override 
     public boolean onTouch(View v, MotionEvent event) { 
      return true; 
     } 
    }); 

这不会阻止滚动了滚动,但滚动视图仍拦截触摸事件导致的图像仍然不会平移向上或向下。

我试过检查nestedScrollingEnabled在布局设计师,没有喜悦.... 我只是想知道有没有办法完全禁用scrollview,然后重新启用它响应触摸事件?

回答

0

我在另一个问题的某处发现了这个答案,但当我意识到这是我的问题的解决方案(回答我的问题)后,我失去了参考。我会继续寻找,所以我可以编辑这篇文章,以便在信贷到期时给予信贷。

public class CustomScrollView extends ScrollView { 

// true if we can scroll the ScrollView 
// false if we cannot scroll 
private boolean scrollable = true; 

public CustomScrollView(Context context) { 
    super(context); 
} 

public CustomScrollView(Context context, AttributeSet attrs) { 
    super(context, attrs); 
} 

public CustomScrollView(Context context, AttributeSet attrs, int defStyleAttr) { 
    super(context, attrs, defStyleAttr); 
} 


public void setScrollingEnabled(boolean scrollable) { 
    this.scrollable = scrollable; 
} 

public boolean isScrollable() { 
    return scrollable; 
} 

@Override 
public boolean onTouchEvent(MotionEvent ev) { 
    switch (ev.getAction()) { 
     case MotionEvent.ACTION_DOWN: 
      // if we can scroll pass the event to the superclass 
      if (scrollable) 
       return super.onTouchEvent(ev); 
      // only continue to handle the touch event if scrolling enabled 
      return false; // scrollable is always false at this point 
     default: 
      return super.onTouchEvent(ev); 
    } 
} 

@Override 
public boolean onInterceptTouchEvent(MotionEvent ev) { 
    // Don't do anything with intercepted touch events if 
    // we are not scrollable 
    if (!scrollable) 
     return false; 
    else 
     return super.onInterceptTouchEvent(ev); 
} 

}

这一部分,我只是想出了为自己....在TouchImageView我添加了一个回调接口时,变焦开始和结束被称为所以在我的活动我只是不得不做这个:

private class OnZoomListener implements TouchImageView.OnZoomListener { 
    @Override 
    public void onZoomBegin() { 
     isZoomed = true; 
     scrollView.scrollTo(0, 0); 
     scrollView.setScrollingEnabled(false); // <-- disables scrollview 
     hideImageControls(); 
     sizeViewPager(); 
    } 

    @Override 
    public void onZoomEnd() { 
     scrollView.setScrollingEnabled(true); // <-- enables scrollview 
     showImageControls(); 
     isZoomed = false; 
    } 
} 
+0

你可以发布你的TouchImageView.java类吗?因为我面临同样的问题。 – Philliphe