3

I'm having some trouble adding a Fragment apart from the Fragments I have linked to each of my Tabs. In other words, I want to be able to swipe between Fragments with ViewPager, but I don't want the first Fragment to be one of the Tabs.

My Adpater for ViewPager and my Tabs is basically that of the example Google gives on the Developers website.

I've tried going ahead and adding the Fragment I want separate from the other in the TabHost as a tab and then setting the TabWidget visibility to GONE at position 0 and although that removes the TabWidget, when I swipe over to any other position the tab for position 0 will still be there. This method is a bit clunky too and I'd rather implement this in a nicer manner.

I'd share some code, but most of it is posted on the Developers website, but if I need to I will.

adneal
  • 30,484
  • 10
  • 122
  • 151
  • If you don't want to be able to swipe back to your first tab, why don't you just remove it from your adapter on the first `onTabChanged()` and then call `tabadapter.notifyDatasetChanged()`? – Reinier Mar 21 '12 at 14:27

1 Answers1

0

I think I have a similar thing going on in my app. I have a set of actionbar tabs, each being its own fragment. When you click on certain buttons within a tab, another fragment will be launched. Obviously you know all about the ViewPager, TabListener solution, etc. But my solution launches another fragment that fills the entire screen. When you press back (while focused on the new fragment), it returns you to where you were in the actionbar tab activity. Here's the code snippet that launches a new fragment:

public class EconFragment extends Fragment {

private ViewGroup container;
private TableLayout questionContainer;
private ScrollView scrollView;
private ViewGroup econFragment;
private View[] questions;
static int pos = 0;
private String[] titles = {"The first title ", "hallo1","hallo2", "hallo3",
        "hallo4", "hallo5","hallo6", "hallo7","hallo8", "hallo9"};

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    Log.d("Econ", "onCreateView");
    this.container = container;
    return inflater.inflate(R.layout.econfragment, container, false);
}

public OnClickListener chartsListener = new OnClickListener() {
    public void onClick(View v) {
        Intent chart = new Intent();
        chart.setClass(getActivity(), Chart.class);
        chart.putExtra("key", titles[v.getId()]);
        Log.v("TAG", Integer.toString(v.getId()));
        startActivity(chart);
    }
};

As you can see, I launch a new intent called chart. chart's class is then set as Chart - which extends fragment. Here's the stack overflow discussion that led me to this implementation: Start a fragment via Intent within a Fragment

Community
  • 1
  • 1
Davek804
  • 2,804
  • 4
  • 26
  • 55
  • Sorry, I forgot to mention something. My original implementation (in the SO link I sent) was trying to make Chart be a fragment. It's actually is a class that extends Activity. Given that I wanted to have it fill the screen, it turned out that making it extend Activity with the same xml file that I had intended for a chart fragment, the solution worked very well. – Davek804 Mar 23 '12 at 06:38