Note that there are some explanatory texts on larger screens.

plurals
  1. POActionBarSherlock, ViewPager, TabsAdapter nested Tab Fragments
    text
    copied!<p>I implemented a ActionBarSherlock with a ViewPager and a TabsAdapter. It works well but now I tried to "push" from a Framework Loaded in "Tab 1" another Fragment.</p> <p>The behavior should be like:</p> <ul> <li>I've 3 Tabs in my Application, When launching I see the first Tab where a Fragment with a Button is in it</li> <li>Pressing the Button on the First Tab, the Fragment on the First Tab should be replaced and another Fragment should be displayed</li> </ul> <p>I tried to implement this, but I've a black Fragment when Replacing.</p> <p>The Code:</p> <p><strong>FragmentDemoActivity.java</strong></p> <pre><code>public class FragmentDemoActivity extends SherlockFragmentActivity { CustomViewPager mViewPager; TabsAdapter mTabsAdapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); ActionBar bar = getSupportActionBar(); bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); bar.setDisplayOptions(0, ActionBar.DISPLAY_SHOW_TITLE); mViewPager = (CustomViewPager)findViewById(R.id.pager); mViewPager.setSwipingEnabled(false); mTabsAdapter = new TabsAdapter(this, bar, mViewPager); mTabsAdapter.addTab(bar.newTab().setText("tab1"), FragmentA.class, null); mTabsAdapter.addTab(bar.newTab().setText("tab2"), FragmentB.class, null); setTitle(R.string.app_name); if (savedInstanceState != null) { bar.setSelectedNavigationItem(savedInstanceState.getInt("tab")); } } @Override protected void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); outState.putInt("tab", getSupportActionBar().getSelectedNavigationIndex()); } public static class TabsAdapter extends FragmentPagerAdapter implements ViewPager.OnPageChangeListener, ActionBar.TabListener { private final Context mContext; private final ActionBar mBar; private final ViewPager mViewPager; private final ArrayList&lt;TabInfo&gt; mTabs = new ArrayList&lt;TabInfo&gt;(); static final class TabInfo { private final Class&lt;?&gt; clss; private final Bundle args; TabInfo(Class&lt;?&gt; _class, Bundle _args) { clss = _class; args = _args; } } public TabsAdapter(FragmentActivity activity, ActionBar bar, ViewPager pager) { super(activity.getSupportFragmentManager()); mContext = activity; mBar = bar; mViewPager = pager; mViewPager.setAdapter(this); mViewPager.setOnPageChangeListener(this); } public void addTab(ActionBar.Tab tab, Class&lt;? extends Fragment&gt; clss, Bundle args) { TabInfo info = new TabInfo(clss, args); tab.setTag(info); tab.setTabListener(this); mTabs.add(info); mBar.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); } @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { } @Override public void onPageSelected(int position) { mBar.setSelectedNavigationItem(position); } @Override public void onPageScrollStateChanged(int state) { } @Override public void onTabSelected(Tab tab, FragmentTransaction ft) { Object tag = tab.getTag(); for (int i=0; i&lt;mTabs.size(); i++) { if (mTabs.get(i) == tag) { mViewPager.setCurrentItem(i,false); } } } @Override public void onTabUnselected(Tab tab, FragmentTransaction ft) { } @Override public void onTabReselected(Tab tab, FragmentTransaction ft) { } } } </code></pre> <p><strong>FragmentA.java</strong></p> <pre><code>public class FragmentA extends Fragment { Button button; int num; @Override public View onCreateView(LayoutInflater inflater, ViewGroup group, Bundle saved) { return inflater.inflate(R.layout.frag_a, group, false); } @Override public void onActivityCreated (Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); num = 0; button = (Button) getActivity().findViewById(R.id.button1); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { FragmentB f1 = new FragmentB(); FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction(); ft.replace(R.id.pager, f1,"newTag"); ft.show(f1); ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN); ft.addToBackStack(null); ft.commit(); } }); } } </code></pre> <p>I'm new at Android Development, so I'd be happy if someone is able to help me. I already investigated a lot of time to search a solution but the result wasn't as I expected.</p> <p>My current project: <a href="http://cl.ly/3q1f2t1O2Y3j" rel="noreferrer">http://cl.ly/3q1f2t1O2Y3j</a></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload