koufa
koufa

Reputation: 113

Android Viewpager as Header in ParallaxHeader Example does not scroll as expected

I am trying to implement the ParallaxHeaderViewPager like here. But instead using a KenBurnsSupportView and an ImageView as a Header i have used a FrameLayout that contains a ViewPager. My problem is that if i try to use vertical scroll on the Header the Event gets consumed but nothing happens. I can only scroll horizontally on the Header(ViewPager) and the Page changes correctly. But this is not what i want to accomplish. I want if i scroll vertically on the Header the event to be recognized as a ListView scroll and when i scroll horizontally (on the Header) as a ViewPager scroll. The scrolling is functioning correctly on the area that is not used from the header. Namely under the header where i have 4 Listviews I can scroll both vertically and horizontally. The Layout I am currently using is like the below code snippet:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<android.support.v4.view.ViewPager
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

<FrameLayout
    android:id="@+id/header"
    android:layout_width="match_parent"
    android:layout_height="@dimen/header_height" >

    <FrameLayout
        android:id="@+id/news_pager_content_area"
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        android:background="@color/white"
         />

    <com.astuetz.PagerSlidingTabStrip
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="48dip"
        android:layout_gravity="bottom"
        android:background="@android:color/transparent" />
</FrameLayout>

I have tried to subclass both the Listviews and the Viewpager to intercept and handle touch Events but it seems that the default behavior documented in the Android Documentation is not applicable in my example. Thank you for your help.

Upvotes: 0

Views: 762

Answers (1)

koufa
koufa

Reputation: 113

I solved my question trying all the answers at similar topics on Stackoverflow and looking at the Android Documentation again. I created a custom FrameLayout for the root container of the above Layout file. I override the dispatchTouchEvent method.

public boolean dispatchTouchEvent(MotionEvent ev)
    {
        switch (ev.Action)
        {
            case MotionEventActions.Down:
                _xDistance = _yDistance = 0f;
                _lastX = ev.GetX();
                _lastY = ev.GetY();
                _startY = _lastY;
                _isFirstTime = true;
                _downEvent = MotionEvent.obtain(ev);
                break;
            case MotionEventActions.Move:
                float curX = ev.GetX();
                float curY = ev.GetY();
                _xDistance += Math.abs(curX - _lastX);
                _yDistance += Math.abs(curY - _lastY);
                _lastX = curX;
                _lastY = curY;
                float yDeltatTotal = curY - _startY;
                if (_yDistance > _xDistance && Math.abs(yDeltatTotal) > _touchSlop)
                {
                    if (_isFirstTime)
                    {
                        _isFirstTime = false;
                        return childView.dispatchTouchEvent(_downEvent);

                    }
                    return childView.dispatchTouchEvent(ev);
                }
        }
        return super.DispatchTouchEvent(ev);
    }

Where as ChildView I set the ViewPager with id = pager from the layout above. To avoid pointerindex out of range Exception I used the obtain method as specified by Dmitry Zaitsev in the first comment at this link.

Upvotes: 1

Related Questions