Friday, October 16, 2009

[android-developers] ViewFlipper with clickable items

Hi!

I am using the ViewFlipper to switch between different views by
implementing a swing (mouse down, move and release). This works great
with non-clickable items, e.g. a TextView.

But if the views are clickable items (such as buttons), the onTouch()
method of the ViewFlipper is not called. :-(
Does anyone know why this is so and how I can implement this "pseudo
gesture"?

I know that I could do this easily by using the 1.6 SDK and the new
gesture support, but my application is limited to 1.5.


Thanks in advance & regards

Marc Reichelt || http://www.marcreichelt.de/


PS: I developed a simple example so that you can see what I mean.

--------- Content of TestFlipper.java ---------
package com.example.test;

import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ViewFlipper;

public class TestFlipper extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

final ViewFlipper flipper = (ViewFlipper) findViewById
(R.id.flipper);
flipper.setOnTouchListener(new OnTouchListener() {

private float oldX;

@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
// save start X location
oldX = event.getX();
return true;

case MotionEvent.ACTION_UP:
// get old X location where the user started the move
float currentX = event.getX();

// do not consume event if it is not clear enough
if (Math.abs(currentX - oldX) < 25.0f)
return false;

// show previous view
if (oldX < currentX) {
flipper.showPrevious();
return true;
}

// show next view
if (oldX > currentX) {
flipper.showNext();
return true;
}
}

// do not consume this event by default
return false;
}
});

}
}
--------- End ---------

--------- Content of main.xml ---------
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/
android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>

<ViewFlipper android:layout_height="fill_parent"
android:layout_width="fill_parent" android:id="@+id/flipper">
<Button android:id="@+id/b1" android:layout_height="fill_parent"
android:layout_width="fill_parent" android:text="1"
android:textSize="100px" />
<Button android:id="@+id/b2" android:layout_height="fill_parent"
android:layout_width="fill_parent" android:text="2"
android:textSize="100px" />
<Button android:id="@+id/b3" android:layout_height="fill_parent"
android:layout_width="fill_parent" android:text="3"
android:textSize="100px" />
</ViewFlipper>

</LinearLayout>
--------- End ---------
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
-~----------~----~----~----~------~----~------~--~---

No comments:

Post a Comment