在我的Android项目中,我使用ViewFlipper来翻转孩子。为此,我使用了定制的脚蹼。在此,我重写onTouchEvent()。
脚蹼还包含每个孩子的图像。但是,当我尝试翻转挡板时,它不会发生touch event,但是当我重写ViewGroup方法onInterceptTouchEvent()并返回true时,则完成了翻转,但是由于这个原因,我的每张图像上的OnclikEvent()都没有得到。
我不明白问题出在哪里。 [但是当我从onInterceptTouchEvent()返回false时,则单击获取事件,但未获取触摸事件。
在这里,我附上我的代码。请给我正确的方法来解决此问题。
谢谢

代码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.
}


}

代码活动:

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());
}


}

代码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>

最佳答案

在MyViewFlipper类中删除onInterceptTouchEvent()方法

关于android - Android ViewFlipper触摸事件,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/7088874/

10-10 17:56