0

I have a FirstFragment with viewPager (ViewPager contains several TestFragments). FirstFragment also contains a button. I would like to change UI of current TestFragment when I click the button.

Unfortunately when I call getView() or try to access the stored property (View rootView). They both return null.

How should I approach this?

class FirstFragment extends Fragment{
    private ViewPager mViewPager;
    private TestAdapter testAdapter;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.first_layout, container, false);
        testAdapter = new TestAdapter(getFragmentManager());
        mViewPager = (ViewPager) view.findViewById(R.id.pager);
        mViewPager.setAdapter(testAdapter);

        mViewPager.setAdapter(questionPagerAdapter);
        view.findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ((TestFragment)testAdapter.getItem(mViewPager.getCurrentItem())).doSomethingOnFragmentUI();
            }
        });
        return view;
    }
}


class TestFragment extends Fragment{
    private View rootView;
    @Override
    public View onCreateView(LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.my_layout, container, false);
        return rootView;
    }
    public void doSomethingOnFragmentUI(){
        if(rootView != null){
            //rootView is always null at this point
        }
    }
}

Please note that viewPager creates a new fragment everyting .getItem() is called. I do not cache the fragments. This problem occured when I stopped "caching" the fragments in HashMap (FirstFragment was keeping the reference).

Vojtech B
  • 2,837
  • 7
  • 31
  • 59
  • `TestAdapter(getFragmentManager());` here you must use `getChildFragmentManager()` – einschnaehkeee Feb 01 '15 at 09:35
  • And you never should call getItem() of your PagerAdapter. It is always called by Android, not by you. If you want more control over your Fragments inside a ViewPager and call them from outside, use this http://stackoverflow.com/a/15261142/535762 – einschnaehkeee Feb 01 '15 at 09:40
  • Thank you. This resolved the issue. If you crate an answer I will accept it – Vojtech B Feb 01 '15 at 12:03

1 Answers1

1

Just so you can have a resolved question:

  1. You need to call getChildFragmentManager() when working with Fragments inside Fragments.
  2. Never invoke getItem() inside a PagerAdapter, unless you precisely know what you're doing. It's meant to be called by the system, not by you. If you really want to invoke your Fragments in your Pager from outside, you can use this approach: https://stackoverflow.com/a/15261142/535762

Hope that covers all. :-)

Community
  • 1
  • 1
einschnaehkeee
  • 1,858
  • 2
  • 17
  • 20