我正在尝试将我的应用锁定在按下按钮时的页面上。我有3个页面可以在之间滑动,只有按一下按钮才能访问第四页面。我通过将getCount设置为4来执行此操作,但是您仍然可以滑动到其他页面,我希望将应用程序锁定为四个,直到按下按钮为止。
这是我的代码:
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
Fragment fragment = null;
switch (position) {
case 0:
fragment = new FragmentRecord();
break;
case 1:
fragment = new FragmentCalc();
break;
case 2:
fragment = new FragmentAccounts();
break;
case 3:
fragment = new FragmentSettings();
break;
}
return fragment;
}
@Override
public int getCount() {
if (showSettings) {
return 4;
} else {
return 3;
}
}
}
public void settingsButton(View view) {
showSettings = true;
View scrollbar = (View) findViewById(R.id.headerSlider);
scrollbar.setVisibility(View.GONE);
mSectionsPagerAdapter.notifyDataSetChanged();
mViewPager.setCurrentItem(3);
}
答案 0 :(得分:1)
您可以创建一个自定义ViewPager
类,可以控制其滑动动作:
import android.content.Context;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.MotionEvent;
public class ViewPagerSwipeControlled extends ViewPager {
private boolean swipeEnabled = true;
public ViewPagerSwipeControlled(Context context) {
super(context);
}
public ViewPagerSwipeControlled(Context context, AttributeSet attrs) {
super(context, attrs);
}
public void setSwipeEnabled(boolean swipeEnabled) {
this.swipeEnabled = swipeEnabled;
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
return swipeEnabled;
}
@Override
public boolean onTouchEvent(MotionEvent ev) {
if (swipeEnabled) {
return super.onTouchEvent(ev);
} else {
return true;
}
}
}
在布局xml中:
<com.myapp.view.ViewPagerSwipeControlled
android:id="@+id/viewPager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
启用和禁用可滑动性:
ViewPagerSwipeControlled viewPager = findViewById(R.id.viewPager);
...
viewPager.setSwipeEnabled(false);
...
viewPager.setSwipeEnabled(true);