タブレイアウトに2つのフラグメントがあり、ボタンをクリックしてタブレイアウト間のスワイプ機能を無効にすることができます。 onclickボタンに無効化機能を実装する方法。よろしくお願いします
MainActivity
public class MainActivity extends AppCompatActivity implements TabLayout.OnTabSelectedListener{
private TabLayout tabLayout;
private ViewPager viewPager;
int ans =2;
------FCViewPager disable;------
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
tabLayout = (TabLayout) findViewById(R.id.tabLayout);
BasePagerAdapter adapter = new BasePagerAdapter(getSupportFragmentManager());
Tab1 tab1 = new Tab1();
Tab2 tab2 = new Tab2();
adapter.addFragment(tab1, "1");
adapter.addFragment(tab2, "2");
viewPager = (ViewPager) findViewById(R.id.pager);
viewPager.setAdapter(adapter);
tabLayout.setOnTabSelectedListener(this);
tabLayout.setupWithViewPager(viewPager);
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
tabLayout.setupWithViewPager(viewPager);
}
----- public void tests(ビュービュー){
disable.setEnableSwipe(false);
}---------
@Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
public class BasePagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public BasePagerAdapter(FragmentManager manager) {
super(manager);
}
@Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
@Override
public int getCount() {
return mFragmentList.size();
}
@Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
public void removeFragment(int position) {
mFragmentTitleList.remove(position);
mFragmentList.remove(position);
notifyDataSetChanged();
}
public void addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
@Override
public int getItemPosition(Object object) {
return POSITION_NONE;
}
}
Tab1
public class Tab1 extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View h = inflater.inflate(R.layout.tab1, container, false);
return h;
}
}
Tab2
public class Tab2 extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View h = inflater.inflate(R.layout.tab2, container, false);
return h;
}
ViewPagerでスワイプ機能を無効にすることができます。次のようにカスタムViewPagerを作成します。
public class FCViewPager extends ViewPager {
private boolean enableSwipe;
public FCViewPager(Context context) {
super(context);
init();
}
public FCViewPager(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
private void init() {
enableSwipe = true;
}
@Override
public boolean onInterceptTouchEvent(MotionEvent event) {
// Never allow swiping to switch between pages
return enableSwipe && super.onInterceptTouchEvent(event);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
// Never allow swiping to switch between pages
return enableSwipe && super.onTouchEvent(event);
}
public void setEnableSwipe(boolean enableSwipe) {
this.enableSwipe = enableSwipe;
}
}
MainActivityで
//R.id.pager has to be FCViewPager not default ViewPager
viewPager = (FCViewPager) findViewById(R.id.pager);
ボタンonClick機能
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
pager.setEnableSwipe(false);
}
});
デフォルトおよびプログラムでスクロールを無効および有効にするためのユーザースクロール無効ビューページャー
_public class ScrollDisabledViewpager extends ViewPager {
private boolean isPagingEnabled = false;
public ScrollDisabledViewpager(Context context) {
super(context);
}
public ScrollDisabledViewpager(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
return this.isPagingEnabled && super.onTouchEvent(event);
}
@Override
public boolean onInterceptTouchEvent(MotionEvent event) {
return this.isPagingEnabled && super.onInterceptTouchEvent(event);
}
public void setPagingEnabled(boolean b) {
this.isPagingEnabled = b;
}
}
_
ボタンonClick
イベントこのtrue/falseを設定して、スクロールを有効/無効にすることができます
viewPager.setPagingEnabled(true/ false);