簡體   English   中英

兩個片段之間的通信。 我沒有為任何片段使用任何已經定義的視圖。 我只是在運行時添加片段

[英]communication between two fragments . I am not using any already defined view for any of the fragments. I am just adding fragments at runtime

我有兩個片段MyFragment和MyFragment2,MyFragment2中定義的接口。

MyFragment2的代碼:

public class MyFragment2 extends android.support.v4.app.Fragment implements View.OnClickListener {
public interface MyInterface2
{
    public void respond(String s);
}
EditText editText;
Button sendData;
MyInterface2 comm;
View v=getView();
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {


    v = inflater.inflate(R.layout.my_fragment_2,container,false);
    return v;
}



@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    this.comm = (MyInterface2)getActivity();
    editText = (EditText)getActivity().findViewById(R.id.text);
    sendData =(Button)getActivity().findViewById(R.id.sendData);
    sendData.setOnClickListener(this);
}


@Override
public void onClick(View v) {

    try {
        String s = editText.getText().toString();
        comm.respond(s);
    }
    catch(Exception e)
    {
        Toast.makeText(getActivity(),"error:"+e,Toast.LENGTH_LONG).show();
    }
  }
 }

我試圖通過按下按鈕( Myfragment2最初使用Java代碼在主活動的視圖上設置)將editText的內容從MyFragment2發送到片段MyFragment

主要活動的代碼如下*(my_layout是我放置片段的布局ID)*:

public class MainActivity extends AppCompatActivity implements     MyFragment2.MyInterface2 {

    //String data;
   @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    MyFragment2 fragment2= new MyFragment2();
    FragmentManager fManager= getSupportFragmentManager();
    FragmentTransaction transaction= fManager.beginTransaction();
    transaction.add(R.id.my_layout,fragment2,"MyFragment2");
    transaction.commit();

}

public void respond(String s)
{

   MyFragment fragment = new MyFragment();
    FragmentManager fManager =getSupportFragmentManager();
    FragmentTransaction transaction = fManager.beginTransaction();
    transaction.replace(R.id.my_layout,fragment);

    transaction.commit();
    fragment.getData(s);
  }
}

當我單擊片段MyFragment2中的sendData按鈕時,MyFragment2被片段Myfragment替換,但MyFragmnet的textView中沒有發生數據更改,並且還顯示了一個錯誤,我在片段MyFragment2的try catch塊中捕獲了( 這里是該圖像) )。 MyFragment的代碼

public class MyFragment extends android.support.v4.app.Fragment {

TextView textView;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,  Bundle savedInstanceState) {
    View v= inflater.inflate(R.layout.my_fragment,container,false);
    //textView =(TextView)v.findViewById(R.id.getText);
    return v;
}

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    textView =(TextView)getActivity().findViewById(R.id.getText);
 }

 public void getData(String data)
  {
    textView.setText(data);
  }
}

您可以使用回調或通過包傳遞參數輕松地在片段之間傳遞數據。 看看這篇文章: https//developer.android.com/training/basics/fragments/communicating.html

onActivityCreated是沒有必要的。 取消注釋onCreateView塊中的文本視圖啟動語句。 確保您在相應的布局文件中具有文本視圖,並且具有相同的ID

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM