如何在片段之间进行通信?

时间:2015-09-02 07:10:46

标签: android android-fragments android-fragmentactivity

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

在主要活动中:

    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;
}

2 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:1)

从Fragment到Fragment进行通信时,您使用一个接口将数据传递给Activity,而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;
  }
}

然后在主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)方法进行初始化,然后使用公共方法从Activity与Fragment进行通信,例如:

 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
      }

 }

多数民众赞成,快乐的编码!

相关问题