2

I have an Activity with a SlidingPaneLayout, and inside there are two fragments: a ListView on the left, and a MapFragment on the right. How is it possible to intercept the touch event generated so that the user can move the map without close the panel?

The only area that I would like to use to close/open the right panel is the first fourth. On the right of that area I would like to move the map.

Thanks

EDIT2:

Ok, now I figured out how to properly subclass SlidingPaneLayout, now the problem is how to capture correctly the touch event:

@Override
public boolean onTouchEvent(MotionEvent event) {
    if (event.getAction() == MotionEvent.ACTION_DOWN
            && event.getX() > (getWidth() / 6)) {
        return false;
    }
    return super.onTouchEvent(event);
}

With this code I'm not able to slide the map, it remains fixed. the problem is that I want to intercept the touch ONLY when the right panel is selected (in other words, only when map is displayed).

Lorenzo Barbagli
  • 1,241
  • 2
  • 16
  • 34

2 Answers2

2

SlidingPaneLayout have it's own touch listener, so when you reset it by calling setOnTouchListener (which is a method from the super class View) you are loosing all the onTouch behaviour specific to a SlidingPaneLayout.

-------------------------------

Here is a try : make your own SlidingPaneLayout :
the constructor should be this way in order to use your view in an xml layout

public class MySlidingPaneLayout extends SlidingPaneLayout{

    public MySlidingPaneLayout(Context context, AttributeSet attrs) {
        super(context, attrs);

    }

    @Override
    public boolean onTouchEvent(MotionEvent event){
        if (event.getX() < widthPIX / 6) {
             return super.onTouchEvent(event);// here it works as a normal SlidingPaneLayout
        }
        return false; // here it returns false so that another event's listener should be called, in your case the MapFragment listener
    }
}

and in your code add MySlidingPaneLayout instead

  • try it and tell me what you got –  Sep 21 '13 at 13:57
  • Yes you are right but the line super.onTouchEvent(event) is wrong because super refers to class Object (it gives me compile error: "The method onTouchEvent(MotionEvent) is undefined for the type Object"). Probably I have to extend SlidingPaneLayout and then override the method onInterceptTouchEvent (I tried this a few time ago with a ViewPager and it worked) but now I'm not able to create a correct subclass of SlidingPaneLayout because it gives me different exceptions.. – Lorenzo Barbagli Sep 21 '13 at 14:01
  • oh am sorry i thought you were making your own SlidingPaneLayout by extending the SlidingPaneLayout class, i edited the answer –  Sep 21 '13 at 14:10
  • Yeah, I tried that, but when I write the subclass I got this error: "Implicit super constructor SlidingPaneLayout() is undefined for default constructor. Must define an explicit constructor". And if a define a constructor, it gives me exceptions at runtime – Lorenzo Barbagli Sep 21 '13 at 14:24
  • will make another edit now to solve this simple problem of constructor –  Sep 21 '13 at 14:34
2

I finally solved the problem: simply override this method and control if the SlidingPaneLayout is closed or open (in my case I have a boolean field value "open")

@Override
public boolean onInterceptTouchEvent(MotionEvent event) {
    if (!homeActivity.open && event.getX() > (getWidth() / 5)) {
            return false;
        }
    return super.onInterceptTouchEvent(event);
}
Lorenzo Barbagli
  • 1,241
  • 2
  • 16
  • 34