我有1个布局。它包含2个片段。此布局中有2个按钮。当我单击按钮1时,片段1将显示。我将在文本视图显示“欢迎”的片段1内容中单击按钮,然后在主布局中单击按钮2,片段2将显示,片段2的textview将显示片段1的textview的内容。
这是我的代码。请给我看一些评论。如何解决这个问题 第一个是mainlayout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="@+id/btnFragment1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="fragment1" />
<Button
android:id="@+id/btnFragment2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Fragment2" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="@+id/container">
</LinearLayout>
第一个是fragment1.xml
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
第一个是fragment1.java
public class Fragment1 extends Fragment{
@Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
}
@Override
public void onAttach(Activity activity) {
// TODO Auto-generated method stub
super.onAttach(activity);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View view = null;
view = inflater.inflate(R.layout.fragment1, null);
//I will get text after I press button and using bundle for storage
and send send to fragment
return view;
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
}
@Override
public void onDestroyView() {
// TODO Auto-generated method stub
super.onDestroyView();
}
@Override
public void onPause() {
// TODO Auto-generated method stub
super.onPause();
}
@Override
public void onResume() {
// TODO Auto-generated method stub
super.onResume();
}
@Override
public void onStart() {
// TODO Auto-generated method stub
super.onStart();
}
@Override
public void onStop() {
// TODO Auto-generated method stub
super.onStop();
}}
第一个是fragment2.xml
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
第一个是fragment2.java
public class Fragment2 extends Fragment{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
return inflater.inflate(R.layout.fragment2, null);
//In this I am going to using Bundle to get message from fragment1
}}
第一个是MainActivity.java
public class MainActivity extends FragmentActivity {
Button btnFragment1, btnFragment2;
FragmentManager fragmentManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
fragmentManager = getSupportFragmentManager();
btnFragment1 = (Button) findViewById(R.id.btnFragment1);
btnFragment1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.replace(R.id.container, new Fragment1(), "TAG_FRAGMENT1");
transaction.addToBackStack(null);
transaction.commit();
}
});
btnFragment2 = (Button) findViewById(R.id.btnFragment2);
btnFragment2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.replace(R.id.container, new Fragment2(), "TAG_FRAGMENT2");
transaction.addToBackStack(null);
transaction.commit();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}}
答案 0 :(得分:3)
使用接口作为对活动的回调,然后将数据传递给fragment2
http://developer.android.com/training/basics/fragments/communicating.html
以上链接中有一个示例
答案 1 :(得分:0)
答案 2 :(得分:0)
您可以使用Activity作为片段之间的代理,这是您可以通过片段告知Activity有关事件的信息:
public class FragmentA extends Fragment {
OnSomethingDoneInFragmentListener mListener;
//this inteface must implement Activity that use the fragment
public interface OnSomethingDoneInFragmentListener {
public void onSomethingDone(Object someObject);
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
mListener = (OnSomethingDoneInFragmentListener) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString() + " must implement OnSomethingDoneInFragmentListener");
}
}
}
答案 3 :(得分:0)
尝试使用EventBus,它可以为您节省大量的工作。
EventBus是Android优化的发布/订阅事件总线。一个 Android应用程序的典型用例是粘贴活动,碎片和 背景线程在一起。这些元件的常规布线 经常会引入复杂且容易出错的依赖关系和生命周期 的问题。 EventBus通过所有参与者传播监听器 (例如,后台服务 - &gt;活动 - >多个片段或帮助程序 类)被弃用了。 EventBus解耦事件发送者和 接收器因此简化了app组件之间的通信。 代码更少,质量更好。而且您不需要实现单个 接口!
定义您的事件,这是一个数据对象:
class MyClickEvent {
// ...
public MyClickEvent(String field1, int field2) {
// ...
}
}
在片段中,注册自己来处理事件:
class MyFragment extends Fragment {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// ...
EventBus.getDefault().register(this, MyClickEvent.class);
}
public void onEvent(MyClickEvent event) {
// handle the event
}
}
点击按钮后发布活动:
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
EventBus.getDefault().post(new MyClickEvent("Some data", 123456));
}
});