2016-03-21 116 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

相關問題