第一次写博客自己上代码
首先建立俩fragment A 和 B;
package com.example.test;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.Toast;
public class A extends Fragment {
public interface CallBack {
public void TodoAnything(String str);
}
private CallBack aCallBack = mActivityCall;
private static CallBack mActivityCall = new CallBack() {
@Override
public void TodoAnything(String str) {
}
};
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
if (!(activity instanceof CallBack)) {
throw new IllegalStateException(
"Activity must implement fragment's callbacks.");
}
aCallBack = (CallBack) activity;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_a_layout, null);
RadioGroup rg = (RadioGroup) v.findViewById(R.id.r_group);
rg.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup arg0, int arg1) {
// TODO Auto-generated method stub
switch (arg1) {
case R.id.radioButton1:
aCallBack.TodoAnything("去做第一件事情!");
break;
case R.id.radioButton2:
aCallBack.TodoAnything("去做第二件事情!");
break;
case R.id.radioButton3:
aCallBack.TodoAnything("去做第三件事情!");
break;
}
}
});
return v;
}
@Override
public void onDetach() {
super.onDetach();
aCallBack = mActivityCall;
}
}
再看A的布局fragment_a_layout.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" >
<RadioGroup
android:id="@+id/r_group"
android:layout_width="fill_parent"
android:orientation="horizontal"
android:checkedButton="1"
android:layout_height="wrap_content" >
<RadioButton
android:id="@+id/radioButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="R1" />
<RadioButton
android:id="@+id/radioButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="R2" />
<RadioButton
android:id="@+id/radioButton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="R3" />
</RadioGroup>
</LinearLayout>
再看fragment B的代码
package com.example.test;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class B extends Fragment {
private String str = "nothing todo";
TextView tv;
@Override
public void onAttach(Activity activity) {
// TODO Auto-generated method stub
super.onAttach(activity);
Bundle b = getArguments();
if (b == null) {
str = "null";
} else {
str = b.getString("todo");
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_b_layout, null);
tv = (TextView) v.findViewById(R.id.tv_fragmentB);
tv.setText(str);
return v;
}
public void actionfromA(String str) {
tv.setText(str);
}
}
再看B 的布局fragment_b_layout.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" >
<TextView
android:id="@+id/tv_fragmentB"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
最后看fragment A和B的宿主activity的布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<RelativeLayout
android:id="@+id/fragmentA"
android:layout_height="wrap_content"
android:layout_width="fill_parent"></RelativeLayout>
<RelativeLayout
android:id="@+id/fragmentB"
android:layout_height="wrap_content"
android:layout_width="fill_parent"></RelativeLayout>
</LinearLayout>
最后一步看MainActivity的代码
package com.example.test;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.Menu;
public class MainActivity extends FragmentActivity implements A.CallBack{
private A a;
private B b;
private int in=1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
}
private void init() {
a=new A();
b=new B();
FragmentManager fm=getSupportFragmentManager();
FragmentTransaction ft=getSupportFragmentManager().beginTransaction();
ft.add(R.id.fragmentA, a);
ft.add(R.id.fragmentB, b);
ft.commit();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public void TodoAnything(String str) {
/**此处代码是重新加载fragment B*/
Bundle bundle=new Bundle();
bundle.putString("todo", str);
b=new B();
b.setArguments(bundle);
getSupportFragmentManager().beginTransaction().replace(R.id.fragmentB, b).commit();
/**下面的代码是不重新加载fragment B 通过调用B里面的public 方法让B 做出相应的动作*/
// FragmentManager f=getSupportFragmentManager();
// b=(B) f.findFragmentById(R.id.fragmentB);
// b.actionfromA("fragment A让我做的事情"+in);
// in++;
}
}
不知道怎么上传源代码的所以就只发帖了,等学会了再上传代码。貌似很简单只给初学者当参考吧。如需代码的联络QQ2193953434s