片段之间如何通信?

How to communicate between fragments?

我正在开发 Android 应用程序。我有一个要求,比如片段 1 中有一个按钮,当用户单击该按钮时,结果应显示在片段 2 中。加载 activity 时,两个片段都已附加。这是我的尝试:

主要activity:

    public void dsp(String str) {
    secondfragment f2=new secondfragment();
    Bundle bundle = new Bundle();
    bundle.putString("edttext", "From Activity");

    f2.setArguments(bundle);
    }

在第一个片段中:

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
    Bundle savedInstanceState) {
    View v=inflater.inflate(R.layout.fragone, container,false);
    Button btn = (Button) v.findViewById(R.id.button1); 
    btn.setOnClickListener(new OnClickListener() {           
          @Override
          public void onClick(View v) 
          {
              m.dsp("clicked");
          }    
        });
    return v;
}

在第二个片段中:

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View v=inflater.inflate(R.layout.fragtwo, container,false);
    tv= (TextView) v.findViewById(R.id.textView1);

    tv.setText(this.getArguments().getString("name"));

    return v;
}

这里有 Android 文档关于 Communicating Between Fragments 的内容。在这里,您将拥有使两个或更多片段安全通信的所有必要步骤:)

在 Fragment 与 Fragment 之间通信时,您使用一个接口将数据传递给 Activity,后者又会更新您要更改的片段。

例如:

在片段 1 中:

public class FragmentOne extends Fragment{

  public Callback mCallback;

  public interface Callback{
       void onUpdateFragmentTwo(String message);
  }


   @Override
   public void onAttach(Activity activity){
     super.onAttach(activity);
     mCallback = (Callback) activity;
   }

   @Override
   public View onCreateView(LayoutInflater inflater, ViewGroup container,
      Bundle savedInstanceState) {
        View v=inflater.inflate(R.layout.fragone, container,false);
        Button btn = (Button) v.findViewById(R.id.button1); 
        btn.setOnClickListener(new OnClickListener() {           
            @Override
           public void onClick(View v) {
               mCallback.onUpdateFragmentTwo("clicked");
            }     
        });
     return v;
  }
}

然后在 main Activity 中实现接口:

public class MainActivity extends AppCompatActivity implements Callback{

  FragmentTwo fragmentTwo;
  @Override
  public void onCreate(Bundle savedInstanceState){
     super.onCreate(savedInstanceState);
     setContentView(R.layout.main);

     // ... Load views or perform logic

     // ... Load Fragment Two into your container
     if(savedInstanceState == null){
         fragmentTwo = FragmentTwo.newInstance(new Bundle()); // use real bundle here
         getSupportFragmentManager()
             .beginTransaction()
             .add(R.id.fragment_holder, fragmentTwo, "Frag2").commit();
     }
  }


  // Interface method
  @Override
  public void onUpdateFragmentTwo(String message){
     // Call activity method with the argument
     if(fragmentTwo != null){
          fragmentTwo.updateFragmentTwo(message);
     }
  }

}

更新

在您的第二个片段中,我通常使用静态 newInstance(Bundle args) 方法进行初始化,然后使用 public 方法从 Activity 到片段进行通信,例如:

 public class FragmentTwo extends Fragment{

      public static FragmentTwo newInstance(Bundle args){
          FragmentTwo fragment = new FragmentTwo();
          fragment.setArguments(args);
          return fragment;
      }

      //... Class overrides here onCreateView etc..

      // declare this method 
      public void updateFragmentTwo(String updateText){
         // .. do something with update text
      }

 }

就是这样,编码愉快!