black fragment content when changing tab back - android

I have a fragment activity with TabHost and ViewPager inside it.
There are two tabs for the activity: the List tab and the Map tab.
On the List tab I've got list of my data (some institutions) and on the Map tab (using Google Maps API) I am displaying that institutions on the map with markers.
When activity is opened default tab is List. Swiping from right to left, or selecting Map tab brings me to the Map with markers.
The problem is, when I'm selecting the List tab again I've got a black content in stead of ListView and it's items. However, when from the List tab I select the Map tab again, Map shows correctly.
Here is activity source:
public class Institutions extends FragmentActivity {
public static final int TAB_LIST = 0;
public static final int TAB_MAP = 1;
private TabHost mTabHost;
private ViewPager mViewPager;
private TabsAdapter mTabsAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.institutions);
mTabHost = (TabHost) findViewById(android.R.id.tabhost);
mTabHost.setup();
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setOffscreenPageLimit(1);
mTabsAdapter = new TabsAdapter(this, mTabHost, mViewPager);
View v = createTab(TAB_LIST);
mTabsAdapter.addTab(mTabHost.newTabSpec("list").setIndicator(v),
InstitutionList.class, null);
v = createTab(TAB_MAP);
mTabsAdapter.addTab(mTabHost.newTabSpec("map").setIndicator(v),
InstitutionMap.class, null);
TextView activity_title = (TextView) findViewById(R.id.activity_title);
activity_title.setText(getIntent().getStringExtra(
Institution.INSTITUTION_TITLE));
}
private View createTab(int type) {
View v = getLayoutInflater().inflate(R.layout.tab, null);
TextView label = (TextView) v.findViewById(R.id.label);
ImageView icon = (ImageView) v.findViewById(R.id.icon);
icon.setVisibility(View.GONE);
icon.getLayoutParams().width = 1;
icon.getLayoutParams().height = 1;
switch (type) {
case TAB_LIST:
label.setText(getString(R.string.list_title));
break;
case TAB_MAP:
label.setText(getString(R.string.map_title));
break;
default:
break;
}
return v;
}
}
Here is the source for TabsAdapter:
public class TabsAdapter extends FragmentStatePagerAdapter 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 Class<? extends Fragment> clss;
private final Bundle args;
private Tab tab;
TabInfo(Class<? extends Fragment> _class, Bundle _args) {
clss = _class;
args = _args;
tab = null;
}
}
static class TabFactory implements TabHost.TabContentFactory {
private final Context mContext;
public TabFactory(Context context) {
mContext = context;
}
#Override
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<? extends Fragment> clss,
Bundle args) {
tabSpec.setContent(new TabFactory(mContext));
TabInfo info = new TabInfo(clss, args);
mTabs.add(info);
mTabHost.addTab(tabSpec);
notifyDataSetChanged();
}
#Override
public int getItemPosition(Object object) {
return POSITION_NONE;
}
#Override
public int getCount() {
return mTabs.size();
}
#Override
public Fragment getItem(int position) {
TabInfo info = mTabs.get(position);
if (info.tab == null) {
info.tab = (Tab) Fragment.instantiate(mContext,
info.clss.getName(), info.args);
}
return info.tab;
}
#Override
public void onTabChanged(String tabId) {
int position = mTabHost.getCurrentTab();
mViewPager.setCurrentItem(position);
for (int i = 0; i < getCount(); i++) {
if (i == position)
continue;
Tab t = mTabs.get(i).tab;
if (t != null) {
t.onTabUnselected();
}
}
}
#Override
public void onPageScrolled(int position, float positionOffset,
int positionOffsetPixels) {
}
#Override
public void onPageSelected(int position) {
TabWidget widget = mTabHost.getTabWidget();
int oldFocusability = widget.getDescendantFocusability();
widget.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
mTabHost.setCurrentTab(position);
widget.setDescendantFocusability(oldFocusability);
}
#Override
public void onPageScrollStateChanged(int state) {
}
}

Related

Update FragmentPagerAdapter from an inside Fragment

I am using a custom FragmentPagerAdapter that in the beggining has a certain number of fragments in it by using a tabHost:
public class TabsAdapter extends FragmentPagerAdapter implements TabHost.OnTabChangeListener, ViewPager.OnPageChangeListener
{
private final Context mcon;
private final TabHost mTabHost;
private final ViewPager mViewPager;
private final ArrayList<TabInfo> mTabs = new ArrayList<TabInfo>();
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;
}
}
class DummyTabFactory implements TabHost.TabContentFactory
{
private final Context mcon;
public DummyTabFactory(Context context)
{
mcon = context;
}
public View createTabContent(String tag)
{
View v = new View(mcon);
v.setMinimumWidth(0);
v.setMinimumHeight(0);
return v;
}
}
public TabsAdapter(Fragment fragment, TabHost tabHost, ViewPager pager) {
super(fragment.getChildFragmentManager());
mcon = fragment.getActivity();
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(mcon));
String tag = tabSpec.getTag();
TabInfo info = new TabInfo(tag, clss, args);
mTabs.add(info);
notifyDataSetChanged();
mTabHost.addTab(tabSpec);
}
#Override
public int getCount()
{
return mTabs.size();
}
#Override
public Fragment getItem(int position)
{
TabInfo info = mTabs.get(position);
return Fragment.instantiate(mcon, info.clss.getName(), info.args);
}
public void onTabChanged(String tabId)
{
int position = mTabHost.getCurrentTab();
mViewPager.setCurrentItem(position);
}
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
View tabView = mTabHost.getTabWidget().getChildAt(position);
if (tabView != null)
{
final int width = mHorizontalScroll.getWidth();
final int scrollPos = tabView.getLeft() - (width - tabView.getWidth()) / 2;
mHorizontalScroll.scrollTo(scrollPos, 0);
} else {
mHorizontalScroll.scrollBy(positionOffsetPixels, 0);
}
}
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)
{
}
}
Every fragment is created in the beggining by:
mTabsAdapter.addTab(mTabHost.newTabSpec("tab1").setIndicator("tab1")), InsideFragment.class, gargs);
What I want to do is to update the number of fragments (add/remove fragment) from inside one of the InsideFragments, after it has been initialized. How could I do that?
I didn't test it but maybe you could pass an interface from the MainFragment to the ChildFragment. Then in the ChildFragment you would call a method of this interface like, listener.refreshParent().
Take a look at this answer: https://stackoverflow.com/a/18585247/3465623

Opening a specific Tab which is inside a Fragment from NavigationDrawer in Android

I have implemented NavigationDrawer which has static types of items in it. And I have added ViewPager and Tabs in one of the many fragments. Everything is working fine till now. But now I want to open specific tabs and ViewPagers onClick of the items of that NavigationDrawer. I'm trying to set the current item and tab from the adapter class of the NavigationDrawer but not getting proper result.
TabsAdapter.java
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) {
}
}
Can anyone please help me in it?
You are implementing nested fragments.
Can you try to replace your activity.getSupportFragmentManager() with activity.getChildSupportFragmentManager().

Android set icon to customTabHost

i'm wrote simple code to have custom TabHost in application. this class extended from ABS and fregment, this code work correctly and i want to set icon for tabs, but i can not do it.
public class ActivitySmsSendSelectorMain extends SherlockFragmentActivity {
TabHost mTabHost;
ViewPager mViewPager;
TabsAdapter mTabsAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sms_send_selector_main);
mTabHost = (TabHost) findViewById(android.R.id.tabhost);
mTabHost.setup();
mViewPager = (ViewPager) findViewById(R.id.pager);
mTabsAdapter = new TabsAdapter(this, mTabHost, mViewPager);
mTabsAdapter.addTab(mTabHost.newTabSpec("ContactView")
.setIndicator(UC.getResourcesText(R.string.abs_sms_send_selector_contact_view)),
FragmentSample1.class, null);
mTabsAdapter.addTab(mTabHost.newTabSpec("MobileArchive")
.setIndicator(UC.getResourcesText(R.string.abs_sms_send_selector_archive_mobile)),
FragmentSample2.class, null);
if (savedInstanceState != null) {
mTabHost.setCurrentTabByTag(savedInstanceState.getString("tab"));
}
}
#Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putString("tab", mTabHost.getCurrentTabTag());
}
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;
}
#Override
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);
}
#Override
public void onTabChanged(String tabId) {
int position = mTabHost.getCurrentTab();
mViewPager.setCurrentItem(position);
}
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {}
#Override
public void onPageSelected(int position) {
TabWidget widget = mTabHost.getTabWidget();
int oldFocusability = widget.getDescendantFocusability();
widget.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
mTabHost.setCurrentTab(position);
widget.setDescendantFocusability(oldFocusability);
}
#Override
public void onPageScrollStateChanged(int state) {
}
}
i'm try to change mTabHost to :
mTabsAdapter.addTab(mTabHost.newTabSpec("ContactView")
.setIndicator(UC.getResourcesText(R.string.abs_sms_send_selector_contact_view),
G.context.getResources().getDrawable(R.drawable.app_user)),
FragmentSmsSendSelectorContactView.class, null);
but it doesnt show icon into tab

Get Fragment Text View from Activity

So Im using view pager and action bar to set fragments in my activity, now I want to set text on one of my fragment from the container activity which at the moment seems impossible to me..Googling suggested that Fragment.bgintransaction() should be added with the tag and then should be identified with that tag but here in my pager based fragment there is no BeginTransaction() method. Kindly suggest any way of doin
public class AdminTabsPager extends SherlockFragmentActivity {
private TabHost mTabHost;
private ViewPager mViewPager;
private TabsAdapter mTabsAdapter;
private String comingFrom ="";
private String moduleName ="";
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
}
#Override
public void onBackPressed() {
return;
}
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
return true;
}
return super.onKeyDown(keyCode, event);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_tabs_pager);
mTabHost = (TabHost)findViewById(android.R.id.tabhost);
mTabHost.setup();
Bundle bundle = this.getIntent().getExtras();
if(bundle.containsKey("name")) {
comingFrom = bundle.getString("name");
}
if(bundle.containsKey("module")) {
moduleName = bundle.getString("module");
}
mViewPager = (ViewPager)findViewById(R.id.pager);
mViewPager.setOffscreenPageLimit(1);
mTabsAdapter = new TabsAdapter(this, mTabHost, mViewPager);
mTabsAdapter.addTab(mTabHost.newTabSpec("Tab 1").setIndicator("Tab 1"),TabOneFragmentActivity.class, null);
mTabsAdapter.addTab(mTabHost.newTabSpec("Tab 2").setIndicator("Tab 2"),TabTwoFragmentActivity.class, null);
mTabsAdapter.addTab(mTabHost.newTabSpec("Tab 3").setIndicator("Tab 3"),TabThreeFragmentActivity.class, null);
mTabsAdapter.addTab(mTabHost.newTabSpec("Tab 4").setIndicator("Tab 4"),TabFoureFragmentActivity.class, null);
mTabsAdapter.addTab(mTabHost.newTabSpec("Tab 5").setIndicator("Tab 5"),TabFiveFragmentActivity.class, null);
if(bundle.containsKey("name") && comingFrom.equals("Tab_1Fragment")) {
mTabHost.setCurrentTab(3);
}else if(bundle.containsKey("name") && comingFrom.equals("Tab_2Fragment")) {
mTabHost.setCurrentTab(4);
}else if(bundle.containsKey("name") && comingFrom.equals("Tab_3Fragment")) {
mTabHost.setCurrentTab(0);
}else if(bundle.containsKey("name") && comingFrom.equals("Tab_4Fragment")) {
mTabHost.setCurrentTab(1);
}else {
mTabHost.setCurrentTab(0);
}
if (savedInstanceState != null) {
mTabHost.setCurrentTabByTag(savedInstanceState.getString("tab"));
}
}
#Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putString("tab", mTabHost.getCurrentTabTag());
}
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 Class<?> clss;
private final Bundle args;
TabInfo(String _tag, Class<?> _class, Bundle _args) {
clss = _class;
args = _args;
}
}
static class DummyTabFactory implements TabHost.TabContentFactory {
private final Context mContext;
public DummyTabFactory(Context context) {
mContext = context;
}
#Override
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);
}
#Override
public void onTabChanged(String tabId) {
int position = mTabHost.getCurrentTab();
mViewPager.setCurrentItem(position);
}
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
#Override
public void onPageSelected(int position) {
TabWidget widget = mTabHost.getTabWidget();
int oldFocusability = widget.getDescendantFocusability();
widget.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
widget.setDescendantFocusability(oldFocusability);
mTabHost.setCurrentTab(position);
}
#Override
public void onPageScrollStateChanged(int state) {
}
}
}

Fragment is not restoring its state in a viewpager when back is pressed and reopened

I am trying to keep a view pager inside a fragment. And the View pager itself contains 3 other fragment. This is the Root fragment which contain the view pager
import android.support.v13.app.FragmentPagerAdapter;
import android.support.v13.app.FragmentStatePagerAdapter;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import com.android.browser1.UI.ComboViews;
public class MyFragment extends Fragment implements
TabHost.OnTabChangeListener, ViewPager.OnPageChangeListener, CombinedBookmarksCallbacks {
private TabHost mTabHost;
private ViewPager mViewPager;
public static final String EXTRA_COMBO_ARGS = "combo_args";
Bundle bundle;
Bundle extra;
public static final String EXTRA_INITIAL_VIEW = "initial_view";
public static BookmarkFragment bookmarkFragmentForPageA = null;
public static BookmarkFragment bookmarkFragmentForPageB = null;
Controller controller;
TabsAdapter mTabsAdapter;
FirstFragment first;
SecondFragment seconde;
ThirdFragment third;
public void setBundle(Bundle bundle) {
extra = bundle;// getActivity().getIntent().getExtras();
this.bundle = extra.getBundle(EXTRA_COMBO_ARGS);
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
mTabHost.setup();
mViewPager.setOffscreenPageLimit(2);
mTabsAdapter = new TabsAdapter(getActivity(), mTabHost, mViewPager);
new setAdapterTask().execute();
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.bookmark_view_pager, null);
mTabHost = (TabHost) view.findViewById(R.id.tabHost);
mViewPager = (ViewPager) view.findViewById(R.id.viewpager);
view.setBackgroundColor(Color.BLACK);
return view;
}
private class setAdapterTask extends AsyncTask<Void, Void, Void> {
protected Void doInBackground(Void... params) {
return null;
}
#Override
protected void onPostExecute(Void result) {
String bookmark = getResources().getString(R.string.tab_bookmarks);
String history = getResources().getString(R.string.tab_history);
String scrapmemo = getResources().getString(R.string.tab_snapshots);
mTabsAdapter.addTab(
mTabHost.newTabSpec(bookmark).setIndicator(bookmark), BrowserBookmarksPage.class, null);
mTabsAdapter.addTab(
mTabHost.newTabSpec(history).setIndicator(history), BrowserHistoryPage.class, null);
mTabsAdapter.addTab(
mTabHost.newTabSpec(scrapmemo).setIndicator(scrapmemo), BrowserSnapshotPage.class, null);
String svStr = extra.getString(EXTRA_INITIAL_VIEW, null);
ComboViews startingView = svStr != null ? ComboViews.valueOf(svStr)
: ComboViews.Bookmarks;
switch (startingView) {
case Bookmarks:
mTabHost.setCurrentTab(0);
mViewPager.setCurrentItem(0);
break;
case History:
mTabHost.setCurrentTab(1);
mViewPager.setCurrentItem(1);
break;
case ScrapMemo:
mTabHost.setCurrentTab(2);
mViewPager.setCurrentItem(2);
break;
}
}
}
public void onTabChanged(String tag) {
ComboViews startingView = tag != null ? ComboViews.valueOf(tag)
: ComboViews.Bookmarks;
switch (startingView) {
case First:
mTabHost.setCurrentTab(0);
break;
case Second:
mTabHost.setCurrentTab(1);
break;
case Third:
mTabHost.setCurrentTab(2);
break;
}
}
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
#Override
public void onPageSelected(int position) {
this.mTabHost.setCurrentTab(position);
}
#Override
public void onPageScrollStateChanged(int state) {
}
public class TabsAdapter extends FragmentStatePagerAdapter
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>();
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;
}
}
class DummyTabFactory implements TabHost.TabContentFactory {
private final Context mContext;
public DummyTabFactory(Context context) {
mContext = context;
}
#Override
public View createTabContent(String tag) {
View v = new View(mContext);
v.setMinimumWidth(0);
v.setMinimumHeight(0);
return v;
}
}
public TabsAdapter(Activity activity, TabHost tabHost, ViewPager pager) {
super(activity.getFragmentManager());
mContext = activity;
mTabHost = tabHost;
mViewPager = pager;
mTabHost.setOnTabChangedListener(this);
mViewPager.setAdapter(this);
mViewPager.setOnPageChangeListener(this);
mViewPager.setOffscreenPageLimit(3);
}
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();
getItem(0);
}
#Override
public int getCount() {
return mTabs.size();
}
#Override
public Fragment getItem(int position) {
Fragment fr = null;
TabInfo info = mTabs.get(position);
// Create a new fragment if necessary.
switch (position) {
case 0:
fr = new FirstFragment();
fr.setArguments(info.args);
break;
case 1:
fr = new SecondFragment ();
fr.setArguments(info.args);
break;
case 2:
fr = new ThirdFragment ();
fr.setArguments(info.args);
break;
default:
fr = new FirstFragment ();
fr.setArguments(info.args);
break;
}
return fr;
}
#Override
public int getItemPosition(Object object) {
if (object instanceof FirstFragment
|| object instanceof SecondFragment
|| object instanceof ThirdFragment )
return POSITION_NONE;
return POSITION_UNCHANGED;
}
#Override
public void onTabChanged(String tabId) {
// called when the user clicks on a tab.
int position = mTabHost.getCurrentTab();
mViewPager.setCurrentItem(position);
}
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
#Override
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);
}
#Override
public void onPageScrollStateChanged(int state) {
}
}
}
And when I attach this fragment in my activity for the first time we can see all the three fragment by swiping but after the back press we remove the fragment. After that again I attach the fragment in the same activity at run time but only first 2 fragments are visible and the content of 3rd one is empty.
I am trying to keep a view pager inside a fragment. And the View pager itself contains 3 other fragment.
Sorry, but fragments inside of other fragments is officially not supported, as indicated by Dianne Hackborn, a core Android team member.

Categories

Resources