使用多个方案处理TabLayout

时间:2017-02-14 10:50:07

标签: java android android-viewpager android-tablayout fragmentstatepageradapter

我使用<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script src="//cdnjs.cloudflare.com/ajax/libs/raphael/2.1.0/raphael-min.js"></script> <script src="//cdnjs.cloudflare.com/ajax/libs/morris.js/0.5.1/morris.min.js"></script> <link href="//cdnjs.cloudflare.com/ajax/libs/morris.js/0.5.1/morris.css" rel="stylesheet" /> <div id="samplechart"></div> <a href="#" onclick="getMorrisOffline();">Refresh Morris</a> + TabLayout来显示几个标签。根据服务器的响应,我想创建两种可能的场景:第一种是三个选项卡,第二种只有两种,&#39;删除&#39;位于0的标签。

因为我正在删除&#39;第一个选项卡,依赖于位置来执行与特定片段相关的任何事情的所有方法都会过时,并且很可能无法通过ViewPager检查。

例如,这是instanceof中的getItem,目前还没有处理第二种情况:

FragmentStatePagerAdapter

@Override public Fragment getItem(int position) { AbstractFragment fragment; switch (position) { case 0: fragment = new MainFragment(); break; case 1: fragment = new MessagesFragment(); break; case 2: fragment = new SearchFragment(); break; default: return null; } return fragment; } 中的监听器示例(检查将在第二种情况下失败):

MainActivity

我唯一的想法是检查Fragment fragment = viewPagerAdapter.getRegisteredFragment(2); if (fragment instanceof SearchFragment) { ((SearchFragment)fragment).doStuff(); } 中每个监听器中的服务器响应(即,是否有两个或三个选项卡)以及MainActivity中的每个监听器,这在我看来是一个可怕的解决方案。有没有更好的方法来解决这个问题?

2 个答案:

答案 0 :(得分:1)

由于只有很少的标签,一个好的方法是制作一个你想要展示的片段列表:

List<AbstractFragment> fragments = new ArrayList<>();
if (showFirstTab) {
    fragments.add(new MainFragment());
}
fragments.add(new MessagesFragment());
fragments.add(new SearchFragment());

然后在getItem方法,这样做:

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

当您需要获取特定片段时,您可以获取fragments列表并迭代测试instanceof

让我知道它是否对你有所帮助。

答案 1 :(得分:0)

您可以使用自定义片段寻呼机适配器来逐位获取

#2-Aggr

现在只需按public abstract class FragmentPagerAdapter extends PagerAdapter { private static final String TAG = "FragmentPagerAdapter"; private static final boolean DEBUG = false; private final FragmentManager mFragmentManager; private FragmentTransaction mCurTransaction = null; private Fragment mCurrentPrimaryItem = null; private String mAdapterClassName; protected FragmentPagerAdapter(FragmentManager fm, String adapterClassName) { mFragmentManager = fm; mAdapterClassName = adapterClassName; } /** * Return the Fragment associated with a specified position. */ protected abstract Fragment getItem(int position); @Override public void startUpdate(ViewGroup container) { } @Override public Object instantiateItem(ViewGroup container, int position) { if (mCurTransaction == null) { mCurTransaction = mFragmentManager.beginTransaction(); } // Do we already have this fragment? String name = makeFragmentTag(position); Fragment fragment = mFragmentManager.findFragmentByTag(name); if (fragment != null) { mCurTransaction.attach(fragment); } else { fragment = getItem(position); mCurTransaction.add(container.getId(), fragment, makeFragmentTag(position)); } if (fragment != mCurrentPrimaryItem) { fragment.setMenuVisibility(false); fragment.setUserVisibleHint(false); } return fragment; } @Override public void destroyItem(ViewGroup container, int position, Object object) { if (mCurTransaction == null) { mCurTransaction = mFragmentManager.beginTransaction(); } mCurTransaction.detach((Fragment) object); } @Override public void setPrimaryItem(ViewGroup container, int position, Object object) { Fragment fragment = (Fragment) object; if (fragment != mCurrentPrimaryItem) { if (mCurrentPrimaryItem != null) { mCurrentPrimaryItem.setMenuVisibility(false); mCurrentPrimaryItem.setUserVisibleHint(false); } if (fragment != null) { fragment.setMenuVisibility(true); fragment.setUserVisibleHint(true); } mCurrentPrimaryItem = fragment; } } @Override public void finishUpdate(ViewGroup container) { if (mCurTransaction != null) { mCurTransaction.commitAllowingStateLoss(); mCurTransaction = null; mFragmentManager.executePendingTransactions(); } } @Override public boolean isViewFromObject(View view, Object object) { return ((Fragment) object).getView() == view; } @Override public Parcelable saveState() { return null; } @Override public void restoreState(Parcelable state, ClassLoader loader) { } /** * Return a unique identifier for the item at the given position. * <p/> * <p>The default implementation returns the given position. * Subclasses should override this method if the positions of items can change.</p> * * @param position Position within this adapter * @return Unique identifier for the item at position */ private long getItemId(int position) { return position; } public String makeFragmentTag(int position) { return "purplle:switcher:" + mAdapterClassName + ":" + position; } public static String makeFragmentTag(int position,String adapterClassName) { return "purplle:switcher:" + adapterClassName + ":" + position; } }

扩展viewPager适配器类

获取活动中的片段。

FragmentPagerAdapter