我有一个片段
FR1
包含多个嵌套
片段
; FRA
, FRB
, FRC
。这些嵌套
片段
由pressing 按钮更改
在 FR1
的布局。每个嵌套
片段
的有内他们几个输入字段;其中包括像 EditTexts
, NumberPickers
和纱厂
。当我的用户经过和填充的嵌套
片段
, FR1 (父段)有一个提交按钮。
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.
这是我的嵌套
片段我怎么能那么,找回我的价值观
并使其 FR1
。
浏览
的声明和编程每个嵌套
片段。
父片段
, FR1
处理嵌套 片段
。
All Views
are declared and programmatically handled within each Nested
Fragment
.
The parent Fragment
, FR1
handles the transaction of the Nested
Fragments
.
我希望这个问题是很清楚,我不知道是否张贴code是必要的,但如果有人认为否则我可以这样做。
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.
修改1:
下面是如何将我的嵌套
片段
:
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();
}
});
在我的 constructNewProfile()
方法,从我的嵌套
片段。
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
public void onAttachFragment(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);
this.mContext = getActivity().getApplicationContext();
onAttachFragment(getParentFragment());
// ...
}
通话监听器上按一下按钮。
Call the listener on button click.
@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([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.
}
}
}
添加标签,当你做交易的片段,所以你可以看看它之后调用它的方法。 FragmentTransaction
Add Tag when you do fragment transaction so you can look it up afterward to call its method. FragmentTransaction
这是正确的方式来处理片段和巢片段之间的沟通,这是几乎相同的活动和片段。
http://developer.android.com/guide/components/fragments.html#EventCallbacks
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.