Jon Zangitu
Jon Zangitu

Reputation: 967

android onscroll gesture detector

In my android app I've got an Activity with a ViewFlipper. Inside the Viewflipper I've got 3 Linear Layouts.

 <ViewFlipper
    android:id="@+id/viewFlipper"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:layout_marginBottom="20dip"
            android:text="@string/publicas"
            android:textAppearance="?android:attr/textAppearanceLarge" />

        <ListView
            android:id="@+id/listViewDemandasPublicas"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:layout_marginBottom="20dip"
            android:text="@string/pendientes"
            android:textAppearance="?android:attr/textAppearanceLarge" />

        <ListView
            android:id="@+id/listViewDemandasPendientes"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:layout_marginBottom="20dip"
            android:text="@string/pendientes"
            android:textAppearance="?android:attr/textAppearanceLarge" />

        <ListView
            android:id="@+id/listViewDemandasFinalizadas"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </LinearLayout>

</ViewFlipper>

So I want to change the ViewFlippers containing when I scroll horizontaly. So I've got a gesture detector and a gesture listener implemented. It works fine even if I have a ListView inside (I've done in in another activity). My onScroll methos is the next one:

@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
 ViewFlipper vf = (ViewFlipper) getWindow().findViewById(R.id.viewFlipper);
    View view = null;
    if ((e1.getX() < e2.getX()) && (distanceX < -20)) { // dedo a la derecha
        Log.i("ListaDemandas", "Muevo a la derecha SCROLL");
        if (esPendientes) {
            esPendientes = false;
            esEntrada = true;
            verPublicas(view);
            vf.setInAnimation(AnimationUtils.loadAnimation(getParent(), R.anim.push_right_in));
            vf.setOutAnimation(AnimationUtils.loadAnimation(getParent(), R.anim.push_right_out));
            vf.showPrevious();
        } else if (esFinalizadas) {
            esFinalizadas = false;
            esPendientes = true;
            verPendientes(view);
            vf.setInAnimation(AnimationUtils.loadAnimation(getParent(), R.anim.push_right_in));
            vf.setOutAnimation(AnimationUtils.loadAnimation(getParent(), R.anim.push_right_out));
            vf.showPrevious();
        }
    } else if ((e1.getX() > e2.getX()) && (distanceX > 20)) { 
        Log.i("ListaDemandas", "Muevo a la izquierda SCROLL");
        if (esPendientes) {
            esPendientes = false;
            esFinalizadas = true;
            verFinalizadas(view);
            vf.setInAnimation(AnimationUtils.loadAnimation(getParent(), R.anim.push_left_in));
            vf.setOutAnimation(AnimationUtils.loadAnimation(getParent(), .anim.push_left_out));
            vf.showNext();
        } else if (esEntrada) {
            esEntrada = false;
            esPendientes = true;
            verPendientes(view);
            vf.setInAnimation(AnimationUtils.loadAnimation(getParent(), R.anim.push_left_in));
            vf.setOutAnimation(AnimationUtils.loadAnimation(getParent(), .anim.push_left_out));
            vf.showNext();
        }
    }
    return true;
}

My problem is that onScroll is being called (of course) each time a scroll distance is bigger than 20. So, if I make a long scroll, each time my scroll movement is bigger than 20 the method is being called. So if my Scroll have a distance of 50, the method is being called in 21, 22, 23....

How can I manage this to get the method being called only once? I've been reading the API and I've been trying controlling the MotionEvent times, but it isn't a good solution because I can make two fast scrolls. I've seen addBatch method too. This is my solution? Anybody knows how can I batch all calls in one? Any "artisanal" way?

Thank you

=============================================================================================================

EDIT:

I've solved it in an artisanal way. A boolean initializated with true newScroll. So when I make a Scroll I put it to false and onDown() I put it to true again. So:

  @Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
    ViewFlipper vf = (ViewFlipper) getWindow().findViewById(R.id.viewFlipper);
    if (scrollNuevo) {

        if ((e1.getX() < e2.getX()) && (distanceX < -20)) {                 
            Log.i("ListaDemandas", "Muevo a la derecha SCROLL");
            scrollNuevo = false; Log.i("ListaDemandas","ScrollNuevo=false");
            if (esPendientes) {
     ...

And the method onDown:

@Override
public boolean onDown(MotionEvent e) {
    scrollNuevo = true;Log.i("ListaDemandas","ScrollNuevo=true");
    return true;
}

Upvotes: 1

Views: 5077

Answers (1)

Jon Zangitu
Jon Zangitu

Reputation: 967

I've solved it in an artisanal way. A boolean initializated with true newScroll. So when I make a Scroll I put it to false and onDown() I put it to true again. So:

  @Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
    ViewFlipper vf = (ViewFlipper) getWindow().findViewById(R.id.viewFlipper);
    if (scrollNuevo) {

        if ((e1.getX() < e2.getX()) && (distanceX < -20)) {                 
            Log.i("ListaDemandas", "Muevo a la derecha SCROLL");
            scrollNuevo = false; Log.i("ListaDemandas","ScrollNuevo=false");
            if (esPendientes) {
     ...

And the method onDown:

@Override
public boolean onDown(MotionEvent e) {
scrollNuevo = true;Log.i("ListaDemandas","ScrollNuevo=true");
return true;
}

Upvotes: 1

Related Questions