gpt4 book ai didi

android - 子 fragment 到父 fragment 的通信

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:46:16 29 4
gpt4 key购买 nike

在我的子 Fragment 中,我有一个 Recyclerview,在我的父 Fragment 中有一个 Appbar 布局。当 Recyclerview 的第一个项目可见时,我需要更新父 fragment 中的应用栏布局

我的界面

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

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

}

}

在父 fragment 类中

我实现了接口(interface)

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。你的 parent 应该看起来像这样:

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

}

你的 child 是这样的:

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

关于android - 子 fragment 到父 fragment 的通信,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38496442/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com