gpt4 book ai didi

java - 从 ViewPager Fragment 监听 DialogFragment dismiss 事件

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:18:27 28 4
gpt4 key购买 nike

有很多(重复的)问题和答案可用,我几乎经历了所有问题和答案,但都失败了。引用this问题,我最近做了一些改变。

简介:在我的应用程序中,MainActivity 包含 Fragment View Pager 和 FrangmentA、B 和 C。FrangmentA 在 Clik 上显示 DialogFragment CDialog。关闭 CDialog 后,我需要调用 FragmentA 的 doReload(),这在此处没有发生。

主要 Activity

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

fragment A

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

和对话

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()设置targetFragment:

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

然后调用此方法打开对话框 fragment :

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 的 onDismissListener() 中,加入如下代码:

@Override
public void onDismiss(DialogInterface dialog) {
super.onDismiss(dialog);

if (getTargetFragment() instanceof FragmentA)
((FragmentA) getTargetFragment()).doReload();

}

你这样做的是:

显示对话框 fragment “CDialog”并告诉它您的目标 fragment 是“FragmentA”,您可以使用它的引用以防您与它有关系。在您的情况下,您必须调用 doReload();

关于java - 从 ViewPager Fragment 监听 DialogFragment dismiss 事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47862837/

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