嵌套片段如何从内部片段调用到外部片段(作为父子关系)

时间:2019-01-17 12:12:13

标签: android android-fragments

我有一个Fragment,其中包含TextViewEditTextImageViewFramelayout。在Framelayout的{​​{1}}中,我使用以下代码将onCreate()替换为Framelayout

Fragment

现在我得到一个public class OneFragment extends Fragment { private static final String TAG = "OneFragment"; public static final String DATA = "SalesData"; private ViewGroup container; private LayoutInflater inflater; private MyData myData; @BindView(R.id.start_task) Button start_task; @BindView(R.id.easy_flip) EasyFlipView easyFlipView; @BindView(R.id.frame_container) FrameLayout frameLayout; BackFragment blankFragment = new BackFragment(); @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { this.container = container; this.inflater = inflater; return initializeView(); } private View initializeView() { View view = inflater.inflate( R.layout.view_pager_fragment, container, false); ButterKnife.bind(this, view); if (getArguments() != null) { myData = (MyData)getArguments().getSerializable(DATA); } final FragmentTransaction ft = getFragmentManager().beginTransaction(); ft.replace(R.id.frame_container, blankFragment, "NewFragmentTag"); ft.commit(); Button back_task =blankFragment.getView().findViewById(R.id.back_task); back_task.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { easyFlipView.flipTheView(); } }); start_task.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { easyFlipView.flipTheView(); } }); return view; } public void flipView(){ easyFlipView.flipTheView(); } } ,说找不到来自RuntimeExpcetion的视图。此BlankFragment上有一个单击的按钮,我需要呼叫BlankFragment。我应该怎么做。

1 个答案:

答案 0 :(得分:1)

创建回调接口并在OneFragment类中实现它,并从BackFragment的onclick侦听器调用回调的方法