ViewPager没有正确加载中间页面

时间:2016-03-29 19:53:28

标签: android android-layout android-fragments android-viewpager android-tabhost

我有一个片段,里面有ggplot(df, aes("", values)) + geom_boxplot() + facet_grid(. ~ source) + labs(x="") ,有3个标签。我的主要活动有一个导航抽屉,当选择内部标签布局时,它会在第一次正确加载所有3个标签,但是如果我打开抽屉并再次单击选项卡,则中间选项永远不会显示。调试时我也经常收到这条消息。

TabHost

我希望有人能帮助我解决这个问题,因为解决方案正在躲避我。

代码中带有标签布局的片段

W/FragmentManager﹕ moveToState: Fragment state for PageThreeFragment{426c2ad8 #3 id=0x7f0f0063 android:switcher:2131689571:2} not updated inline; expected state 3 found 2

xml layout

package com.robotca.ControlApp.Fragments;

import android.content.Context;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TabHost;
import android.widget.TabWidget;

import java.util.ArrayList;
import com.robotca.ControlApp.R;

public class HelpFragment extends Fragment
{

private TabHost mTabHost;
private ViewPager mViewPager;
private TabsAdapter mTabsAdapter;

public HelpFragment() {
}

@Override
public void onCreate(Bundle instance)
{
    super.onCreate(instance);

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.fragment_help, container, false);

    mTabHost = (TabHost) v.findViewById(android.R.id.tabhost);
    mTabHost.setup();

    mViewPager = (ViewPager) v.findViewById(R.id.pager);
    mTabsAdapter = new TabsAdapter(getActivity(), mTabHost, mViewPager);

    // Here we load the content for each tab.
    mTabsAdapter.addTab(mTabHost.newTabSpec("one").setIndicator("Setup"), PageOneFragment.class, null);
    mTabsAdapter.addTab(mTabHost.newTabSpec("two").setIndicator("Using"), PageTwoFragment.class, null);
    mTabsAdapter.addTab(mTabHost.newTabSpec("three").setIndicator("FAQ"), PageThreeFragment.class, null);


    return v;
}

public static class TabsAdapter extends FragmentPagerAdapter implements TabHost.OnTabChangeListener, ViewPager.OnPageChangeListener
{
    private final Context mContext;
    private final TabHost mTabHost;
    private final ViewPager mViewPager;
    private final ArrayList<TabInfo> mTabs = new ArrayList<TabInfo>();

    static final class TabInfo
    {
        private final String tag;
        private final Class<?> clss;
        private final Bundle args;

        TabInfo(String _tag, Class<?> _class, Bundle _args)
        {
            tag = _tag;
            clss = _class;
            args = _args;
        }
    }

    static class DummyTabFactory implements TabHost.TabContentFactory
    {
        private final Context mContext;

        public DummyTabFactory(Context context)
        {
            mContext = context;
        }

        public View createTabContent(String tag)
        {
            View v = new View(mContext);
            v.setMinimumWidth(0);
            v.setMinimumHeight(0);
            return v;
        }
    }

    public TabsAdapter(FragmentActivity activity, TabHost tabHost, ViewPager pager)
    {
        super(activity.getSupportFragmentManager());
        mContext = activity;
        mTabHost = tabHost;
        mViewPager = pager;
        mTabHost.setOnTabChangedListener(this);
        mViewPager.setAdapter(this);
        mViewPager.setOnPageChangeListener(this);
    }

    public void addTab(TabHost.TabSpec tabSpec, Class<?> clss, Bundle args)
    {
        tabSpec.setContent(new DummyTabFactory(mContext));
        String tag = tabSpec.getTag();

        TabInfo info = new TabInfo(tag, clss, args);
        mTabs.add(info);
        mTabHost.addTab(tabSpec);
        notifyDataSetChanged();
    }

    @Override
    public int getCount()
    {
        return mTabs.size();
    }

    @Override
    public Fragment getItem(int position)
    {
        TabInfo info = mTabs.get(position);

        return Fragment.instantiate(mContext, info.clss.getName(), info.args);

    }

    public void onTabChanged(String tabId)
    {
        int position = mTabHost.getCurrentTab();
        mViewPager.setCurrentItem(position);
    }

    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels)
    {
    }

    public void onPageSelected(int position)
    {
        // Unfortunately when TabHost changes the current tab, it kindly
        // also takes care of putting focus on it when not in touch mode.
        // The jerk.
        // This hack tries to prevent this from pulling focus out of our
        // ViewPager.
        TabWidget widget = mTabHost.getTabWidget();
        int oldFocusability = widget.getDescendantFocusability();
        widget.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
        mTabHost.setCurrentTab(position);
        widget.setDescendantFocusability(oldFocusability);
    }

    public void onPageScrollStateChanged(int state)
    {
    }
}
}

滑动选项卡选项的片段代码(所有3个基本相同)

<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent">

<LinearLayout
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TabWidget
        android:id="@android:id/tabs"
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0"/>

    <FrameLayout
        android:id="@android:id/tabcontent"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_weight="0"/>

    <android.support.v4.view.ViewPager
        android:id="@+id/pager"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"/>

</LinearLayout>
</TabHost>

所有3个片段的布局都是一个简单的TextView

gradle文件

public class PageOneFragment extends Fragment
{
@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
    return inflater.inflate(R.layout.pageone_fragment, container, false);
}

@Override
public void onActivityCreated(Bundle savedInstanceState)
{
    super.onActivityCreated(savedInstanceState);
}

@Override
public void onAttach(Activity activity)
{
    super.onAttach(activity);
}

@Override
public void onStart()
{
    super.onStart();
}

@Override
public void onResume()
{
    super.onResume();
}

@Override
public void onDestroy()
{
    super.onDestroy();
}
}   

2 个答案:

答案 0 :(得分:0)

https://code.google.com/p/android/issues/detail?id=202037报告了与ViewPager + FragmentStatePagerAdapter相关的嵌套片段问题。

尝试将支持库版本降级为23.0.123.1.123.2.0似乎存在此错误。

希望这会有所帮助!!

答案 1 :(得分:0)

将每个支持库的版本号减少到23.0.1对我来说很有用。

build.gradle文件 -

compile 'com.android.support:design:23.0.1'
compile 'com.android.support:appcompat-v7:23.0.1'
compile 'com.android.support:support-v13:23.0.1'    
compile 'com.android.support:recyclerview-v7:23.0.1'

我希望这就是你要找的东西。