Fragmentation Tab open By Default 3rd tab - android

I have Create Fragment Tab where 6 Tab are available when we call this tab then it open by default first tab but we want to implement when we call this page then its by default open 3rd tab how bto resolve it please suggest.
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
tabHost = FindViewById<TabHost>(Android.Resource.Id.TabHost);
tabHost.Setup();
viewPager = FindViewById<ViewPager>(Resource.Id.pager);
tabsAdapter = new TabsAdapter(this, tabHost, viewPager);
tabsAdapter.AddTab(tabHost.NewTabSpec("Sale").SetIndicator("Sale"), Java.Lang.Class.FromType(typeof(retailerStockSaleList.PageFragment)), null);
tabsAdapter.AddTab(tabHost.NewTabSpec("L Y S M Sale").SetIndicator("L Y S M Sale"), Java.Lang.Class.FromType(typeof(retailerStockSale1List.PageFragment)), null);
tabsAdapter.AddTab(tabHost.NewTabSpec("L M Sale").SetIndicator("L M Sale"), Java.Lang.Class.FromType(typeof(retailerStockSaleLMList.PageFragment)), null);
tabsAdapter.AddTab(tabHost.NewTabSpec("Stock").SetIndicator("Stock"), Java.Lang.Class.FromType(typeof(retailerPhysicalStockList.PageFragment)), null);
tabsAdapter.AddTab(tabHost.NewTabSpec("Order List").SetIndicator("Order List"), Java.Lang.Class.FromType(typeof(retailerOrderList.PageFragment)), null);
tabsAdapter.AddTab(tabHost.NewTabSpec("Retailer Details").SetIndicator("Retailer Details"), Java.Lang.Class.FromType(typeof(retailerDetail.PageFragment)), null);
if (bundle != null)
{
tabHost.SetCurrentTabByTag(bundle.GetString("tab"));
}
}
protected override void OnSaveInstanceState(Bundle outState)
{
base.OnSaveInstanceState(outState);
outState.PutString("tab", tabHost.CurrentTabTag);
}
protected class TabsAdapter : FragmentPagerAdapter, TabHost.IOnTabChangeListener, ViewPager.IOnPageChangeListener
{
private Context _context;
private TabHost _tabHost;
private ViewPager _viewPager;
private List<TabInfo> _tabs = new List<TabInfo>();
public class TabInfo
{
public string tag;
public Class clss;
public Bundle args;
public Android.Support.V4.App.Fragment fragment { get; set; }
public TabInfo(string _tag, Class _class, Bundle _args)
{
tag = _tag;
clss = _class;
args = _args;
}
}
public class DummyTabFactory : Java.Lang.Object, TabHost.ITabContentFactory
{
private Context _context;
public DummyTabFactory(Context context)
{
_context = context;
}
public View CreateTabContent(string tag)
{
var v = new View(_context);
v.SetMinimumHeight(0);
v.SetMinimumWidth(0);
return v;
}
}
public TabsAdapter(FragmentActivity activity, TabHost tabHost, ViewPager pager)
: base(activity.SupportFragmentManager)
{
_context = activity;
_tabHost = tabHost;
_viewPager = pager;
_tabHost.SetOnTabChangedListener(this);
_viewPager.Adapter = this;
_viewPager.SetOnPageChangeListener(this);
}
public void AddTab(TabHost.TabSpec tabSpec, Class clss, Bundle args)
{
tabSpec.SetContent(new DummyTabFactory(_context));
var tag = tabSpec.Tag;
var info = new TabInfo(tag, clss, args);
_tabs.Add(info);
_tabHost.AddTab(tabSpec);
NotifyDataSetChanged();
}
public override int Count
{
get
{
return _tabs.Count;
}
}
public override Android.Support.V4.App.Fragment GetItem(int position)
{
var info = _tabs[position];
return Android.Support.V4.App.Fragment.Instantiate(_context, info.clss.Name, info.args);
}
public void OnTabChanged(string tabId)
{
int position ;
position = _tabHost.CurrentTab;
_viewPager.CurrentItem = position;
}
public void OnPageScrolled(int position, float positionOffset, int positionOffsetPixels)
{
}
public void OnPageSelected(int position)
{
var widget = _tabHost.TabWidget;
var oldFocusability = widget.DescendantFocusability;
widget.DescendantFocusability = DescendantFocusability.BlockDescendants;
_tabHost.CurrentTab = position;
widget.DescendantFocusability = oldFocusability;
}
}
}
}

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

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) {
}
}
}

black fragment content when changing tab back

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) {
}
}

Android create fragment more than one time

Using this sample: I have make my own Fragment that holds tabhost and tab content:
public class TabsFragment extends Fragment
{
private static final String EXTRA_TAB = "EXTRA_TAB";
private TabHost tabHost;
private TabManager tabManager;
private MaptrixFragmentActivity activity;
private Context context;
#Override
public void onAttach(Activity activity)
{
this.activity = (MaptrixFragmentActivity) activity;
this.context = activity;
super.onAttach(activity);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View view = inflater.inflate(R.layout.fragment_tabs, container, false);
tabHost = (TabHost)view.findViewById(android.R.id.tabhost);
tabHost.setup();
tabManager = new TabManager(activity, tabHost, R.id.realtabcontent);
initTabs();
if (savedInstanceState != null)
{
tabHost.setCurrentTabByTag(savedInstanceState.getString(EXTRA_TAB));
}
return view;
}
private void initTabs()
{
// tab one
addTab(Fragment.class, null, R.drawable.icon1, Res.S(R.string.tab1));
// I have not implement the tab fragment yet!
// tab two
addTab(Fragment.class, null, R.drawable.icon2, Res.S(R.string.tab2));
}
private void addTab(Class<?> fragmenClass, Bundle bundle, int drawableId, String tag)
{
TabHost.TabSpec spec = tabHost.newTabSpec(tag);
View tabIndicator = LayoutInflater.from(context).inflate(R.layout.item_tab, tabHost.getTabWidget(), false);
ImageView icon = (ImageView) tabIndicator.findViewById(R.id.tab_image);
icon.setImageResource(drawableId);
spec.setIndicator(tabIndicator);
tabManager.addTab(spec, fragmenClass, bundle);
}
#Override
public void onSaveInstanceState(Bundle outState)
{
super.onSaveInstanceState(outState);
outState.putString(EXTRA_TAB, tabHost.getCurrentTabTag());
}
public static class TabManager implements TabHost.OnTabChangeListener
{
private final FragmentActivity activity;
private final TabHost tabHost;
private final int containerID;
private final HashMap<String, TabInfo> tabs = new HashMap<String, TabInfo>();
TabInfo lastTab;
TabFactory factory;
public TabManager(FragmentActivity activity, TabHost tabHost, int containerID)
{
this.activity = activity;
this.tabHost = tabHost;
this.containerID = containerID;
factory = new TabFactory(activity);
tabHost.setOnTabChangedListener(this);
}
public void addTab(TabHost.TabSpec tabSpec, Class<?> clss, Bundle args)
{
tabSpec.setContent(factory);
String tag = tabSpec.getTag();
TabInfo info = new TabInfo(tag, clss, args);
info.fragment = activity.getSupportFragmentManager().findFragmentByTag(tag);
if (info.fragment != null && !info.fragment.isDetached())
{
FragmentTransaction ft = activity.getSupportFragmentManager().beginTransaction();
ft.detach(info.fragment);
ft.commit();
}
tabs.put(tag, info);
tabHost.addTab(tabSpec);
}
#Override
public void onTabChanged(String tabId)
{
TabInfo newTab = tabs.get(tabId);
if (lastTab != newTab)
{
FragmentTransaction ft = activity.getSupportFragmentManager().beginTransaction();
if (lastTab != null) {
if (lastTab.fragment != null)
{
ft.detach(lastTab.fragment);
}
}
if (newTab != null)
{
if (newTab.fragment == null)
{
newTab.fragment = Fragment.instantiate(activity, newTab.clss.getName(), newTab.args);
ft.add(containerID, newTab.fragment, newTab.tag);
}
else
{
ft.attach(newTab.fragment);
}
}
lastTab = newTab;
ft.commit();
}
}
}
private static class TabInfo
{
private final String tag;
private final Class<?> clss;
private final Bundle args;
private Fragment fragment;
TabInfo(String _tag, Class<?> _class, Bundle _args)
{
tag = _tag;
clss = _class;
args = _args;
}
}
private 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;
}
}
}
But onCreateView method calls two or more times when device changes orientation.
My FragmentActivity is:
public class MaptrixFragmentActivity extends FragmentActivity
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.factivity);
replace(R.id.factivity_content, new TabsFragment(), false, false);
}
#Override
protected void onResume()
{
super.onResume();
}
#Override
protected void onPause()
{
super.onPause();
}
public void replace(int rootView, Fragment fragment)
{
replace(rootView, fragment, true, true);
}
public void replace(int rootView, Fragment fragment, boolean backStack)
{
replace(rootView, fragment, backStack, true);
}
public void replace(int rootView, Fragment fragment, boolean backStack, boolean animation)
{
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
if (animation) transaction.setCustomAnimations(R.anim.fragment_open, R.anim.fragment_close, R.anim.fragment_open_stack, R.anim.fragment_close_stack);
if (backStack) transaction.addToBackStack(null);
transaction.replace(R.id.factivity_maptrix_content, fragment);
transaction.commit();
}
}
Why i have this bug?
You could perhaps add android:configChanges="keyboardHidden|orientation" to your AndroidManifest.xml file. This avoids recreating the activity when orientation changes and therefore the TabsFragment won't be created twice.
<activity android:name="MaptrixFragmentActivity" android:configChanges="keyboardHidden|orientation">
See this for more details.

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