儿童片段到父母片段沟通

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了儿童片段到父母片段沟通相关的知识,希望对你有一定的参考价值。

在我的孩子片段中,我有一个Recyclerview,我的父片段中有一个Appbar布局。当Recyclerview第一个项目可见时,我需要更新父片段中的appbar布局

我的界面

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

在Child 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);

        }

    }

在Parent Fragment类中

我实现了界面

public class MyParentFragment extends Fragment implements OnListFirstItemVisibleListener{

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

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

}

但我得到Appbar布局为null

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

我哪里错了?

答案

您必须使用ChildFragmentManager。你父母应该是这样的:

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);
    }

}

而你的孩子喜欢这样:

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);
                }
            }
        });
    }
}
另一答案
ParentFragment parentFrag = ((ParentFragment)ChildFragment.this.getParentFragment());
parentFrag.YourParentFragmentMethod();

这对我有用......

以上是关于儿童片段到父母片段沟通的主要内容,如果未能解决你的问题,请参考以下文章

从片段中调用父母的活动

text 片段沟通

将触摸传递给下的视图

将片段添加到片段中(嵌套片段)

如何正确地将多个片段添加到片段过渡?

从父片段到选项卡片段的接口侦听器不起作用