听DialogFragment解雇事件从ViewPager片段

有很多(重复)的问题和答案是可用的,我经历了几乎所有的失败。关于this问题的参考,我最近做了一些修改。听DialogFragment解雇事件从ViewPager片段

简介:在我的应用程序中,MainActivity持有Fragment View Pager和FrangmentA,B和C. FrangmentA在onClik上显示DialogFragment CDialog。解雇CDialog后,我需要调用FragmentA的doReload(),这不是在这里发生的。

MainActivity

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

}

FragmentA

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

}

}

回答:

你总是可以有直接回调到您的片段本身。

第一步,是利用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();

}

,然后调用此方法打开对话片段:

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()中,有一些cod Ë象下面这样:

@Override 

public void onDismiss(DialogInterface dialog) {

super.onDismiss(dialog);

if (getTargetFragment() instanceof FragmentA)

((FragmentA) getTargetFragment()).doReload();

}

做过什么,这种方式是:用告诉它你的目标片段是“FragmentA”,其引用您可以柜面你有使用

显示对话框片段“CDialog的”沿与它有关。在你的情况下,你不得不打电话给doReload();

以上是 听DialogFragment解雇事件从ViewPager片段 的全部内容, 来源链接: utcz.com/qa/261097.html

回到顶部