Hiren Nakrani
Hiren Nakrani

Reputation: 244

Nested Fragment Communication

I want to communicate between parent and child fragment. I have read so many posts on SO but none of the posts seem to be useful for me.

I have 3 Fragments FragmentA, FragmentB and FragmentC.

FragmentA is a parent fragment for FragmentB and FragmentB is parent Fragment of FragmentC.

FragmentA has EditText with TextWatcher and a viewpager with tabs implemented. FragmentB has another Viewpager with Tabs.(So basically I am using Nested tab which is not recommended but I really need to use this scenario). FragmentC has recyclerView which then shows a list of products based on selected Tabs. this works fine for me.

Now, I want to add search functionality for products in FragmentC (Search is performed in webservice and webservice for product listing is called from FragmentC).

To implement this What I have done so far is, FragmentA will setup viewpager based on web response and here is a method to setup viewpager in FragmentA.(The Code refers to how I get the instance of Fragment)

 private void setupViewPager(ViewPager viewPager, RetailCategoryListArr retailCategoryListArr) {
        ViewPagerAdapter adapter = new ViewPagerAdapter(getChildFragmentManager());
        for (int i = 0; i < retailCategoryListArr.getData().size(); i++) {
            RetailerCategoryArr retailerCategoryArr = retailCategoryListArr.getData().get(i);
            FragmentB FragB= FragmentB.newInstance(retailerCategoryArr, retailerArr.getUrlIdentifier());
            adapter.addFragment(FragB, retailerCategoryArr.getName());
        }
        viewPager.setAdapter(adapter);
        tabs.setupWithViewPager(viewPager);
    }

FragmentB has another ViewPager which then implemented like this.

private void setUpViewPager(ViewPager viewpager) {
        ViewPagerAdapter adapter = new ViewPagerAdapter(getChildFragmentManager());

        for (int i = 0; i < retailerCategoryArr.getChild().size(); i++) {
            RetailerCategoryArr.RetailerSubCategoryArr retailerSubCategoryArr = retailerCategoryArr.getChild().get(i);
            FragmentC FragC = FragmentC.newInstance(retailerSubCategoryArr.getId(), retailer);
            adapter.addFragment(FragC, retailerSubCategoryArr.getName());

        }
        viewpager.setAdapter(adapter);
        tabs.setupWithViewPager(viewpager);
    }

So, the question is how can I listen the textchange of Edittext of FragmentA in FragmentC? If this can be possible by Interface then How?

Upvotes: 1

Views: 246

Answers (2)

Nirav Shah
Nirav Shah

Reputation: 263

You can make interface and implements in Parent Activity to listen textwatcher from fragment A.

 interface Test{
      void setTextChange(String ch);
    }

In Parent Activity you implement listener

public class FragmenA extends Fragment{
    Test mCallback;
    public Edittext;


    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);

        // This makes sure that the container activity has implemented
        // the callback interface. If not, it throws an exception
        try {
            mCallback = (Test) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString()
                    + " must implement Test");
        }
    }


}



 public class MainActivity extends Activity
                implements FragmentA.Test{

               Fragment fragmentc=getFragmentManager().findFragmentByTag(FragmentC.class);



            public setTextChange(String ch) {
                     if(fragmentc!=null){

                      fragmentc.onChangeText(ch);
        }
            }
        }


        public class FragmenC extends Fragment{
            Test mCallback;


            public void onChangeText(String text){
        }


        }

Upvotes: 0

Alex Nik
Alex Nik

Reputation: 763

From FragmentC try to do this:

Fragment frag = getParentFragment(); //it will be FragmentB
FragmentA fragA = (FragmentA)frag.getParentFragment();
fragA.editText.addTextChangedListener(new TextWatcher() {
    ...
});

Upvotes: 1

Related Questions