Changing a ViewPager fragment from a child fragment

I have a ViewPager using a TabLayout with multiple fragments. I would like to click a button on one of the ViewPager fragments and direct the user to another tab / fragment using the tab name.

When the button is pressed, I need the TabLayout to change, and also display the snippet associated with this tab. I will also need to send additional data requesting a new fragment.

I do not have access to ViewPager setCurrentItem (int index). Ideally, I would like to contact the parent to complete the request.

+3
android android-fragments android-viewpager android-tablayout
source share
2 answers

You should write a method in the parent (containing ViewPager and sub-fragments) as follows:

public void setPagerFragment(int a) { pager.setCurrentItem(a); } 

This will set the current fragment in the ViewPager to the specified. Then you can call this method from the child fragment with:

 int newFrag = 0; //the number of the new Fragment to show ParentActivity parent = (ParentActivity) getActivity(); parent.setPagerFragment(newFrag); 

As for sending additional data with a request for showing on a new fragment, you can make another method in the parent, which should be called in the child element, which will set some data in the parent object, which then can use the parent element when setting up the new fragment.

For example, in the parent object:

 public void setExtraData(Object data) { dataFromChildFrag = data; } 

And use this in the child like this:

 String data = "My extra data"; //the number of the new Fragment to show ParentActivity parent = (ParentActivity) getActivity(); parent.setExtraData(data); 

Finally, if the parent is actually the fragment itself, and not the activity, just replace all the links:

 ParentActivity parent = (ParentActivity) getActivity(); 

in

 ParentFragment parent = (ParentFragment) getParentFragment(); 

Hope this helps!

+4
source share

the easiest way to achieve this using the EventBus framework!

I prefer (ed) with EventBus from greenRobot

How to implement:

1) Create an event class that will satisfy your needs.

 public class ClickedButtonInsideFragmentEvent { // some data you want to store } 

2) Prepare subscribers! In your case, it will be Activity, which contains a link to the tab layout:

 public class MyTabActivity { public void onCreate(Bundle savedInstanceSate) { // your stuff you do in onCreate eventBus.register(this); } @Subscribe public void onEvent(ClickedButtonInsideFragmentEvent event) { // Do what you want to do } } 

3) and finally: post the event from your OnClickListener insider of your fragment:

 public class MyClickableFragment { public void initOnClickListiner(View clickableView) { clickableView.setOnClickListener(new OnClickListener() { public void onClick(View view) { ClickedButtonInsideFragmentEvent event = new ClickedButtonInsideFragmentEvent(); // add what you want to your event eventBus.post(event); } )); } @Subscribe public void onEvent(ClickedButtonInsideFragmentEvent event) { // Do what you want to do } } 
0
source share

All Articles