Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Wear WearableListView in a GridViewPager

I'm building an Android Wear application which I would like to present as a few pages that can be swiped across horizontally.

For this I am using a GridViewPager and associated FragmentGridPagerAdapter which is hardwired to use a single row. This is working fine for swiping horizontally between tiles, however in one of my fragments I want to use a WearableListView to allow the user to select between several actions. Unfortunately this does not work as it seems the GridViewPager is prevent any swipes getting to the WearableListView. Does anyone know if there is a way this can be done using the components described?

I have also tried using a standard ViewPager and this allowed the WearableListView to scroll fine, but the horizontal swiping then becomes flakey and you often need to swipe a few times to move the view pager.

like image 764
JamieH Avatar asked Jul 17 '14 13:07

JamieH


1 Answers

EDIT:
A while after posting my original answer below, I found the correct way to do this. Just set

    wearableListView.setGreedyTouchMode( true )

and the list view works perfectly inside a GridViewPager.

The original answer is still a good view to get scrolling working with a vertical scrolling GridViewPager nested inside a horizontal scrolling one.

ORIGINAL ANSWER:
More or less the same thing as the accepted answer can be implemented more succinctly by extending the GridViewPager like this:

public class HorizontalListPager extends GridViewPager {

    private final GestureDetector mGestureDetector;

    public HorizontalListPager( Context context, AttributeSet attrs ) {
        super( context, attrs );
        mGestureDetector = new GestureDetector( context, new HScrollDetector() );
    }

    @Override
    public boolean onInterceptTouchEvent( MotionEvent ev ) {
        // If we have more horizontal than vertical scrolling, intercept the event,
        // otherwise let the child handle it
        return super.onInterceptTouchEvent( ev ) && mGestureDetector.onTouchEvent( ev );
    }

    class HScrollDetector extends GestureDetector.SimpleOnGestureListener {

        @Override
        public boolean onScroll( MotionEvent e1, MotionEvent e2, float distanceX, float distanceY ) {
            // Returns true if scrolling horizontally
            return ( Math.abs( distanceX ) > Math.abs( distanceY ) );
        }
    }
}
like image 83
Morne Avatar answered Oct 19 '22 11:10

Morne