Android ViewFlipper 触摸事件

标签 android

在我的 Android 项目中,我使用 ViewFlipper 来翻转 child 。为此,我使用了定制的脚蹼。在这里,我重写了 onTouchEvent()。 鳍状肢也包含每个 child 的图像。但是当我尝试翻转 flipper 时它没有得到触摸事件,但是当我重写 ViewGroup 方法 onInterceptTouchEvent() 并返回 true 时,翻转完成但由于这个我的每张图像上的 OnclikEvent() 没有得到。 我不知道问题出在哪里。 [但是,当我从 onInterceptTouchEvent() 返回 false 时,单击事件获取但触摸事件未获取。] 在这里我附上我的代码。请给我正确的方法来解决这个问题。 谢谢你

代码 View 翻转器:

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

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

代码 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 ViewFlipper 触摸事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7088874/

相关文章:

java - ListView 元素背景 - resource mutable()

android - setOnItemClickListener 在 eclipse 中给出错误

java - 广播接收器泄露

java - MainActivity 不是抽象的,不会重写 Listener 中的抽象方法 onPageStarted(String,Bitmap)

具有很长 URL 的 Android HttpGet 请求

Android:将文件保存到 SD 卡?

android - 如何防止屏幕自动旋转但防止 Activity 旋转

java - adapter.notifyDataSetChanged 不刷新回收器 View

java - 根据随机值获取数组元素

java - 为什么我应该将 NIO 用于 TCP 多人游戏而不是简单的套接字 (IO) - 或 : where is the block?