我实施滑动标签有什么问题?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了我实施滑动标签有什么问题?相关的知识,希望对你有一定的参考价值。

我正在使用android Navigation Drawer和Swipe Tabs实现,类似于他们在Android开发者网站上的内容。当我启动应用程序并使用导航抽屉打开新闻片段时,它可以正常工作,如下图所示。

但是当我使用导航抽屉导航到另一个菜单然后返回新闻时,World and Sports片段显示空内容,如下图所示。

这是新闻片段的代码

public class NewsFragment extends Fragment {

private FragmentActivity mActivity;
private ViewPager mViewPager;
private NewsTabsFragmentPagerAdapter mAdapter;
private ActionBar mActionBar;
// Tab titles
private String[] mTabs = { "World", "Sports", "Entertainment" };

private static final String TAG = "NewsFragment";

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    Log.i(TAG, getClass().getSimpleName() + ":entered onCreateView()");

    return inflater.inflate(R.layout.news_fragment,
            container, false);

}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    Log.i(TAG, getClass().getSimpleName() + ":entered onActivityCreated()");
    super.onActivityCreated(savedInstanceState);

    // Get a fragment activity instance that will be used by mAdapter and action bar
    mActivity = (FragmentActivity) getActivity();

    // Create the adapter that will return a fragment for each of the three primary sections
    // of the app.
    mAdapter = new NewsTabsFragmentPagerAdapter(mActivity.getSupportFragmentManager());

    // Set up the action bar.
    //final ActionBar actionBar = mActivity.getActionBar();
    mActionBar = mActivity.getActionBar();

    // Specify that the Home/Up button should not be enabled, since there is no hierarchical
    //parent. Not used in this case since the activity implementing this fragment uses
    //navigation drawer.
    //actionBar.setHomeButtonEnabled(false);
    //mActionBar.setHomeButtonEnabled(false);

    // Specify that we will be displaying tabs in the action bar.
    mActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
    //mActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    // Set up the ViewPager, attaching the adapter and setting up a listener for when the
    // user swipes between sections.
    mViewPager = (ViewPager) mActivity.findViewById(R.id.pager);
    mViewPager.setAdapter(mAdapter);
    mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
        @Override
        public void onPageSelected(int position) {
            // When swiping between different app sections, select the corresponding tab.
            // We can also use ActionBar.Tab#select() to do this if we have a reference to the
            // Tab.
            mActionBar.setSelectedNavigationItem(position);
        }
    });

    // Setup the tabListiner for the tabs.
    ActionBar.TabListener tabListener = new ActionBar.TabListener() {
        @Override
        public void onTabSelected(Tab tab, FragmentTransaction ft) {
            // When the tab is selected, switch to the
            // corresponding page in the ViewPager.
            mViewPager.setCurrentItem(tab.getPosition());

            Log.i(TAG, getClass().getSimpleName() + ":entered onTabSelected()");
        }

        @Override
        public void onTabUnselected(Tab tab, FragmentTransaction ft) {
        }

        @Override
        public void onTabReselected(Tab tab, FragmentTransaction ft) {
        }
    };

    if (mViewPager.getAdapter() != null) {
        // For each of the news category in the app, add a tab to the action bar.
        for (String tab_name : mTabs) {
            mActionBar.addTab(mActionBar.newTab().setText(tab_name)
                    .setTabListener(tabListener));
        }
    }
}

我的问题是,当我离开和离开新闻片段时,为什么世界和体育片段不显示内容。

答案

只需将此mViewPager.setOffscreenPageLimit(3);添加到代码中即可。当您远离它们时,它将保存碎片的状态

以上是关于我实施滑动标签有什么问题?的主要内容,如果未能解决你的问题,请参考以下文章

选择选项卡或滑动页面时如何加载片段

在tablayout viewpager中运行调整选项卡片段

在标签中滑动文本

选项卡片段内的卡片视图

特定片段中的 Snackbar

滑动时 ViewPager 更新片段