android - 如何从android中的子 fragment 更新 fragment 中的 View

标签 android android-fragments fragment

enter image description here

有两个 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/

相关文章:

android - 如何在 Android 中使用右上角的关闭按钮进行 Activity ?

Android Studio 停止按钮没有停止

android - Analytics sendEvent 不发送数据

android - 查看寻呼机垂直重叠选项卡 fragment

java - 具有空构造函数的 fragment 与 newInstance

android - 如何实现Android Fragment Transaction .add语法错误

Android 按钮或 TextView 垂直对齐

java - 如何在android中设置一个主要 Activity 并填充 fragment

android - fragment 内的表格布局

android - 抽屉导航中每个 fragment 的不同工具栏