0

I have following layout

<ViewSwitcher>
  <LinearLayout>
    <ListView />
  </LinearLayout>

  <LinearLayout>
    <ListView />
  </LinearLayout>
</ViewSwitcher>

But when I switch ViewSwitcher(by onTouchListener) the onItemClickListener of ListView in my layout responds too. I do not need that on switching. How to fix it?

This is my switcher class:

public class MMGViewSwitcher extends ViewSwitcher{
private float startX = 0;
private float startY = 0;
private float endX = 0;
private float endY = 0;


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

}

@Override
public boolean onTouchEvent(MotionEvent event)
{
    String ActionString="";
    switch(event.getActionMasked())
    {
        case MotionEvent.ACTION_DOWN:
            startX = event.getX();
            startY = event.getY();
            ActionString="ACTION_DOWN";
            break;
        case MotionEvent.ACTION_MOVE: 
            ActionString="ACTION_MOVE";
            break;
        case MotionEvent.EDGE_LEFT:
        this.showNext();
            break;
        case MotionEvent.EDGE_RIGHT:
        this.showPrevious();
            break;
        case MotionEvent.ACTION_UP: 
            endX = event.getX();
            endY = event.getY();
            if((startX > endX) && Math.abs(startX-endX)>30){
                this.showNext();
            }
            if((startX < endX) && Math.abs(startX-endX)>30){
                this.showPrevious();
            }
            ActionString="ACTION_UP";
        break;
    }
    return true;
}


@Override
public boolean onInterceptTouchEvent(MotionEvent ev)
{
    onTouchEvent(ev);
    return false;
}

}

Pavel Shysh
  • 196
  • 2
  • 13

1 Answers1

0

First: Be more patient than an hour before you comment on your own question asking again for an answer (not welcome in the community).

Your issue is that you use two listeners that interfere each other. I don't know on which rule your OnTouchListener flips the view, but you should replace it with a GestureDetector and a SimpleGestureListener. There you can implement a swipe gesture detection to flip your view.

WarrenFaith
  • 57,492
  • 25
  • 134
  • 150
  • I create my own view extended viewSwitcher and override onTouch event. that work is good, but when i flip the view - onClickListener responds together. as a result a have flip and onclick simultaneously. Its wrong for me. May I disable Onclick's of childrens from my switcher? But why? – Pavel Shysh Feb 20 '12 at 13:19
  • Your gesture detector should work on the viewswitcher, not on the children. If you have problems, ask a new question here. – WarrenFaith Feb 20 '12 at 14:55
  • hello. I refresh my question, please look it. It's work good, switcher has 2 layouts with tabs. but when I moving on ListItem which have onClick listener - both listeners responds. I need to response only of OnTouch when i do not click on the listItem. Maybe you have answer why I may disable onClick at the time of OnTouch or good example with GestureDetector for my case? – Pavel Shysh Feb 20 '12 at 16:08
  • @Maxim maybe your have ansver? – Pavel Shysh Feb 20 '12 at 16:32
  • I still can't see a gesture detector... the way you do that is simply wrong. Sorry but without a gesture detector you will have this issue. I will not help if you didn't react on my advises... – WarrenFaith Feb 21 '12 at 12:13
  • i am tryin gesture detector from this http://stackoverflow.com/questions/4184382/how-to-implement-both-ontouch-and-also-onfling-in-a-same-listview . But it's don't work for me. Maybe you have example... – Pavel Shysh Feb 21 '12 at 12:19
  • As I said before: if you have trouble with the GestureDetector, make a new question. – WarrenFaith Feb 21 '12 at 12:20
  • tell me please where I must implement gesture detector: in activity or switcher class? – Pavel Shysh Feb 21 '12 at 12:22
  • You don't get what I am telling you, right? Make a new question for it! Can you read what is right above this input text when you type your next comment? **Please avoid extended discussions in comments.** – WarrenFaith Feb 21 '12 at 12:24
  • sorry. it's a new question: http://stackoverflow.com/questions/9378010/vievsvitcher-and-gestures-probl – Pavel Shysh Feb 21 '12 at 13:01