客製化UI元件(1) Custom UI components - disable sliding function of VeiwPager

如果想改變他的預設動作,那麼就可以不要執行他的super,就可以自訂在某些時後要做啥些事。

以ViewPager這個control為例,如果想把swipe then change page這件事改掉,可以new一個class去extend ViewPager,

package com.alison.test

pubilc class CustomViewPager extends ViewPager{

     private boolean value;
     public CustomViewPager(Context context, AttributeSet attrs){
          super(context,attrs);
          this.value = false;
     }
     
     // override some method
     public boolean onTouchEvent(MotionEvent event){
         // do some customization
          if(value){
               return super.onTouchEvent(event);
          }
          return false
     }
}

xml file

<com.alison.test.CustomViewPager 
     android:id="@+id/customPager
     ...
/>

java file that call it

CustomViewPager mViewPager = (CustomViewPager)findViewById(R.id.customPager); 
More detail -- [http://developer.android.com/training/custom-views/create-view.html][1]