Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Child Fragment to Parent Fragment communication

In my child Fragment, I have a Recyclerview, and I haved an Appbar layout in my parent Fragment. When the Recyclerview's first item is visible, I need to update the appbar layout in my parent fragment

My interface

public interface OnListFirstItemVisibleListener {
    public void sendDataToFragmentOnFirstItemVisible(boolean data, int dy);
}

in Child Fragment class

public class MyChildFragment extends Fragment{
private OnListFirstItemVisibleListener mListFirstItemVisibleListener;
.............
  @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        mListFirstItemVisibleListener=(OnListFirstItemVisibleListener) new ParentFragment();

        return view;
    }
 @Override
    public void onScrolled(RecyclerView recyclerView, int dx, int dy) {

        if(mLinearLayoutManager.findFirstCompletelyVisibleItemPosition()==0&&mListFirstItemVisibleListener != null){
            mListFirstItemVisibleListener.sendDataToFragmentOnFirstItemVisible(true,dy);
        }else{
            mListFirstItemVisibleListener.sendDataToFragmentOnFirstItemVisible(false,dy);

        }

    }

In Parent Fragment class

I implemented the interface

public class MyParentFragment extends Fragment implements OnListFirstItemVisibleListener{

............

@Override
    public void sendDataToFragmentOnFirstItemVisible(boolean data, int dy) {
        if (dy < 0&&data==true)
            mAppBarLayout.setExpanded(true);
    }

}

But I get Appbar layout as null

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.design.widget.AppBarLayout.setExpanded(boolean)' on a null object

Where am I going wrong?

like image 592
MrRobot9 Avatar asked Jul 21 '16 06:07

MrRobot9


People also ask

Could two fragments can communicate directly?

To keep fragments self-contained, you should not have fragments communicate directly with other fragments or with its host activity. The Fragment library provides two options for communication: a shared ViewModel and the Fragment Result API. The recommended option depends on the use case.

How can ViewModel communicate with fragments and activity?

To allow a Fragment to communicate up to its Activity , you can define an interface in the Fragment class and implement it within the Activity . The Fragment captures the interface implementation during its onAttach() lifecycle method and can then call the Interface methods in order to communicate with the Activity .

How do you pass data between bundles using fragments?

To pass data between fragments in the same fragment manager, the listener should be added to the destination fragment with requestKey in order to receive the result produces from another fragment with the same key.


2 Answers

You have to use the ChildFragmentManager. You Parent should look like this:

public class ParentFragment extends Fragment {

    private TextView mActionBar;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_parent, container, false);
        mActionBar = (TextView) view.findViewById(R.id.actionBar);
        return view;
    }

    @Override
    public void onResume() {
        super.onResume();
        getChildFragmentManager().beginTransaction().add(R.id.childHolder, new ChildFragment()).commit();
    }

    public void setActionBarHidden(boolean isHidden) {
        mActionBar.setVisibility(isHidden ? GONE : VISIBLE);
    }

}

And your child like this:

public class ChildFragment extends Fragment {

    public ChildFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_child, container, false);
        mRecyclerView = (RecyclerView) view.findViewById(R.id.recyclerView);
        mLinearLayoutManager = new LinearLayoutManager(getActivity());
        mRecyclerView.setLayoutManager(mLinearLayoutManager);
        mRecyclerView.setAdapter(new MyAdapter(mDataSet));

        return view;
    }

    @Override
    public void onResume() {
        super.onResume();

        mParentFragment = (ParentFragment) getParentFragment();
        mRecyclerView.setOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                if (mLinearLayoutManager.findFirstCompletelyVisibleItemPosition() == 0 && mParentFragment != null) {
                    mParentFragment.setActionBarHidden(true);
                } else {
                    mParentFragment.setActionBarHidden(false);
                }
            }
        });
    }
}
like image 181
Alex Walger Avatar answered Sep 28 '22 16:09

Alex Walger


ParentFragment parentFrag = ((ParentFragment)ChildFragment.this.getParentFragment());
parentFrag.YourParentFragmentMethod();

this is worked for me...

like image 40
Pankaj Talaviya Avatar answered Sep 28 '22 17:09

Pankaj Talaviya