我想在我的案例中仅在特定片段,HISTORY选项卡上删除/隐藏FAB。
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
.... (Overriding methods , I didnt do anything here
});
我的浏览View Pager的方法
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFragment(new HistoryFragment(), "HISTORY");
adapter.addFragment(new TransactionFragment(), "CURRENT");
adapter.addFragment(new FutureFragment(), "FUTURE");
viewPager.setAdapter(adapter);
}
class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
@Override
public android.support.v4.app.Fragment getItem(int position) {
return mFragmentList.get(position);
}
@Override
public int getCount() {
return mFragmentList.size();
}
public void addFragment(android.support.v4.app.Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
@Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
我应该在何处以及如何更改我的代码以从HISTORY选项卡/片段中隐藏我的FAB并在其他选项卡上再次显示
答案 0 :(得分:5)
我认为这很简单:
viewPager.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
@Override
public void onPageSelected(int position) {
if(position == 0) {
// hide FAB
} else {
// show FAB
}
}
}
您可以进行大量清理以改善维护,例如为每个页面分配页码,如:
private static final int PAGE_HISTORY = 0;
private static final int PAGE_TRANSACTION = 1;
private static final int PAGE_FUTURE = 2;
而不是if(position == 0)
你说的是if(position == PAGE_HISTORY)
。这样,如果您更改了页面,则无需更改大量代码。
答案 1 :(得分:0)
您可以尝试将OnTabSelectedListener设置为TabLayout。
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
yourFAB.setVisibilty(View.GONE);
// or
yourFAB.setVisibilty(View.VISIBLE);
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
答案 2 :(得分:0)
只需将你的OnPageChangeListener添加:
@Override
public void onPageSelected(int position) {
if (adapter.getItem(position) instanceof HistoryFragment) fab.hide();
//else fab.show();
}