1.布局,先写两个FrameLayout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="horizontal">
<FrameLayout
android:id="@+id/fram1"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent" >
</FrameLayout>
<FrameLayout
android:id="@+id/fram2"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent" >
</FrameLayout>
</LinearLayout>
2.MainActivity开启事物
public class MainActivity extends FragmentActivity {
private Fra1 f1;
private Fra2 f2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//开启事务
FragmentManager manager = getSupportFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
//实例化两个布局
f1 = new Fra1();
f2 = new Fra2();
//添加到FragmentManager中
transaction.add(R.id.fram1,f1);
transaction.add(R.id.fram2,f2);
//同时暂时两个页面
transaction.show(f1).show(f2);
//提交
transaction.commit();
}
}
3.两个Fragment布局
//第一个布局写列表
<?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">
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
//第二个布局写一个TextView
<?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">
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
</LinearLayout>
4.开始写接口
public class Fra1 extends Fragment {
private ListView listview;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fra1, null);
//写ListView加适配器
listview = (ListView) view.findViewById(R.id.listView1);
final List<String> list=new ArrayList<String>();
for (int i = 0; i <10; i++) {
list.add("dadada"+i);
}
//适配器
listview.setAdapter(new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, list));
//条目点击事件
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
call.getzhi(list.get(position));
}
});
return view;
}
//定义接口
interface callbacks{
void getzhi(String name);
}
//起个小名
public callbacks call;
//构造方法
public void callbacks(callbacks call) {
this.call = call;
}
}
5.接收接口,并回显
//第一步,切记要继承接口
public class Fra2 extends Fragment implements Fra1.callbacks {
private TextView text;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fra2, null);
text = (TextView) view.findViewById(R.id.textView1);
FragmentManager manager = getActivity().getSupportFragmentManager();
Fra1 fr1 = (Fra1) manager.findFragmentById(R.id.fram1);
fr1.callbacks(this);
return view;
}
@Override
public void getzhi(String name) {
text.setText(name);
}
}