Ich habe ein Fragment mit einem TabHost
darin mit 3 Tabs. Meine Hauptaktivität hat eine Navigationsleiste und wenn die Option mit einem Tab-Layout ausgewählt ist, lädt sie beim ersten Mal alle drei Tabs korrekt. Wenn ich aber die Schublade öffne und erneut auf die Option mit Tabs klicke, wird die mittlere Option nicht angezeigt. Ich bekomme diese Meldung auch während des Debuggens.ViewPager lädt die mittlere Seite nicht korrekt
W/FragmentManager﹕ moveToState: Fragment state for PageThreeFragment{426c2ad8 #3 id=0x7f0f0063 android:switcher:2131689571:2} not updated inline; expected state 3 found 2
Ich hoffe, jemand kann mir helfen, diesen Fehler zu beheben, weil die Lösung mir ausweicht.
Fragment mit Tab Layout innerhalb Code
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 Layout
<?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 Code für die Schiebe Register Optionen (insgesamt 3 sind im Grunde die gleichen)
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();
}
}
Das Layout für alle 3 Fragmente ist ein einfaches TextView
gradle Datei
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'
}
Ist Ihre Support-Bibliothek Version 23.2.0? –
Ja, das ist die Version, die ich verwende –
Hallo, konnten Sie das Problem lösen? –