1

this questionという回答と同様のものを使用して、ユーザーが特定のアイテムをスワイプしたときのViewPagerスワイプアクションを無効にしようとしました。問題のビューはMPAndroidChartライブラリのスクロール可能なチャートですので、当然ビューポケベルがチャートのスクロールを妨げないようにしたいと思います。特定のビューをタッチ/ドラッグするときにビューページを無効にする

私が抱えている問題は、onTouchListenerが必要なビューで呼び出される前に、ViewPagerに「onInterceptTouch」が呼び出されることが多いということです。ビューは/押されていない押されたときに、このコードセグメントで

、Iは記録だ:このコードセグメントで

private long lastDown; 

private long lastUp; 

... 

public void foo(){ 

    barChart.setOnTouchListener(new View.OnTouchListener() { 
     @Override 
     public boolean onTouch(View v, MotionEvent event) { 

     System.out.println("AAA"); 

     if(event.getAction() == MotionEvent.ACTION_DOWN){ 
      lastDown = System.currentTimeMillis(); 
     }else if(event.getAction() == MotionEvent.ACTION_UP){ 
      lastUp = System.currentTimeMillis(); 
     } 

    return false; 
     } 
    }); 
} 

ビューが選択されている場合、私が決定:

public boolean isGraphTouched(){ 
    return lastDown > lastUp; 
    } 

このコードセグメントでは、onInterceptTouchEventメソッドをオーバーライドしています。

@Override 
    public boolean onInterceptTouchEvent(MotionEvent ev) { 
    System.out.println("BBB"); 
    return isGraphSelected() ? super.onInterceptTouchEvent(ev) : false; 
    } 

プリントラインのTEは、onInterceptTouchEvent方法は

Println

私はこれを歩き回るの考えることができる唯一の方法は、グラフは、運動の座標に存在するかどうかを確認する方法を作ることです...の前に呼ばれていますイベント(もしこれが可能なのかどうかはわかりませんが)を使って、ページャがスワイプ可能かどうかを判断してください。

答えて

0

別の投稿のthis answerを組み込むことで、私の問題を解決することができました。このコードでは、指定されたビューの座標を取得し、それらの座標を自分の座標と比較することができます。私CustomViewPagerクラスの内部

は、私は上記のものを実装:

private boolean isPointInsideView(float x, float y, View view) { 
    int location[] = new int[2]; 
    view.getLocationOnScreen(location); 
    int viewX = location[0]; 
    int viewY = location[1]; 

    return ((x > viewX && x < (viewX + view.getWidth())) && (y > viewY && y < (viewY + view.getHeight()))); 
    } 

私はその後、条件のいくつかをチェックし、ページャが必要がある場合にはfalse/trueを返すブール値を返すメソッドを、持っていましたスワイプすることができ:

public boolean canSwipe(float x, float y) { 
    boolean canSwipe = true; 
    if (launchActivity.isReady(MainScreenPagerAdapter.STAT_PAGE)) { 
     FragmentStatistics fragmentStatistics = (FragmentStatistics) launchActivity.getPageAdapter().instantiateItem(this, MainScreenPagerAdapter.STAT_PAGE); 
     View chart = fragmentStatistics.getView().findViewById(R.id.chart); 
     canSwipe = !isPointInsideView(x, y, chart) || !fragmentStatistics.isGraphPannable(); 
    } 
    return canSwipe; 
    } 

そして、もちろん、私はそうのようなonInterceptTouchEventを上書き:

@Override 
    public boolean onInterceptTouchEvent(MotionEvent ev) { 
    return canSwipe(ev.getX(), ev.getY()) ? super.onInterceptTouchEvent(ev) : false; 
    } 

これで、Pagerが全く干渉しないようにグラフを完全にパンすることができます。

全CustomViewPagerコード:

public class CustomViewPager extends ViewPager { 

    /** Reference to the launch activity */ 
    private LaunchActivity launchActivity; 

    /** 
    * Constructor to call the super constructor 
    * 
    * @param context The application context 
    * @param attrs The attributes 
    */ 
    public CustomViewPager(Context context, AttributeSet attrs) { 
    super(context, attrs); 
    } 

    /** 
    * Sets object reference for the {@code launchActivity} 
    * 
    * @param launchActivity The LaunchActivity to be set 
    */ 
    public void set(LaunchActivity launchActivity) { 
    this.launchActivity = launchActivity; 
    } 

    /** 
    * Determines if the pager can be swiped based off the x and y inputs provided, as well as if the 
    * barchart can be panned or not. 
    * 
    * @param x The x coordinate to check 
    * @param y The y coordinate to check 
    * @return True if the ViewPager will continue with normal swiping action. 
    */ 
    public boolean canSwipe(float x, float y) { 
    boolean canSwipe = true; 
    if (launchActivity.isReady(MainScreenPagerAdapter.STAT_PAGE)) { 
     FragmentStatistics fragmentStatistics = (FragmentStatistics) launchActivity.getPageAdapter().instantiateItem(this, MainScreenPagerAdapter.STAT_PAGE); 
     View chart = fragmentStatistics.getView().findViewById(R.id.chart); 
     canSwipe = !isPointInsideView(x, y, chart) || !fragmentStatistics.isGraphPannable(); 
    } 
    return canSwipe; 
    } 

    /** 
    * Takes x and y coordinates and compares them to the coordinates of the passed view. Returns true if the passed coordinates 
    * are within the range of the {@code view} 
    * 
    * @param x The x coordinate to compare 
    * @param y The y coordinate to compare 
    * @param view The view to check the coordinates of 
    * @return True if the x and y coordinates match that of the view 
    */ 
    private boolean isPointInsideView(float x, float y, View view) { 
    int location[] = new int[2]; 
    view.getLocationOnScreen(location); 
    int viewX = location[0]; 
    int viewY = location[1]; 

    // point is inside view bounds 
    return ((x > viewX && x < (viewX + view.getWidth())) && (y > viewY && y < (viewY + view.getHeight()))); 
    } 

    /** 
    * Override of the onInterceptTouchEvent which allows swiping to be disabled when chart is selected 
    * 
    * @param ev The MotionEvent object 
    * @return Call to super if true, otherwise returns false 
    */ 
    @Override 
    public boolean onInterceptTouchEvent(MotionEvent ev) { 
    return canSwipe(ev.getX(), ev.getY()) ? super.onInterceptTouchEvent(ev) : false; 
    } 

} 
関連する問題