布局如图,外面是一个父fragment(即黑线部分,我们称它为fragment4),上面有两个按钮,点左边按钮fragment5,就会在下面content 布局处切换到fragment5的内容,如果点上面右边按钮fragment6,则会在content处显示fragment6 的内容。所以父类fragment4和fragment5之间怎么传递数据呢,fragment4 和fragment6之间怎么传递数据呢。
在frgment4页面添加changeFragment代码
public void changeFragment(int flag) {
flag2 = flag;
if (flag == FRAGMENT_FLAG1) {
binding.title.titleTab.setTabIndex(flag);
transactionsTab5Fragment = new TransactionsTab5Fragment();
transactionsTab6Fragment = null;
if (!transactionsTab5Fragment.isAdded()) {
Bundle bundle = new Bundle();
bundle.putString("Type", type);
transactionsTab5Fragment.setArguments(bundle);
getChildFragmentManager().beginTransaction().replace(R.id.content, transactionsTab5Fragment).commitAllowingStateLoss();
}
} else if (flag == FRAGMENT_FLAG2) {
binding.title.titleRight.setVisibility(View.VISIBLE);
binding.title.titleRightImg.setVisibility(View.VISIBLE);
binding.title.titleTab.setTabIndex(flag);
transactionsTab6Fragment = new TransactionsTab6Fragment();
transactionsTab5Fragment = null;
if (!transactionsTab6Fragment.isAdded()) {
Bundle bundle = new Bundle();
bundle.putString("Type", type);
transactionsTab6Fragment.setArguments(bundle);
getChildFragmentManager().beginTransaction().replace(R.id.content, transactionsTab6Fragment).commitAllowingStateLoss();
}
}
}
?主要代码:
Bundle bundle = new Bundle();
bundle.putString("Type", type);
transactionsTab5Fragment.setArguments(bundle);
在fragment5页面这样接收数据:
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
ctx = getActivity();
if (getArguments() != null) {
financeType = getArguments().getString("Type");
}
}
|