gpt4 book ai didi

Android ViewFlipper 触摸事件

转载 作者:行者123 更新时间:2023-11-29 14:05:52 46 4
gpt4 key购买 nike

在我的 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/

46 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com