开发者

Android ViewFlipper Touch Event

In my Android project I used ViewFlipper for flipping child. For this I used customized flipper. In this I override onTouchEvent(). Also flipper contain images in each child. But when I try to flip flipper it not get touch event , but when I override ViewGroup method onInterceptTouchEvent() & return true, then flipping is done but due to this my OnclikEvent() on each image is not get. I am not getting where is problem. [But when I return false from onInterceptTouchEvent() then click event get but touch event not get.] Here I attach my code. Please give me right way to resolve this issue. Thank You

Code ViewFlipper:

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Toast;
import android.widget.ViewFlipper;
public class MyViewFlipper extends ViewFlipper {

static final String logTag = "ViewFlipper";
static final int MIN_DISTANCE = 30;
private float downX, downY, upX, upY;
Animation slideLeftIn;
Animation slideLeftOut;
Animation slideRightIn;
Animation slideRightOut;
Context context;
ViewFlipper viewFlipper;

public MyViewFlipper(Context context) {
    super(context);
    viewFlipper=this;
     this.context=context;
     System.out.println("I am in MyFlipper() counstructor...");
}

public MyViewFlipper(Context context, AttributeSet attrs) {
    super(context, attrs);
    this.context=context;
     viewFlipper=this;
     System.out.println("I am in MyFlipper() counstructor...");
     slideLeftIn = AnimationUtils.loadAnimation(context, R.anim.slide_left_in);
     slideLeftOut = AnimationUtils.loadAnimation(context, R.anim.slide_left_out);
     slideRightIn = AnimationUtils.loadAnimation(context, R.anim.slide_right_in);
     slideRightOut = AnimationUtils.loadAnimation(context, R.anim.slide_right_out);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
     switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN: {
            downX = event.getX();
            downY = event.getY();
            return true;
        }
        case MotionEvent.ACTION_UP: {
            upX = event.getX();
            upY = event.getY();
            float deltaX = downX - upX;
            float deltaY = downY - upY;
            // swipe horizontal?
            if (Math.abs(deltaX) > MIN_DISTANCE) {
                // left or right
                if (deltaX < 0) {
                    this.onLeftToRightSwipe();
                    return true;
                }
                if (deltaX > 0) {
                    this.onRightToLeftSwipe();
                    return true;
                }
            } else {
                if(Math.abs(deltaX)<15){
                    onClickEvent();
                }
                //Log.i(logTag, "Swipe was only " + Math.abs(deltaX)
                //        + " long, need at least " + MIN_DISTANCE);
            }
            // swipe vertical?
            if (Math.abs(deltaY) > MIN_DISTANCE) {
                // top or down
                if (deltaY < 0) {
                    this.onTopToBottomSwipe();
                    return true;
                }
                if (deltaY > 0) {
                    this.onBottomToTopSwipe();
                    return true;
                }
            } else {
                Log.i(logTag, "Swipe was only " + Math.abs(deltaX)
                        + " long, need at least " + MIN_DISTANCE);
            }
            return true;
        }
        }
        return false;
}

    public void onRightToLeftSwipe() {

    viewFlipper.setInAnimation(slideLeftIn);
    viewFlipper.setOutAnimation(slideLeftOut);
    viewFlipper.showNext();
}
public void onLeftToRightSwipe() {

    viewFlipper.setInAnimation(slideRightIn);
    viewFlipper.setOutAnimation(slideRightOut); 
    viewFlipper.showPrevious();
}
public void onTopToBottomSwipe() {
    Log.i(logTag, "onTopToBottomSwipe!");
    // activity.doSomething();
}
public void onBottomToTopSwipe() {
    Log.i(logTag, "onBottomToTopSwipe!");
    // activity.doSomething();
}

public void onClickEvent(){
    Toast.makeText(context, "Click",Toast.LENGTH_SHORT);
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
    return true;//Here if true then Flipping done
                        //And  if false then click event done. 
}

}

Code Activity:

import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
public class ViewFlipperDemo extends Activity implements OnClickListener{
MyViewFlipper myFlipper;
LinearLayout mainLayout;
ImageView img1,img2,img3,img4,img5,img6,img7,img8,img9,img10;
LayoutInflater inflater;
TextView txt;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    myFlipper=(MyViewFlipper) findViewById(R.id.viewFlipper);
    txt=(TextView) findViewById(R.id.txt);
    txt.setOnClickListener(this);

    inflater=LayoutInflater.from(getApplicationContext());
    img1=(ImageView)inflater.inflate(R.layout.image, null);
    img2=(ImageView)inflater.inflate(R.layout.image, null);
    img3=(ImageView)inflater.inflate(R.layout.image, null);
    img4=(ImageView)inflater.inflate(R.layout.image, null);
    img5=(ImageView)inflater.inflate(R.layout.image, null);


    myFlipper.addView(img1);
    myFlipper.addView(img2);
    myFlipper.addView(img3);
    myFlipper.addView(img4);
    myFlipper.addView(img5);

    System.out.println("Count "+myFlipper.getChildCount());

    img1.setOnClickListener(this);
    img2.开发者_高级运维setOnClickListener(this);
    img3.setOnClickListener(this);
    img4.setOnClickListener(this);
    img5.setOnClickListener(this);
  }
    @Override
public void onClick(View v) {
        System.out.println("OnClik Image"); 
        txt.setText("Current Child Index : "+myFlipper.getDisplayedChild());
}

}

Code 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"
    android:id="@+id/mainLayout">
 <TextView android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:id="@+id/txt"
    android:background="#0000ff"/>   
 <Benchmark.ViewFlipper1.MyViewFlipper  
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:id="@+id/viewFlipper"
    android:background="#ff0000"
    ></Benchmark.ViewFlipper1.MyViewFlipper  > 
</LinearLayout>
Code image.xml
<?xml version="1.0" encoding="utf-8"?>
<ImageView
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:-Src="@drawable/big_image">
 </ImageView>


Remove the onInterceptTouchEvent() method in your MyViewFlipper class

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜