在ViewFlipper中添加Button和ListView后,如何在触摸Button,LitView的item后,实现左右滑动呢?Textview和ImageView是可以左右滑动的,但Button和ListView就不知道如何滑动,现在是在ListView上手势滑动变成了单击,要么就没反应。
Java codeimport android.app.Activity;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.view.GestureDetector.OnGestureListener;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.ViewFlipper;
public class ViewFlipperTest extends Activity implements OnGestureListener {
private Button previous, next;
private ViewFlipper flipper;
private GestureDetector detector;
private Animation slideLeftIn;
private Animation slideLeftOut;
private Animation slideRightIn;
private Animation slideRightOut;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
initiaView();
Button btn = (Button)addButtonByText("Start");
btn.setLongClickable(true);
flipper.addView(btn, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
flipper.addView(addTextByText("Start..."));
flipper.addView(addImageById(R.drawable.food_gutouwang));
flipper.addView(addImageById(R.drawable.food_foshanliaoli));
flipper.addView(addTextByText(">>>End"));
flipper.addView(addButtonByText("End"),
new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
}
/**
* Initialize view
*/
private void initiaView(){
detector = new GestureDetector(this);
previous = (Button) findViewById(R.id.btnPrevious);
next = (Button) findViewById(R.id.btnNext);
flipper = (ViewFlipper) findViewById(R.id.flipper);
slideLeftIn = AnimationUtils.loadAnimation(this, R.anim.slide_left_in);
slideLeftOut = AnimationUtils.loadAnimation(this, R.anim.slide_left_out);
slideRightIn = AnimationUtils.loadAnimation(this, R.anim.slide_right_in);
slideRightOut = AnimationUtils.loadAnimation(this, R.anim.slide_right_out);
flipper.setInAnimation(slideLeftIn);
flipper.setOutAnimation(slideLeftOut);
previous.setOnClickListener(listener);
next.setOnClickListener(listener);
}
private OnClickListener listener = new OnClickListener(){
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId()){
case R.id.btnPrevious:
flipper.setInAnimation(slideRightIn);
flipper.setOutAnimation(slideRightOut);
flipper.showPrevious();
break;
case R.id.btnNext:
flipper.setInAnimation(slideLeftIn);
flipper.setOutAnimation(slideLeftOut);
flipper.showNext();
break;
}
}
};
public View addTextByText(String text){
TextView tv = new TextView(this);
tv.setText(text);
tv.setGravity(1);
return tv;
}
public View addImageById(int id){
ImageView iv = new ImageView(this);
iv.setImageResource(id);
return iv;
}
public View addButtonByText(String text){
Button btn = new Button(this);
btn.setText(text);
return btn;
}
@Override
public boolean onTouchEvent(MotionEvent event) {
return this.detector.onTouchEvent(event);
}
@Override
public boolean onDown(MotionEvent e) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
if (e1.getX() - e2.getX() > 120) { // flip from right to left
flipper.setInAnimation(slideLeftIn);
flipper.setOutAnimation(slideLeftOut);
this.flipper.showNext();
return true;
} else if (e1.getX() - e2.getX() < -120) { // flip from left to right
flipper.setInAnimation(slideRightIn);
flipper.setOutAnimation(slideRightOut);
this.flipper.showPrevious();
return true;
}
return false;
}
@Override
public void onLongPress(MotionEvent e) {