fanf
fanf

Reputation: 120

Android - Multiple fragments based on the same class

I am developping an Android application in which I need to have multiple fragments based on the same class inside a FragmentActivity. For now, my code is mostly based on this example from the official doc, except I instantiate the same class for each fragment:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.fragment_tabs_pager);
    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("tab1").setIndicator("Tab 1"), MyFragment.class, null);
    mTabsAdapter.addTab(mTabHost.newTabSpec("tab2").setIndicator("Tab 2"), MyFragment.class, null);
    mTabsAdapter.addTab(mTabHost.newTabSpec("tab3").setIndicator("Tab 3"), MyFragment.class, null);
    mTabsAdapter.addTab(mTabHost.newTabSpec("tab4").setIndicator("Tab 4"), MyFragment.class, null);

    if (savedInstanceState != null) {
        mTabHost.setCurrentTabByTag(savedInstanceState.getString("tab"));
    }
}

The class MyFragment is pretty simple:

public class MyFragment extends Fragment {

    View v;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        if (container == null) {
                return null;
        }

        v = inflater.inflate(R.layout.test, container, false);
        return v;
    }
}

I simplified the layout inflated by the Fragment class to a simple TextView:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">

<TextView
    android:id="@+id/test_textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/some_default_value"
    android:textAppearance="?android:attr/textAppearanceLarge" />

</LinearLayout>

So, the thing is, I would like to ba able to modify the value of test_textView1 separately in each fragment, after they have been instantiated. My problem is that I don't manage to access to the elements of the layout of a fragment. I cannot directly use the findViewById() method since it would affect all the fragments. Inside the TabsAdapter class, I tried to access a single fragment, and then access its View by doing the following:

getItem(number).getView();

but it returns null. (getItem() returns a correct fragment but getView() returns null).

I also read this solution, but if I declare a TextView in my Fragment, add a method setTextView(String s) and call it from the FragmentActivity it generates a NullPointerException.

So, is there a specific way to separately access layout items in my fragments? Thanks!

Upvotes: 2

Views: 6316

Answers (3)

fanf
fanf

Reputation: 120

Ok finally I started all over again and based my code on this, and it worked.

Upvotes: 0

Tushar
Tushar

Reputation: 8049

If you have fragments: frag1, frag2, frag3, etc. you can use frag1.getView().findViewById() to limit the scope to just that fragment since each fragment extends the same class.

Example:

TextView frag1text = (TextView)frag1.getView().findViewById();

Upvotes: 2

Warpzit
Warpzit

Reputation: 28152

In onCreateView of your fragments remember to save a pointer to the view. Then you'll always be able to say myPointer.findViewById(); in order to get a subitem of your view.

Upvotes: 0

Related Questions