Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access ViewPager's Parent Fragment View in child fragment of Pager

I want to access a parent Fragment views in a child fragment which is attached with a View Pager. Means Parent Fragment contains a View Pager and View Pager then have a Fragment, and I want to use Parent Fragment Views in current Fragment of Pager.

Parent Fragment---->(Contains)View Pager------>(Contains)Child Fragment. Child Fragment wants to use Parent Fragment multiple views. Please suggest any solution regarding the same.

Thanks in advance.

like image 882
Sanat Pandey Avatar asked Sep 25 '14 13:09

Sanat Pandey


3 Answers

in your child fragment use:

ParentFragment frag = ((ParentFragment)this.getParentFragment());

and in your parent fragment you can store the references of your required views and use getter setter to access them, another solution is creating a listener interface. follow below link to find out how to implement:

http://developer.android.com/training/basics/fragments/communicating.html

like image 105
mmlooloo Avatar answered Sep 29 '22 11:09

mmlooloo


Inside Parent Fragment

     ViewPagerAdapter adapter = new ViewPagerAdapter(getChildFragmentManager())

make sure it is getChildFragmentManager()

and inside the child fragment, to access anything from parent fragment

     ((ParentFragment)this.getParentFragment()).any_thing_inside_parent_fragment;
like image 25
minato Avatar answered Sep 29 '22 11:09

minato


You can use the below code.

        FloatingActionButton fab =  getParentFragment().getView().findViewById(R.id.menu_item);
like image 31
Saurabh Singh Avatar answered Sep 29 '22 09:09

Saurabh Singh