Search code examples
androidandroid-fragmentsandroid-viewpagerandroid-overscoll

Slide android fragment using other view


i have 2 fragments, and I need to scroll this fragments up and down by selecting and dragging some small view(not selecting any place in fragment, only if I drag this veiw). Similary, i have an OverScroll on first fragment, which scrolls photos with animation. I tried using VerticalViewPager for it, but it unsuitable for this task, because it conflicts with overscroll.


Solution

  • Use the VerticalViewPager, but override onInterceptTouchEvent and define a "hotspot" that will trigger the fragment flip. As an example to get you started, I've overridden onInterceptTouchEventin a custom ViewPager such that edge swipes still "flip" the fragments (horizontally, in my case), but that interior swipes still get passed onto the ViewFlipper views inside the fragments

    @Override
    public boolean onInterceptTouchEvent(MotionEvent event) {
    
                int screenW = getWidth();
                int screenH = getHeight();
                int screenWReducer = Math.round(screenW*0.5f);
    
                Rect rect = new Rect(
                        0 + screenWReducer,
                        0,
                        screenW - screenWReducer,
                        screenH
                        );
    
                if (rect.contains((int) event.getX(), (int) event.getY())) {
    
                    return false;//Don't intercept. Left ViewFlippers get touch.
    
                }
    
                return super.onInterceptTouchEvent(event); //Edges still allow page swiping.
    
    }