将数据从嵌套片段发送到父片段 [英] Sending data from nested fragments to parent fragment
问题描述
我有一个 Fragment
FR1
包含几个 Nested
Fragments
;FRa
、FRb
、FRc
.这些 Nested
Fragments
通过按 FR1
布局上的 Buttons
进行更改.每个 Nested
Fragments
中都有几个输入字段;其中包括 EditTexts
、NumberPickers
和 Spinners
之类的内容.当我的用户完成并填写 Nested
Fragments
的所有值时,FR1
(父片段)有一个提交按钮.>
然后我怎样才能从我的 Nested
Fragments
中检索我的值并将它们带入 FR1
.
- 所有
Views
都在每个Nested
Fragment
中声明并以编程方式处理. - 父
Fragment
,FR1
处理Nested
Fragments
的事务.
我希望这个问题足够清楚,我不确定是否需要发布代码,但如果有人觉得我可以这样做.
编辑 1:
这是我添加我的 Nested
Fragments
的方法:
tempRangeButton.setOnClickListener(new View.OnClickListener() {@覆盖public void onClick(View v) {getChildFragmentManager().beginTransaction().add(R.id.settings_fragment_tertiary_nest, tempFrag).犯罪();}});scheduleButton.setOnClickListener(new View.OnClickListener() {@覆盖public void onClick(View v) {getChildFragmentManager().beginTransaction().add(R.id.settings_fragment_tertiary_nest, scheduleFrag).犯罪();}});alertsButton.setOnClickListener(new View.OnClickListener() {@覆盖public void onClick(View v) {getChildFragmentManager().beginTransaction().add(R.id.settings_fragment_tertiary_nest,alertsFrag).犯罪();}});submitProfile.setOnClickListener(new View.OnClickListener() {@覆盖public void onClick(View v) {构造新配置文件();}});
我的 constructNewProfile()
方法需要来自我的 Nested
Fragments
的值.
public Fragment tempFrag = fragment_profile_settings_temperature.newInstance();public Fragment scheduleFrag= fragment_profile_settings_schedules.newInstance();公共片段警报Frag = fragment_profile_settings_alerts.newInstance();
以上是指父片段的字段;以及它们最初是如何实例化的.
最好的方法是使用接口:
在嵌套片段中声明一个接口
//容器Activity或Fragment必须实现这个接口公共接口 OnPlayerSelectionSetListener{public void onPlayerSelectionSet(List
players_ist);} 将接口附加到父片段
//在子片段中.公共无效 onAttachToParentFragment(片段片段){尝试{mOnPlayerSelectionSetListener = (OnPlayerSelectionSetListener)fragment;}捕获(ClassCastException e){抛出新的 ClassCastException(fragment.toString() + " 必须实现 OnPlayerSelectionSetListener");}}@覆盖public void onCreate(Bundle savedInstanceState){Log.i(TAG, "onCreate");super.onCreate(savedInstanceState);onAttachToParentFragment(getParentFragment());//...}
点击按钮时调用监听器.
//在子片段中.@覆盖公共无效 onClick(查看 v){开关 (v.getId()){案例 R.id.tv_submit:如果(mOnPlayerSelectionSetListener != null){mOnPlayerSelectionSetListener.onPlayerSelectionSet(selectedPlayers);}休息;}}
让你的父片段实现接口.
公共类 Fragment_Parent 扩展 Fragment 实现 Nested_Fragment.OnPlayerSelectionSetListener{//...@覆盖public void onPlayerSelectionSet(final List
players_list){FragmentManager fragmentManager = getChildFragmentManager();SomeOtherNestFrag someOtherNestFrag = (SomeOtherNestFrag)fragmentManager.findFragmentByTag("Some fragment tag");//添加时应使用的片段标签if(someOtherNestFrag != null){//您的其他一些片段需要根据视图提供一些数据.SomeData somedata = someOtherNestFrag.getSomeData();//它可以是一个字符串,或 int,或一些自定义的 java 对象.}}}
在做fragment事务的时候添加Tag,这样你就可以在之后查找它来调用它的方法.片段交易
这是处理片段和嵌套片段之间通信的正确方法,活动和片段几乎相同.http://developer.android.com/guide/components/fragments.html#EventCallbacks
其实还有另外一种官方方式,就是使用活动结果,不过这个已经足够好用了.
I have a Fragment
FR1
that contains several Nested
Fragments
; FRa
, FRb
, FRc
. These Nested
Fragments
are changed by pressing Buttons
on FR1
's layout. Each of the Nested
Fragments
have several input fields within them; which include things like EditTexts
, NumberPickers
, and Spinners
. When my user goes through and fills in all the values for the Nested
Fragments
, FR1
(the parent fragment) has a submit button.
How can I then, retrieve my values from my Nested
Fragments
and bring them into FR1
.
- All
Views
are declared and programmatically handled within eachNested
Fragment
. - The parent
Fragment
,FR1
handles the transaction of theNested
Fragments
.
I hope this question is clear enough and I am not sure if code is necessary to post but if someone feels otherwise I can do so.
EDIT 1:
Here is how I add my Nested
Fragments
:
tempRangeButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
getChildFragmentManager().beginTransaction()
.add(R.id.settings_fragment_tertiary_nest, tempFrag)
.commit();
}
});
scheduleButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
getChildFragmentManager().beginTransaction()
.add(R.id.settings_fragment_tertiary_nest, scheduleFrag)
.commit();
}
});
alertsButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
getChildFragmentManager().beginTransaction()
.add(R.id.settings_fragment_tertiary_nest, alertsFrag)
.commit();
}
});
submitProfile.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
constructNewProfile();
}
});
where my constructNewProfile()
method needs the values from my Nested
Fragments
.
public Fragment tempFrag = fragment_profile_settings_temperature
.newInstance();
public Fragment scheduleFrag= fragment_profile_settings_schedules
.newInstance();
public Fragment alertsFrag = fragment_profile_settings_alerts
.newInstance();
The above refers to the fields of the parent fragment; and how they are initially instantiated.
The best way is use an interface:
Declare an interface in the nest fragment
// Container Activity or Fragment must implement this interface public interface OnPlayerSelectionSetListener { public void onPlayerSelectionSet(List<Player> players_ist); }
Attach the interface to parent fragment
// In the child fragment. public void onAttachToParentFragment(Fragment fragment) { try { mOnPlayerSelectionSetListener = (OnPlayerSelectionSetListener)fragment; } catch (ClassCastException e) { throw new ClassCastException( fragment.toString() + " must implement OnPlayerSelectionSetListener"); } } @Override public void onCreate(Bundle savedInstanceState) { Log.i(TAG, "onCreate"); super.onCreate(savedInstanceState); onAttachToParentFragment(getParentFragment()); // ... }
Call the listener on button click.
// In the child fragment. @Override public void onClick(View v) { switch (v.getId()) { case R.id.tv_submit: if (mOnPlayerSelectionSetListener != null) { mOnPlayerSelectionSetListener.onPlayerSelectionSet(selectedPlayers); } break; } }
Have your parent fragment implement the interface.
public class Fragment_Parent extends Fragment implements Nested_Fragment.OnPlayerSelectionSetListener { // ... @Override public void onPlayerSelectionSet(final List<Player> players_list) { FragmentManager fragmentManager = getChildFragmentManager(); SomeOtherNestFrag someOtherNestFrag = (SomeOtherNestFrag)fragmentManager.findFragmentByTag("Some fragment tag"); //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. } } }
Add Tag when you do fragment transaction so you can look it up afterward to call its method. FragmentTransaction
This is the proper way to handle communication between fragment and nest fragment, it's almost the same for activity and fragment. http://developer.android.com/guide/components/fragments.html#EventCallbacks
There is actually another official way, it's using activity result, but this one is good enough and common.
这篇关于将数据从嵌套片段发送到父片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!