2014-05-07 67 views
1

我正在開發一個android應用程序,在這個應用程序中,我使用android view pager在4個片段之間滑動,在表現良好的片段之間進行滑動。我需要的是我想禁用滑動,直到第一個片段completed.if所有必要的操作在特定的片段滑動執行必須啓用該片段等等,直到最後一個片段。我該怎麼做這個可以任何人有suggetions?。請記住,我需要控制從所有的刷卡fragments.please幫我out.thanks提前..在Android ViewPager上禁用滑動

public class MyPagerAdapter extends FragmentPagerAdapter { 

    private List<Fragment> fragments; 

    public MyPagerAdapter(FragmentManager fm) { 
     super(fm); 
     this.fragments = new ArrayList<Fragment>(); 
     fragments.add(new SalesTransactionFragment()); 
     fragments.add(new FragmentCollection()); 

    // fragments.add(new FragmentBlue()); 
     fragments.add(new FragmentGreen()); 
     fragments.add(new FragmentPink()); 

    } 

    @Override 
    public Fragment getItem(int position) { 
     return fragments.get(position); 
    } 

    @Override 
    public int getCount() { 
     return fragments.size(); 
    } 
} 

MainActivity

public class MainActivity extends FragmentActivity { 


     @Override 
     protected void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.activity_main); 

      MyPagerAdapter pageAdapter = new MyPagerAdapter(getSupportFragmentManager()); 
      ViewPager pager = (ViewPager)findViewById(R.id.myViewPager); 
      pager.setOffscreenPageLimit(4); 

      pager.setAdapter(pageAdapter); 
     } 

其中一個片段的

public class FragmentPink extends Fragment { 
Button b; 
    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 

     View view = inflater.inflate(R.layout.fragment_pink, container, false); 
     return view; 
    } 
    @Override 
    public void onActivityCreated(Bundle savedInstanceState) { 
     // TODO Auto-generated method stub 
     super.onActivityCreated(savedInstanceState); 
     b=(Button) getActivity().findViewById(R.id.button1); 
     b.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
Intent i = new Intent(getActivity(),MainActivity.class); 
startActivity(i); 
      } 
     }); 
    } 
} 
+0

如果您不想滑動功能,則可以在其他選項中使用選項卡布局。 –

回答

2

一種方法是設置OnTouchListenerViewPager這樣的 -

mPager.setOnTouchListener(new OnTouchListener() {   
    @Override 
    public boolean onTouch(View v, MotionEvent event) { 
     if(mPaginEnabled) 
      return false; 
     else 
      return true; 
    } 
}); 
+0

它sdoms doessome swiping ..就像1% –

+0

這對我不起作用 –

2

你需要繼承ViewPager

public class MyViewPager extends ViewPager { 

    private boolean mSwipable = true; 

    public MyViewPager(Context context) { 
     super(context); 
    } 

    public MyViewPager(Context context, AttributeSet attrs) { 
     super(context, attrs); 
    } 

    @Override 
    public boolean onInterceptTouchEvent(MotionEvent event) { 
     return mSwipable ? super.onInterceptTouchEvent(event) : false; 
    } 

    @Override 
    public boolean onTouchEvent(MotionEvent event) { 
     return mSwipable ? super.onTouchEvent(event) : false; 
    } 

    public boolean isSwipable() { 
     return mSwipable; 
    } 

    public void setSwipable(boolean swipable) { 
     mSwipable = swipable; 
    } 
} 

現在你可以撥打啓用/禁用滑動功能3210方法。

UPD:要在XML編輯此屬性的類將是:

public class MyViewPager extends ViewPager { 

    private boolean mSwipable = true; 

    public MyViewPager(Context context) { 
     super(context); 
    } 

    public MyViewPager(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.MyViewPager); 
     try { 
      mSwipable = a.getBoolean(R.styleable.MyViewPager_swipeable, true); 
     } finally { 
      a.recycle(); 
     } 
    } 

    @Override 
    public boolean onInterceptTouchEvent(MotionEvent event) { 
     return mSwipable ? super.onInterceptTouchEvent(event) : false; 
    } 

    @Override 
    public boolean onTouchEvent(MotionEvent event) { 
     return mSwipable ? super.onTouchEvent(event) : false; 
    } 

    public boolean isSwipable() { 
     return mSwipable; 
    } 

    public void setSwipable(boolean swipable) { 
     mSwipable = swipable; 
    } 
} 

與你的價值觀/ attr.xml:

<declare-styleable name="MyViewPager"> 
    <attr name="swipeable" format="boolean" /> 
</declare-styleable> 

現在你可以在你的佈局中使用它:

<my.package.name.MyViewPager 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:id="@+id/viewPager" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    app:swipeable="false"/> 
+0

AttributeSet attrs – Noufal

+0

將會有什麼價值@Noufal請看我更新的答案。 – erakitin