activitygroup中用了ViewFlipper控件,会被拦截onFling事件

activitygroup中用了ViewFlipper控件,会被拦截onFling事件,然后奇葩的是,我把onDown函数的 return false改成return true就好了

参考:http://jykenan.iteye.com/blog/1586483

这里说把onDown()方法return true;这样才能触发onFling事件。  



package com.zll.clostyle;

import java.util.List;
import java.util.Map;

import com.Bill_Ming.image.LoadImage;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ViewFlipper;
/**
 *  接收JSON中Url和专题名
 */
public class Activity_MainApp extends Activity implements OnGestureListener{

	private static final int FLING_MIN_DISTANCE = 50;
	final public String AppImg = "img";
	final public String AppName = "name";
	final public String AppLink = "link";
	final public String AppPosition = "AppPosition";
	final public String AppList = "AppList";
	
	private List<Map<String, Object>> AppPhotoList;		// 存储专题信息
	private GridView gridView;
	private Button login = null;
	private ViewFlipper ad_viewfilpper;
	private GestureDetector ad_detector;
	
	private void startIntent(int position){
		Intent intent = new Intent(Activity_MainApp.this, Activity_MainItemApp.class);
		intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
		intent.putExtra("position", position);	// 将单个专题 传到下一个Activity
		Log.i("positio", position+"");
		Window w = Activity_MainAppGroup.group_first.getLocalActivityManager().startActivity("Activity_MainItemApp", intent);
		Log.i("window", w+"");
		View view = w.getDecorView();
		Log.i("view!!", view+"");
		Activity_MainAppGroup.group_first.setContentView(view);
	}
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_tabhost_main_app);

		AppPhotoList = FirstActivity.AppPhotoList;

		gridView = (GridView) findViewById(R.id.AppView);
		gridView.setAdapter(new Activity_MainApp_GridViewAdapter(this, AppPhotoList));
		
		gridView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				// TODO Auto-generated method stub
				startIntent(arg2);
			}
		});
		 login = (Button)findViewById(R.id.photograph);
		  login.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent loginint = new Intent();
				loginint.setClass(Activity_MainApp.this, LoginClosy.class);
				startActivity(loginint);
			}
		});
		  
		// 注册一个GestureDetector
		ad_detector = new GestureDetector(this);
		ad_viewfilpper = (ViewFlipper) findViewById(R.id.advertisement);
		setAdImg();
		/**
		 * 自动滑动,从右向左
		 * 启动自动播放View,可以通过设置如下三个成员函数实现:
		 *  1、setAutoStart(true),设置是否自动播放功能,true为自动播放,false为不自动播放,开启自动播放设为true 
		 *  2、setFlipInterval(int milliseconds),设置View播放的时间间隔,如3000(3秒) 
		 *  3、startFlipping(),开始自动播放 停止自动播放View,设置成员函数如下: 
		 * 		1、 stopFlipping(),停止自动播放
		 * 		2、 setAutoStart(false),停止自动播放,设为false
		 */
		ad_viewfilpper.setAutoStart(true); // 设置自动播放功能(点击事件,前自动播放)
	
		ad_viewfilpper.setFlipInterval(3000);
		if (ad_viewfilpper.isAutoStart() && !ad_viewfilpper.isFlipping()) {
			ad_viewfilpper.startFlipping();
		}
	}

	public void setAdImg(){
		//广告图片
		Bitmap bitmap = null;
		Drawable drawable = null;
		LoadImage loadImage = new LoadImage();
		Log.e("setAdImg","广告啊啊");
		for(int i =0;i <FirstActivity.AdData.size(); i++ )
		{
			ImageView image1 = new ImageView(this);
			bitmap = loadImage.getBitmap(FirstActivity.AdData.get(i).get("img").toString());
			drawable = new BitmapDrawable(bitmap); 
			image1.setBackgroundDrawable(drawable);
			// 增加view
			ad_viewfilpper.addView(image1);
		}
	}
	
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		// TODO Auto-generated method stub
		ad_viewfilpper.stopFlipping(); // 点击事件后,停止自动播放
		ad_viewfilpper.setAutoStart(false);
		
	
		Log.e("onTouchEvent", "触摸事件");
		// 将触屏事务交给手势辨认类处理
		return this.ad_detector.onTouchEvent(event);
	}
	@Override
	public boolean onDown(MotionEvent e) {
		// TODO Auto-generated method stub
		Log.i("OnDown", "true");
		return true;   //return false;   原来是false时,滑动没用,改成true却行了
	}
	@Override
	public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
			float velocityY) {
			/**
		 * 用户按下触摸屏,快速移动后松开,就会触发这个事件。 在这段代码示例中,对手指滑动的距离进行了计算,如果滑动距离大于100像素,
		 * 就做切换动作,否则不做任何切换动作。
		 */
		Log.e("onfling","aaaaaaaaaaaaaaaaaaa" );
		if (e1.getX() - e2.getX() > FLING_MIN_DISTANCE) {
			// 设置View进入和退出的动画效果
			this.ad_viewfilpper.setInAnimation(AnimationUtils.loadAnimation(
					this, R.anim.left_in));
			this.ad_viewfilpper.setOutAnimation(AnimationUtils.loadAnimation(
					this, R.anim.left_out));
			this.ad_viewfilpper.showNext();
			Log.e("onfling","	if (e1.getX() - e2.getX() > FLING_MIN_DISTANCE)" );
			return true;
		}
		if (e1.getX() - e2.getX() < -FLING_MIN_DISTANCE) {
			this.ad_viewfilpper.setInAnimation(AnimationUtils.loadAnimation(
					this, R.anim.right_in));
			this.ad_viewfilpper.setOutAnimation(AnimationUtils.loadAnimation(
					this, R.anim.right_out));
			this.ad_viewfilpper.showPrevious();
			Log.e("onfling","	if (e1.getX() - e2.getX() < -FLING_MIN_DISTANCE)" );
			return true;
		}
		return false;
	}
	@Override
	public void onLongPress(MotionEvent e) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
			float distanceY) {
		// TODO Auto-generated method stub
		return false;
	}
	@Override
	public void onShowPress(MotionEvent e) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public boolean onSingleTapUp(MotionEvent e) {
		// TODO Auto-generated method stub
		return false;
	}

}


你可能感兴趣的:(image,String,float,button,login,distance)