//Fragment是静态注册
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
public class MainActivity extends FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
//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="horizontal" >
<fragment
android:id="@+id/fragment_left"
android:name="com.bwie.fragmenttofragment.LetfFragment"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" />
<fragment
android:id="@+id/fragment_right"
android:name="com.bwie.fragmenttofragment.RightFragment"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" />
</LinearLayout>
//LeftFragment界面
import java.util.ArrayList;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.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class LetfFragment extends Fragment {
private View view;
private ArrayList<String> list;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view = inflater.inflate(R.layout.activity_fragment_left, container,
false);
return view;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
ListView listView = (ListView) view.findViewById(R.id.listview);
list = new ArrayList<String>();
for (int i = 0; i < 20; i++) {
list.add("这是第" + i + "条数据");
}
listView.setAdapter(new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_list_item_1, list));
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
String string = list.get(position);
listener.onItemClick(string);
}
});
}
// 定义回调接口的步骤
// 1. 定义一个接口OnListClickListener
// 2.定义接口中的方法来将数据回传到需要接收数据的组件中onItemClick(String str)
// 3.给外部提供一个设置回调接口的方法setListener(),参数就是回调接口的一个实例
// 4.在需要回传数据的地方调用 外部 传来的接口实例的回调方法
private OnListClickListener listener;
public void setListener(OnListClickListener listener) {
this.listener = listener;
}
interface OnListClickListener {
void onItemClick(String str);
}
}
//LeftFragment的布局文件
<?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" >
<ListView
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</ListView>
</LinearLayout>
//RightFragment的页面
import com.bwie.fragmenttofragment.LetfFragment.OnListClickListener;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 RightFragment extends Fragment {
private View view;
private TextView textView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view = inflater.inflate(R.layout.activity_fragment_right, container,false);
return view;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
textView = (TextView) view.findViewById(R.id.textview);
LetfFragment letfFragment = (LetfFragment) getActivity().getSupportFragmentManager().findFragmentById(R.id.fragment_left);
// 外部在需要设置回调接口的地方,设置回调接口的实例,然后在接口的回调方法中接收传回来的数据
letfFragment.setListener(new OnListClickListener() {
@Override
public void onItemClick(String str) {
textView.setText(str);
}
});
}
}
//RightFragment的布局文件
<?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/textview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center" />
</LinearLayout>