java 从对话框片段调用父片段方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26813744/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Call parent fragment method from dialog fragment
提问by Naruto
I have a parent fragment, within this. Upon a button click, a child dialog fragment is getting created. Now I would like to know how to call parent fragment function from child dialog fragment.
我有一个父片段,在这个片段中。单击按钮后,将创建一个子对话框片段。现在我想知道如何从子对话框片段调用父片段函数。
Here is the sample code :
这是示例代码:
/**SampleFragment.java**/
public class SampleFragment extends Fragment {
// Instantiate view & add event handlers
public void onButtonClick(....) {
// Create a dialog framgent
}
public void refreshView() {
}
}
/**SampleDialogFragment.java**/
public class SampleDialogFragment extends DialogFragment {
// Instantiate view for dialog
public void onButtonClick(...) {
// Call parent fragment method, i.e call refreshView() of SampleFragment
}
}
回答by SteD
In say your parent fragment, SettingsFragment for example. Note the setTargetFragment()
比如说你的父片段,例如 SettingsFragment。请注意setTargetFragment()
public void onButtonClick(....)
{
PrefLanguageDialogFragment prefLang = PrefLanguageDialogFragment.newInstance();
prefLang.setTargetFragment(SettingsFragment.this, 1337);
prefLang.show(getFragmentManager(), "dialog");
}
In our dialog, note the getTargetFragment()
在我们的对话中,注意 getTargetFragment()
SettingsFragment frag = (SettingsFragment)getTargetFragment();
if(frag != null){
frag.refreshSomething();
}
回答by Anant Shah
In a Fragment:
在一个片段中:
SampleDialogFragment dialogFragment = new SampleDialogFragment();
dialogFragment.show(getChildFragmentManager());
In a DialogFragment:
在 DialogFragment 中:
((SampleFragment) getParentFragment()).refreshView();
After calling this method, you can access public methods of a parent fragment.
调用该方法后,即可访问父片段的公共方法。
回答by mmlooloo
when you want add SampleFragment
to your activity set it a tag, e.g "SampleFragment".
当您想添加SampleFragment
到您的活动时,请为其设置一个标签,例如“SampleFragment”。
then
然后
public void onButtonClick(...){
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
SampleFragment parent = (SampleFragment)fm.findFragmentByTag("SampleFragment");
parent.refreshview();
}
have not test it but it may help:-)
还没有测试,但它可能有帮助:-)
回答by Ashish Tamrakar
The best way is to go for interface, declare an interface in nested fragment -
最好的方法是去接口,在嵌套片段中声明一个接口 -
public interface checkingClickListener { public void checkingClickListener(String data); }
公共接口checkingClickListener { public void checksClickListener(String data); }
then attach this interface to parent fragment -
然后将此接口附加到父片段 -
public void onAttachFragment(Fragment fragment)
{
try
{
clickListener = (checkingClickListener) fragment;
} catch (ClassCastException e)
{
throw new ClassCastException(fragment.toString() + " must implement checkingClickListener");
}
}
@Override
public void onCreate(Bundle savedInstanceState)
{
Log.i(TAG, "onCreate");
super.onCreate(savedInstanceState);
this.mContext = getActivity().getApplicationContext();
onAttachFragment(getParentFragment());
....
}
you need to call this listener on some button click -
您需要在单击某个按钮时调用此侦听器 -
@Override
public void onClick(View v)
{
switch (v.getId())
{
case R.id.tv_submit:
if (clickListener != null)
{
clickListener.checkingClickListener("sending data");
}
break;
}
}
Implement this interface in parent fragment -
在父片段中实现此接口 -
public class Fragment_Parent extends Fragment implements Nested_Fragment.checkingClickListener
{
....
@Override
public void checkingClickListener(final List<Player> players_list)
{
FragmentManager fragmentManager = getChildFragmentManager();
SomeOtherNestFrag someOtherNestFrag = (SomeOtherNestFrag) fragmentManager.findFragmentByTag([Tag of your fragment which you should use when you add]);
if(someOtherNestFrag != null)
{
// your some other frag need to provide some data back based on views.
SomeData somedata = someOtherNestFrag.getSomeData();
// it can be a string, or int, or some custom java object.
}
}
}
Hope this helps you.
希望这对你有帮助。