在使用ViewFlipper的时候,里面是两个GridView,每个GridView里面的item都有一个点击事件
如果处理不好很容易在GridView的onFling的时候就点击到他的item就同时执行了item上面的onClick事件
但是这个时候我们想要的仅仅是滑动显示另外一个GridView而已,下面是解决办法
写一个自己的GestureListener, please refer to http://stackoverflow.com/questions/4184382/how-to-implement-both-ontouch-and-also-onfling-in-a-same-listview
public class MYGestureListener extends SimpleOnGestureListener implements OnTouchListener { private GestureDetector gDetector; private ViewFlipper viewFlipper; public MYGestureListener(){ super(); } public MYGestureListener(Context con){ this(con, null, null); } public MYGestureListener(Context con, GestureDetector gDetector, ViewFlipper viewFlipper){ if(null == gDetector){ gDetector = new GestureDetector(con, this); }; this.gDetector = gDetector; this.viewFlipper = viewFlipper; } @Override public boolean onSingleTapConfirmed(MotionEvent e) { return super.onSingleTapConfirmed(e); } @Override public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { //TODO viewFlipper.showNext()...whatever you want return false; } @Override public boolean onTouch(View v, MotionEvent event) { return gDetector.onTouchEvent(event); } public GestureDetector getDector(){ return this.gDetector; } }
public class MyActivity extends Activity{ private Context appCon; private ViewFlipper viewFlipper; private MTCGestureListener gestureListener; private GridView page1Grid; private GridView page2Grid; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.main); appCon = this; initView(); } @Override public boolean onTouchEvent(MotionEvent event) { //This is the important part return gestureListener.getDector().onTouchEvent(event); } private void initView(){ viewFlipper = (ViewFlipper)findViewById(R.id.viewFlipper); page1Grid = (GridView)findViewById(R.id.iconList1); page2Grid = (GridView)findViewById(R.id.iconList2); SimpleAdapter adapter1 = new SimpleAdapter(this, fillMap1(), R.layout.function_item, new String[]{"fnImg","fnTitle"}, new int[]{R.id.fnImg, R.id.fnTitle}); page1Grid.setAdapter(adapter1); SimpleAdapter adapter2 = new SimpleAdapter(this, fillMap2(), R.layout.function_item, new String[]{"fnImg","fnTitle"}, new int[]{R.id.fnImg, R.id.fnTitle}); page2Grid.setAdapter(adapter2); initListener(); } private void initListener(){ gestureListener = new MTCGestureListener(appCon, null, viewFlipper); page1Grid.setOnTouchListener(gestureListener); page2Grid.setOnTouchListener(gestureListener); page1Grid.setOnItemClickListener(listener1); page2Grid.setOnItemClickListener(listener2); } private OnItemClickListener listener1 = new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { //TODO whatever you want } }; private OnItemClickListener listener2 = new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { //TODO whatever you want } }; }