Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android getting fragment that is in FragmentPagerAdapter

I have following problem: I have one activity in which I have two tabs which are made both as fragments using FragmentPagerAdapter In some moment I would like to change View of one of these fragments, but I am not able to findFragmentById() or findFragmentByTag() I am unable to set id, because I am not declaring Fragment in XML, because of the FragmentPagerAdapter And when I am trying to set tag I am getting following problem:

08-15 21:46:23.805: E/AndroidRuntime(9297): java.lang.IllegalStateException: Can't  change tag of fragment Companies{416d7b78 id=0x7f04002e companies}: was companies now android:switcher:2130968622:1 

My code of the pager FragmentPagerAdapter:

public class TabsAdapter extends FragmentPagerAdapter implements    ActionBar.TabListener, ViewPager.OnPageChangeListener {     private final FragmentActivity mContext;     private final ActionBar mActionBar;     private final ViewPager mViewPager;     private final ArrayList<TabInfo> mTabs = new ArrayList<TabInfo>();       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) {         super(activity.getSupportFragmentManager());         mContext = activity;         mActionBar = activity.getSupportActionBar();         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);         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);         FragmentTransaction ft=mContext.getSupportFragmentManager().beginTransaction();         Fragment fragment=Fragment.instantiate(mContext, info.clss.getName(), info.args);          if(position==1){             ft.add(mViewPager.getId(), fragment, "companies");         }else{             ft.add(mViewPager.getId(), fragment);         }         Log.v("FRAGMENT ID", "fragment tag: "+fragment.getTag());         ft.commit();         Log.v("FRAGMENT ID", "fragment tag: "+fragment.getTag());         return fragment;     }      @Override     public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {     }      @Override     public void onPageSelected(int position) {         mActionBar.setSelectedNavigationItem(position);         new Thread(new Runnable() {              @Override             public void run() {                 MainActivity.this.hideKeyboard();              }         }).start();      }      @Override     public void onPageScrollStateChanged(int state) {     }      @Override     public void onTabSelected(Tab tab, FragmentTransaction ft) {         Object tag = tab.getTag();         for (int i = 0; i < mTabs.size(); i++) {             if (mTabs.get(i) == tag) {                 mViewPager.setCurrentItem(i);                                }         }       }      @Override     public void onTabUnselected(Tab tab, FragmentTransaction ft) {       }      @Override     public void onTabReselected(Tab tab, FragmentTransaction ft) {     } } 

Thank you in advance for your answers.

like image 894
ziky90 Avatar asked Aug 15 '12 20:08

ziky90


People also ask

How do you call ViewPager fragment from activity?

You'll just need to cast it to the real fragment class if you want to call a specific method. int pos = viewpager. getCurrentItem(); Fragment activeFragment = adapter. getItem(pos); if(pos == 0) ((NPListFragment)activeFragment).

How do I refresh FragmentPagerAdapter?

You can use startActivityForResult(or broadcast/receiver) to get the result from the activity. Then use adapter. notifyDataSetChanged to refresh the fragment.

How do you know when a fragment becomes visible?

fragment:fragment:1.1. 0 you can just use onPause() and onResume() to determine which fragment is currently visible for the user. onResume() is called when the fragment became visible and onPause when it stops to be visible. To enable this behavior in the first ViewPager you have to pass FragmentPagerAdapter.


2 Answers

The Fragments supplied by the FragmentPagerAdapter are auto-tagged when they're instantiated. You can retrieve the tag with this method:

private static String makeFragmentName(int viewPagerId, int index) {      return "android:switcher:" + viewPagerId + ":" + index; } 

Reference: reusing fragments in a fragmentpageradapter

like image 188
DeeV Avatar answered Oct 06 '22 00:10

DeeV


With this function you can find the fragment by pager adapter position.

public Fragment findFragmentByPosition(int position) {     FragmentPagerAdapter fragmentPagerAdapter = getFragmentPagerAdapter();     return getSupportFragmentManager().findFragmentByTag(             "android:switcher:" + getViewPager().getId() + ":"                     + fragmentPagerAdapter.getItemId(position)); } 

Sample code for v4 support api.

like image 21
Daniel De León Avatar answered Oct 06 '22 00:10

Daniel De León