有两个 fragment A和B, fragment A有Textview, fragment B有edittext和button。 在 FragmentB 中点击提交需要用 Edittext 文本更新 FragmentA 中的 textview。
fragment之间如何通信?
最佳答案
在这个例子中,FragmentA调用notify。 通知程序
public interface INotifier {
public void notify(Object data);
}
工具
public class Utils {
public static INotifier notifier;
}
fragment A
public FragmentA extends Fragment {
public void onCreateView(...) {
}
public void inSomeMethod() {
if (Utils.notifier != null) {
Utils.notifier.notify(data);
}
}
}
fragment B
public FragmentB extends Fragment implements INotifier {
public void onCreateView(...) {
Utils.notifier = this;
}
@Override
public void notify(Object data) {
// handle data
}
}
关于android - 如何从android中的子 fragment 更新 fragment 中的 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35284327/