2

I have two view pager, one is nested in the other. To get the correct behaviour (swiping the inner view pager without changing the outer) I had to override the inner view pagers onTouchListener and put all my onTouch/onClick logic into it (got the idea from here).

Works all fine, but since I don't have a onClickListener anymore I lost my selector effect. When I put android:clickable="true" on the layout element I get my selector effect, but the view pagers behaviour is wrong again.

Is there any way to achieve the selector effect out of the onTouchListener?

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

                if (gestureDetector.onTouchEvent(event)) {
                    Log.d(DEBUG_LOG, "Single tap.");
                    return true;
                } else if(event.getAction() == MotionEvent.ACTION_DOWN && v instanceof ViewGroup) {
                    ((ViewGroup) v).requestDisallowInterceptTouchEvent(true);
                }

                return false;
            }
        });
Community
  • 1
  • 1
dabo248
  • 3,367
  • 4
  • 27
  • 37

1 Answers1

3

This solved it for me. Just added the following code to my OnTouchListener and replaced the card view with my inner view pagers current item:

// Since the host view actually supports clicks, you can return false
// from your touch listener and continue to receive events.
myTextView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent e) {
    // Convert to card view coordinates. Assumes the host view is
    // a direct child and the card view is not scrollable.
    float x = e.getX() + v.getLeft();
    float y = e.getY() + v.getTop();

    // Simulate motion on the card view.
    myCardView.drawableHotspotChanged(x, y);

    // Simulate pressed state on the card view.
    switch (e.getActionMasked()) {
        case MotionEvent.ACTION_DOWN:
            myCardView.setPressed(true);
            break;
        case MotionEvent.ACTION_UP:
        case MotionEvent.ACTION_CANCEL:
            myCardView.setPressed(false);
            break;
    }

    // Pass all events through to the host view.
    return false;
}
});
Community
  • 1
  • 1
dabo248
  • 3,367
  • 4
  • 27
  • 37