如何从android中的子片段更新片段中的视图
有两个片段A和B,片段A有Textview,片段B有edittext和button。 单击FragmentB中的submit需要使用Edittext文本更新FragmentA中的textview如何从android中的子片段更新片段中的视图,android,android-fragments,fragment,Android,Android Fragments,Fragment,有两个片段A和B,片段A有Textview,片段B有edittext和button。 单击FragmentB中的submit需要使用Edittext文本更新FragmentA中的textview 如何在片段之间进行通信?您需要首先与活动交互,后者将与第二个片段交互。并阅读这篇关于如何做到这一点的文章 您需要首先与活动交互,活动将与第二个片段交互。并阅读这篇关于如何做到这一点的文章 片段之间的通信是使用侦听器来完成的。当您想要更新片段时,使用侦听器告诉MainActivity按照Google的建议
如何在片段之间进行通信?您需要首先与活动交互,后者将与第二个片段交互。并阅读这篇关于如何做到这一点的文章
您需要首先与活动交互,活动将与第二个片段交互。并阅读这篇关于如何做到这一点的文章
片段之间的通信是使用
侦听器来完成的。当您想要更新片段时,使用侦听器告诉MainActivity
按照Google的建议更新第二个片段。在片段
中创建接口,并在活动
片段中的侦听器
public interface FragmentUpdateInterface {
void updateFragment(String newText);
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
// This makes sure that the container activity has implemented
// the callback interface. If not, it throws an exception
try {
mCallback = (FragmentUpdateInterface ) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString()
+ " must implement FragmentUpdateListener");
}
}
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
// Send the event to the host activity
mCallback.updateFragment("New Text");
}
main活动
在MainActivity
as中实现片段
public static class MainActivity extends Activity
implements MyFragment.FragmentUpdateListener{
public void updateFragment(String newText) {
OtherFragment otherFrag = (OtherFragment)
getSupportFragmentManager().findFragmentById(R.id.other_fragment);
if (otherFrag != null) {
otherFrag.updateFragment(newText);
} else {
// Otherwise, we're in the one-pane layout and must swap frags...
// Create fragment and give it an argument for the selected article
OtherFragment otherFrag = new OtherFragment();
Bundle args = new Bundle();
args.putInt(ArticleFragment.ARG_POSITION, position);
otherFrag.setArguments(args);
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.fragment_container, otherFrag);
transaction.addToBackStack(null);
// Commit the transaction
transaction.commit();
}
}
希望这有帮助
更新:
您还可以使用LocalBroadcastManager.getInstance().sendBroadcast()
通知其他片段。片段之间的通信使用侦听器完成。当您想要更新片段时,使用侦听器告诉MainActivity
按照Google的建议更新第二个片段。在片段
中创建接口,并在活动
片段中的侦听器
public interface FragmentUpdateInterface {
void updateFragment(String newText);
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
// This makes sure that the container activity has implemented
// the callback interface. If not, it throws an exception
try {
mCallback = (FragmentUpdateInterface ) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString()
+ " must implement FragmentUpdateListener");
}
}
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
// Send the event to the host activity
mCallback.updateFragment("New Text");
}
main活动
在MainActivity
as中实现片段
public static class MainActivity extends Activity
implements MyFragment.FragmentUpdateListener{
public void updateFragment(String newText) {
OtherFragment otherFrag = (OtherFragment)
getSupportFragmentManager().findFragmentById(R.id.other_fragment);
if (otherFrag != null) {
otherFrag.updateFragment(newText);
} else {
// Otherwise, we're in the one-pane layout and must swap frags...
// Create fragment and give it an argument for the selected article
OtherFragment otherFrag = new OtherFragment();
Bundle args = new Bundle();
args.putInt(ArticleFragment.ARG_POSITION, position);
otherFrag.setArguments(args);
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.fragment_container, otherFrag);
transaction.addToBackStack(null);
// Commit the transaction
transaction.commit();
}
}
希望这有帮助
更新:
您还可以使用LocalBroadcastManager.getInstance().sendBroadcast()
通知其他片段。n在本例中,FragmentA调用notify。
杀菌剂
public interface INotifier {
public void notify(Object data);
}
乌提尔斯
碎片
public FragmentA extends Fragment {
public void onCreateView(...) {
}
public void inSomeMethod() {
if (Utils.notifier != null) {
Utils.notifier.notify(data);
}
}
}
碎片B
public FragmentB extends Fragment implements INotifier {
public void onCreateView(...) {
Utils.notifier = this;
}
@Override
public void notify(Object data) {
// handle data
}
}
在本例中,FragmentA调用notify。
杀菌剂
public interface INotifier {
public void notify(Object data);
}
乌提尔斯
碎片
public FragmentA extends Fragment {
public void onCreateView(...) {
}
public void inSomeMethod() {
if (Utils.notifier != null) {
Utils.notifier.notify(data);
}
}
}
碎片B
public FragmentB extends Fragment implements INotifier {
public void onCreateView(...) {
Utils.notifier = this;
}
@Override
public void notify(Object data) {
// handle data
}
}
尝试在带有回调的片段中使Textview无效。有两种可能,您可以使用这些片段之间的接口,也可以使用Eventbus库,它允许您为此创建本地事件和事件侦听器。如果您需要更多关于eventbus的信息,请告诉我。您必须添加一个侦听器,并按照@androidnoobdev所说的,您可以使用Interface或SharedReferences来更新视图。下面是接口try invalidate Textview在带有回调的片段中的简单实现。有两种可能,您可以使用这些片段之间的接口,也可以使用Eventbus库,它允许您创建本地事件和事件侦听器。如果您需要更多关于eventbus的信息,请告诉我。您必须添加一个侦听器,并按照@androidnoobdev所说的,您可以使用Interface或SharedReferences来更新视图。这是接口的简单实现,它给出了循环继承问题。您是否使用正在片段中实现的任何其他接口?是的,我正在使用制表符更改列表器…检查我的回答是的,这是一个问题。Fragment和activity都是相互依赖的。ok check实现了一些代码,但它没有在工作完成后更新第一个调用它给出了循环继承问题您是否使用正在Fragment中实现的任何其他接口?是的,我正在使用tab change listners…检查我的答案是的,这是一个问题。片段和活动都是相互依赖的。ok check实现了一些代码,但在工作正常后没有更新第一个调用