2014-02-14 53 views
0

使用與PageAdapter一個動態創建viewpager,作爲禁用特定子(方含拖放)

myViewPager = ControlVP(context); where 

public class ControlVP extends ViewPager { 


    public ControlViewPager(Context context) { 


     super(context); 
     } 

    @Override 
    public boolean onInterceptTouchEvent(MotionEvent event) { 

      return false; 

    } 


     @Override 
    public boolean onTouchEvent(MotionEvent event) { 


    static final int MIN_DISTANCE = 100; 
     float DOWNx = 0; 


       float UPx=0; 

     boolean ret=false; 

     switch (event.getAction()) { 

     case MotionEvent.ACTION_DOWN: {// 

      DOWNx = event.getX(); 

     } 

     case MotionEvent.ACTION_UP: { 

      UPx = event.getX(); 

     } 
     if(UPx-DOWNx>MIN_DISTANCE) 

     { 
      ret=true; 

     } 

     } 
       return ret; 
    } 

} 

但它會導致禁用viewpager作爲一個整體,但我想要的東西這一領域的空白啓用前兩個片段viewpager同時禁用了三分之一第三片段包含拖拽,是有可能,感謝名單提前 我也試過

int[] childIdArr = 
     {R.id.iA,R.id.iAA1,R.id.iAAA2}; 

int sourceId =event.getSource(); 

     for(int i=0;i<=childIdArr.length;i++) { 

      if(sourceId == i) 

{ return false; } 

else { return true; } 


} 

onInterceptTouchEvent(MotionEvent event)但沒有任何工程。

回答

0

在ViewPager的onPageSelected事件中,您可以獲取當前顯示的視圖,然後禁用該視圖。

pager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() { 

      @Override 
      public void onPageSelected(int position) { 
       // on changing the page 
           //if (position == position_to_disable)  
       //see link https://stackoverflow.com/questions/12854783/android-viewpager-get-the-current-view 
           //to see how you get the current view 
           //then set view.setEnabled(false); 
      } 

      @Override 
      public void onPageScrolled(int arg0, float arg1, int arg2) { 
      } 

      @Override 
      public void onPageScrollStateChanged(int arg0) { 
      } 
     }); 

鏈接Android ViewPager get the current View顯示瞭如何獲取當前顯示視圖。