解决方法,重新定义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 context 14 * @param attrs 15 */ 16 public UserGallery(Context context) { 17 super(context); 18 // TODO Auto-generated constructor stub 19 } 20 21 /** 22 * @param context 23 * @param attrs 24 */ 25 public UserGallery(Context context, AttributeSet attrs) { 26 super(context, attrs); 27 // TODO Auto-generated constructor stub 28 } 29 30 @Override 31 public boolean dispatchTouchEvent(MotionEvent ev) { 32 // TODO Auto-generated method stub 33 mPager.requestDisallowInterceptTouchEvent(true); 34 return super.dispatchTouchEvent(ev); 35 } 36 37 @Override 38 public boolean onInterceptTouchEvent(MotionEvent ev) { 39 // TODO Auto-generated method stub 40 mPager.requestDisallowInterceptTouchEvent(true); 41 return super.onInterceptTouchEvent(ev); 42 } 43 44 @Override 45 public boolean onTouchEvent(MotionEvent event) { 46 // TODO Auto-generated method stub 47 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"
/>