gpt4 book ai didi

android - ViewPager 没有正确加载中间页

转载 作者:太空宇宙 更新时间:2023-11-03 11:03:56 29 4
gpt4 key购买 nike

我有一个 fragment ,里面有一个 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

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

代码中带有选项卡布局的 fragment

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>

滑动选项卡选项的 fragment 代码(所有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 个 fragment 的布局都是一个简单的 TextView

渐变文件

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'
}

最佳答案

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

尝试将您的支持库版本降级到 23.0.123.1.123.2.0 似乎存在这个错误。

希望这对您有所帮助!

关于android - ViewPager 没有正确加载中间页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36293897/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com