Update FragmentPagerAdapter from an inside Fragment - android

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

Related

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().

force ViewPager to call getItem when going up the backstack

I'm trying to create an Android application which contains a single activity with a container and a navigation drawer. The initialy empty container loads fragments which has a ViewPager inside a tab layout in which I load a frgment with a FragmentTransaction:
public static void replaceFragmentInContainer(FragmentManager fragmentManager, Fragment fragmentToShow,
boolean addToBackStack)
{
FragmentTransaction transaction = fragmentManager.beginTransaction();
if (addToBackStack)
{
transaction.addToBackStack(null);
}
transaction.replace(R.id.container, fragmentToShow);
transaction.commit();
}
The problem I'm facing is with the backstack. When a fragment is added to the backstack and I press the back button, the app does go back to the previous fragment like I want to and I do see the tabs layout itself, but the content of the tabs is empty like there was nothing loaded to that tab. When it happens, I manage to reload the tab's content only when choosing that screen again with the navigational drawer.
After debugging I saw that the pager adapter's getItem method is not getting called when pressing the back button. The adapter itself is FragmentStatePagerAdapter.
I tried overriding the getItemPosition method:
public int getItemPosition(Object object)
{
return POSITION_NONE;
}
but that method wasn't called either when pressing the back button so I end up seeing empty tabs.
And this is the tabs adapter which is also the ViewPager adapter:
public static 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>();
public TabsAdapter(final FragmentActivity activity, final TabHost tabHost, final ViewPager pager)
{
super(activity.getSupportFragmentManager());
mContext = activity;
mTabHost = tabHost;
mViewPager = pager;
mTabHost.setOnTabChangedListener(this);
mViewPager.setAdapter(this);
mViewPager.setOnPageChangeListener(this);
}
public ViewPager getViewPager()
{
return mViewPager;
}
static final class TabInfo
{
private final String tag;
private final Class<?> clss;
private final Bundle args;
TabInfo(final String _tag, final Class<?> _class, final Bundle _args)
{
tag = _tag;
clss = _class;
args = _args;
}
}
static class TabFactory implements TabHost.TabContentFactory
{
private final Context mContext;
public TabFactory(final Context context)
{
mContext = context;
}
#Override
public View createTabContent(final String tag)
{
final View v = new View(mContext);
v.setMinimumWidth(0);
v.setMinimumHeight(0);
return v;
}
}
public void addTab(final TabHost.TabSpec tabSpec, final Class<?> clss, final Bundle args)
{
tabSpec.setContent(new TabFactory(mContext));
final String tag = tabSpec.getTag();
final 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(final int position)
{
final TabInfo info = mTabs.get(position);
return Fragment.instantiate(mContext, info.clss.getName(), info.args);
}
// #Override
// public Object instantiateItem(ViewGroup container, int position)
// {
//
// // Inflate a new layout from the resources.
// View view = ((Activity) mContext).getLayoutInflater().inflate(
// R.layout.fragment_single_conversation, container, false);
// // Add the newly created View to the ViewPager
// container.addView(view);
//
// // Populate the GUI and views now
// return view;
// }
#Override
public int getItemPosition(Object object)
{
return POSITION_NONE;
}
#Override
public void onTabChanged(final String tabId)
{
final int position = mTabHost.getCurrentTab();
mViewPager.setCurrentItem(position);
}
#Override
public void onPageScrolled(final int position, final float positionOffset,
final int positionOffsetPixels)
{
}
#Override
public void onPageSelected(final 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.
final TabWidget widget = mTabHost.getTabWidget();
final int oldFocusability = widget.getDescendantFocusability();
widget.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
mTabHost.setCurrentTab(position);
widget.setDescendantFocusability(oldFocusability);
}
#Override
public void onPageScrollStateChanged(final int state)
{
}
}
How can this issue be solved?

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

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 TabsAdapter handle fragments

I want to handle one fragment within tabsadapter which is a static class in my Activity. The problem is how can i get the getfragmentbytag in the onTabSelected?
From the onCreate i have this code:
Tab tab1 = actionBar.newTab().setText("ABOUT");
tabsAdapter.addTab(tab1, PoiAboutFragment.class, null);
and my tabs adapter class is the following which works with no problem.
public static class TabsAdapter extends FragmentPagerAdapter implements
ActionBar.TabListener, ViewPager.OnPageChangeListener {
private final Context mContext;
private final ActionBar mActionBar;
private final ViewPager mViewPager;
private final ArrayList<TabInfo> mTabs = new ArrayList<TabInfo>();
private Object gridTag;
static final class TabInfo {
private final Class<?> clss;
private final Bundle args;
TabInfo(Class<?> _class, Bundle _args) {
clss = _class;
args = _args;
}
}
public TabsAdapter(SherlockFragmentActivity activity, ViewPager pager,
ActionBar actionBar) {
super(activity.getSupportFragmentManager());
mContext = activity;
mActionBar = actionBar;
mViewPager = pager;
mViewPager.setAdapter(this);
mViewPager.setOnPageChangeListener(this);
}
public void addTab(ActionBar.Tab tab, Class<?> clss, Bundle args) {
TabInfo info = new TabInfo(clss, args);
tab.setTag(info);
if(clss.equals(GridFragment.class))
gridTag = tab.getTag();
tab.setTabListener(this);
mTabs.add(info);
mActionBar.addTab(tab);
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 onPageScrolled(int position, float positionOffset,
int positionOffsetPixels) {
}
public void onPageSelected(int position) {
mActionBar.setSelectedNavigationItem(position);
}
public void onPageScrollStateChanged(int state) {
}
public void onTabSelected(Tab tab, FragmentTransaction ft) {
Object tag = tab.getTag();
Log.i(TAG, "onTabSelected has been called");
if(tag.equals(gridTag))
Log.i(TAG, "gridtab has been called");
for (int i = 0; i < mTabs.size(); i++) {
if (mTabs.get(i) == tag) {
mViewPager.setCurrentItem(i);
}
}
}
the problem is that i want to handle one of my fragments in both the oncreate method and in the tabsAdapter but i don't have neither a fragment tag neither an fragment Id. How can i handle their methods from my Activity?
The FragmentPagerAdapter source's makeFragmentName methods shows that fragments are created with the following tags:
"android:switcher:" + viewId + ":" + id
Where viewId is the id of the ViewPager, i.e., use
activity.getSupportFragmentManager().findFragmentByTag(
"android:switcher:" + pager.getId() + ":0"
to get the first Fragment.
I found the answer I was looking for in this site android-er.blogasppot

Categories

Resources