解决方法,重新定义gallery,禁止触发pager的触摸事件
1 public class UserGallery extends Gallery implements OnGestureListener { 2 ViewPager mPager; 3 4 public ViewPager getmPager() { 5 return mPager; 6 } 7 8 public void setmPager(ViewPager mPager) { 9 this.mPager = mPager;10 }11 12 /**13 * @param context14 * @param attrs15 */16 public UserGallery(Context context) {17 super(context);18 // TODO Auto-generated constructor stub19 }20 21 /**22 * @param context23 * @param attrs24 */25 public UserGallery(Context context, AttributeSet attrs) {26 super(context, attrs);27 // TODO Auto-generated constructor stub28 }29 30 @Override31 public boolean dispatchTouchEvent(MotionEvent ev) {32 // TODO Auto-generated method stub33 mPager.requestDisallowInterceptTouchEvent(true);34 return super.dispatchTouchEvent(ev);35 }36 37 @Override38 public boolean onInterceptTouchEvent(MotionEvent ev) {39 // TODO Auto-generated method stub40 mPager.requestDisallowInterceptTouchEvent(true);41 return super.onInterceptTouchEvent(ev);42 }43 44 @Override45 public boolean onTouchEvent(MotionEvent event) {46 // TODO Auto-generated method stub47 mPager.requestDisallowInterceptTouchEvent(true);48 return super.onTouchEvent(event);49 }50 51 }
这里注意xml中的使用方法
<com.example.UserGallery
android:layout_width="fill_parent" android:layout_height="100dp" android:id="@+id/GalleryView" />