解决在android开发中ViewPager中Gallery无法滑动问题

时间:2024-01-10 22:28:02

我的是在viewpager中某个fragment中有gallery... 导致无法滑动,网上找到解决方法。 自定义Gallery.

代码:

import android.content.Context;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.GestureDetector.OnGestureListener;
import android.view.MotionEvent;
import android.widget.Gallery;

public class CustomGallery extends Gallery implements OnGestureListener {
	private ViewPager mPager;

	public ViewPager getmPager() {
		return mPager;
	}

	public void setmPager(ViewPager mPager) {
		this.mPager = mPager;
	}

	/**
	 * @param context
	 * @param attrs
	 */
	public CustomGallery(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
	}

	/**
	 * @param context
	 * @param attrs
	 *                     
	 */
	public CustomGallery(Context context, AttributeSet attrs) {
		super(context, attrs); // TODO Auto-generated constructor stub
	}

	@Override
	public boolean dispatchTouchEvent(MotionEvent ev) {
		// TODO Auto-generated method stub
		mPager.requestDisallowInterceptTouchEvent(true);
		return super.dispatchTouchEvent(ev);
	}

	@Override
	public boolean onInterceptTouchEvent(MotionEvent ev) {
		// TODO Auto-generated method stub
		mPager.requestDisallowInterceptTouchEvent(true);
		return super.onInterceptTouchEvent(ev);
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		// TODO Auto-generated method stub
		mPager.requestDisallowInterceptTouchEvent(true);
		return super.onTouchEvent(event);
	}
}