侦听DialogFragment从ViewPager Fragment中删除事件
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了侦听DialogFragment从ViewPager Fragment中删除事件相关的知识,希望对你有一定的参考价值。
有很多(重复的)问题和答案可用,我经历了几乎所有的问题并且失败了。关于this问题的参考,我最近做了一些改变。
简介:在我的应用程序中,MainActivity持有Fragment View Pager和FrangmentA,B和C. FrangmentA在Clik上显示DialogFragment CDialog。在解雇CDialog后,我需要调用FragmentA的doReload(),这不会发生在这里。
主要信息
protected void onCreate(Bundle savedInstanceState){
...
mSectionsPageAdapter = new FragmentAdapter(getSupportFragmentManager());
mViewPager = (ViewPager) findViewById(R.id.container);
setupViewPager(mViewPager);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(mViewPager);
int defaultValue = 0;
int page = getIntent().getIntExtra("One", defaultValue);
mViewPager.setCurrentItem(page);
}
private void setupViewPager(ViewPager viewPager)
{
FragmentAdapter adapter = new
FragmentAdapter(getSupportFragmentManager());
adapter.addFragment(new FragmentA(), "FragA");
adapter.addFragment(new FragmentB(), "FragB");
adapter.addFragment(new FragmentC(), "FragC");
viewPager.setAdapter(adapter);
}
片段
public class FragmentA extends Fragment implements CDialog.Dismissed{
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
...
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
FragmentManager fm = getActivity().getFragmentManager();
DialogFragment f = new CDialog();
f.show(fm, "CDialog");
}
});
@Override
public void dialogDismissed() {
Log.e(DFD_1, "dialogDismiss Called" );// <-- This is not working*
doReload();
}
}
和CDialogue
public class CDialog extends DialogFragment{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
....
return v;
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
...
dfd_1.setOnClickListener(
new View.OnClickListener() {
public void onClick(View v) {
getDialog().dismiss(); //<--when this happens*
}
});
}
@Override
public void onDismiss(DialogInterface dialog) {
if (getActivity() != null && getActivity() instanceof Dismissed) {
((Dismissed) getActivity()).dialogDismissed();
}
super.onDismiss(dialog);
}
public interface Dismissed {
public void dialogDismissed(); //<-- FragmentA implements this
}
}
您可以随时直接回调Fragment本身。
第一步,使用setTargetFragment()
设置目标片段:
DialogFragment#setTargetFragment(Fragment fragment, int requestCode);
我是这样做的:
public void showDialogFragment(Fragment targetFragment, AppCompatDialogFragment appCompatDialogFragment, int requestCode) {
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.setCustomAnimations(android.R.anim.fade_in, android.R.anim.fade_out);
appCompatDialogFragment.setTargetFragment(targetFragment, requestCode);
fragmentTransaction.add(appCompatDialogFragment, appCompatDialogFragment.getClass().getSimpleName());
fragmentTransaction.commitAllowingStateLoss();
}
然后调用此方法打开对话框片段:
public static final int RC_CDIALOG = 111;
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
showDialogFragment(FragmentA.this, new CDialog(), RC_CDIALOG);
}
});
然后,在你的DialogFragment qazxsw poi中,有一些代码如下:
onDismissListener()
你这样做的是:
显示Dialog片段“CDialog”并告诉它你的目标片段是“FragmentA”,你可以使用它的引用,因为你有与它有关的事情。在你的情况下你必须打电话给@Override
public void onDismiss(DialogInterface dialog) {
super.onDismiss(dialog);
if (getTargetFragment() instanceof FragmentA)
((FragmentA) getTargetFragment()).doReload();
}
以上是关于侦听DialogFragment从ViewPager Fragment中删除事件的主要内容,如果未能解决你的问题,请参考以下文章
DialogFragment是从Activity还是Fragment打开的?
通过 findFragmentByTag 从 FragmentManager 获取 DialogFragment