【问题标题】:ViewPager not loading middle page correctlyViewPager 没有正确加载中间页
【发布时间】:2016-03-29 19:53:28
【问题描述】:

我有一个片段,里面有一个 TabHost 和 3 个标签。 My main activity has a navigation drawer and when the option with a tab layout inside is selected it loads all 3 tabs correctly the first time, but if I open the drawer and click the option with tabs again, the middle option never shows up.我在调试时也经常收到此消息。

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

我希望有人可以帮助我解决这个错误,因为解决方案正在逃避我。

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

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

所有 3 个片段的布局都是一个简单的 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 吗?
  • 是的,这就是我正在使用的版本
  • 嗨,你能解决这个问题吗?

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


【解决方案1】:

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

尝试将您的支持库版本降级为23.0.1。使用23.1.1 和23.2.0 似乎存在这个bug。

希望这会有所帮助!

【讨论】:

  • 不幸的是,这对我不起作用。 support-v4 和 appcombat-v7 都需要 23.0.1 吗?
  • 即使我的代码也遇到了这个问题。还不能弄清楚问题。不过,尝试了你的建议 - 仍然卡住了。
  • 对我来说,即使有 2 个选项卡,它们第一次加载也很好,但如果我再次单击导航抽屉中的选项,它们都不会出现。三个选项,只有中间不显示
  • 我首先将 appcompat-v7 和 support:design 库更改为 23.0.1,但没有用。之后,我将包括回收站视图库在内的所有 android 支持库更改为 23.0.1 - 现在我不再收到 MoveToState 警告。我希望这可以帮助你。
  • 你能把你的 gradle 文件里的东西贴出来吗?
【解决方案2】:

将每个支持库的版本号减少到 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'

我希望这就是你要找的。​​p>

【讨论】:

  • 感谢您的尝试,但遗憾的是这对我不起作用
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-08-18
  • 2015-10-19
  • 1970-01-01
  • 2023-04-06
  • 1970-01-01
  • 1970-01-01
  • 2015-12-20
相关资源
最近更新 更多