ViewPager未正确加载中间页面

4

我有一个包含TabHost的片段,其中有3个选项卡。我的主活动有一个导航抽屉,当选择带有选项卡布局的选项时,第一次正确加载所有3个选项卡,但如果我打开抽屉并再次点击具有选项卡的选项,则中间选项永远不会出现。同时在调试过程中我也经常得到这个消息。

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

我希望有人能帮助我解决这个bug,因为解决方案一直逃避我。
代码中包含选项卡布局的片段。
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)
    {
    }
}
}

XML布局

<?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个基本相同)

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();
}
}   

所有三个片段的布局都是简单的TextView。
gradle文件。
dependencies {
//    compile project(':control_app_lib')
compile fileTree(dir: 'libs', include: ['*.jar'])
///////////////

compile 'org.ros.android_core:android_10:[0.2,0.3)'
compile 'org.ros.android_core:android_15:[0.2,0.3)'
compile 'com.github.rosjava.android_extras:gingerbread:[0.2,0.3)'
compile 'org.ros.rosjava_messages:tf2_msgs:[0.5,0.6)'
compile 'com.google.code.gson:gson:2.5'
compile 'com.android.support:support-v13:23.0.1'
compile 'com.android.support:appcompat-v7:23.0.1'
compile 'com.android.support:cardview-v7:23.0.1'
compile 'com.android.support:recyclerview-v7:23.0.1'
compile 'org.osmdroid:osmdroid-android:5.0.1'
compile 'org.slf4j:slf4j-android:1.6.1-RC1'
compile 'com.github.MKergall.osmbonuspack:OSMBonusPack:v5.7'
compile 'com.android.support:support-v4:23.0.1'
compile 'com.github.amlcurran.showcaseview:library:5.4.1'
}

你的支持库版本是23.2.0吗? - Gueorgui Obregon
是的,那是我正在使用的版本。 - Kenneth Spear
你好,你能解决这个问题吗? - Rahul Hawge
2个回答

0

很遗憾,这对我来说没有起作用。support-v4和appcombat-v7两者都需要是23.0.1版本吗? - Kenneth Spear
我也遇到了这个代码问题,一直无法找出问题所在。虽然尝试了你的建议,但仍然卡住了。 - Pranav Bhoraskar
对我来说,即使有两个选项卡,在第一次加载时它们都能正常工作,但如果我再次从导航抽屉中选择该选项,它们中的任何一个也不会出现。如果有三个选项,则仅中间的不会出现。 - Kenneth Spear
1
我首先将appcompat-v7和support:design库更改为23.0.1,但没有用。之后,我将所有的android support库,包括recycler view库,都更改为23.0.1 - 现在我不再收到MoveToState警告了。希望这能对你有所帮助。 - Pranav Bhoraskar
你能发一下你的Gradle文件里都有什么吗? - Kenneth Spear

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'

希望这是你所寻找的。


谢谢您的尝试,但很遗憾这对我没有起作用。 - Kenneth Spear

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接