儿童片段到父母片段沟通

时间:2016-07-21 06:24:28

标签: android android-fragments

在我的孩子片段中,我有一个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

我哪里错了?

2 个答案:

答案 0 :(得分:6)

您必须使用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);
                }
            }
        });
    }
}

答案 1 :(得分:1)

var value = grid.Columns["Company"].CellValue(row)

这对我有用......