Child fragment to transmit parent fragment

In my child snippet, I have a Recyclerview, and I saved the Appbar layout in my parent snippet. When the first Recyclerview element is visible, I need to update the app bar layout in the parent fragment

My interface

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

in the class of the children's fragment

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 the parent fragment class

I implemented an 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 the 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?

+4
source share
2 answers

You must 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);
                }
            }
        });
    }
}
+6
source
ParentFragment parentFrag = ((ParentFragment)ChildFragment.this.getParentFragment());
parentFrag.YourParentFragmentMethod();

it worked for me ...

+1
source

Source: https://habr.com/ru/post/1648609/


All Articles