先演示一下如何在一个activity中放置两个Fragment,先定义两个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" android:gravity="center" android:orientation="vertical"> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:text="注销登陆" /> </LinearLayout>
<?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:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:text="上面的片段显示用户信息" /> </LinearLayout>
package com.ouc.wkp.ui1; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; /** * Created by wkp on 2016/8/25. */ public class FragmentIndexBottom extends Fragment { @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view=inflater.inflate(R.layout.fragment_indexbottom,container,false); return view; } }
package com.ouc.wkp.ui1; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; /** * Created by wkp on 2016/8/25. */ public class FragmentIndexTop extends Fragment{ @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view=inflater.inflate(R.layout.fragment_indextop,container,false); return view; } }
然后定义Activity,放置两个FrameLayout
<?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"> <FrameLayout android:id="@+id/fragment_layout_top" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1"></FrameLayout> <FrameLayout android:id="@+id/fragment_layout_bottom" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1"></FrameLayout> </LinearLayout>
package com.ouc.wkp.ui1; import android.os.Bundle; import android.os.PersistableBundle; import android.support.annotation.Nullable; import android.support.v4.app.FragmentActivity; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentTransaction; /** * Created by wkp on 2016/8/25. */ public class FragmentDemo2 extends FragmentActivity { @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.fragment_demo2); FragmentManager fm = getSupportFragmentManager(); FragmentTransaction ft = fm.beginTransaction(); ft.replace(R.id.fragment_layout_top, new FragmentIndexTop()); ft.replace(R.id.fragment_layout_bottom, new FragmentIndexBottom()); ft.commit(); } }
上面的例子比较简单,和上一篇基本类似,所以很容易看出使用Fragment是为了代码的复用。
下面介绍fragment和activity之间的通信。有两种形式,一种是fragment->activity,另一种是activity->fragment。
实现方式是定义modify函数或者定义事件监听方法回调。方法回调的方式比较不好理解
<?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:background="#f00" android:orientation="vertical"> <TextView android:id="@+id/tv_in_fragment" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="这是一个Fragment,通过动态方式加载" /> <Button android:id="@+id/btn_in_fragment" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>
package com.ouc.wkp.ui1; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; /** * Created by wkp on 2016/8/25. */ public class FragmentIndex extends Fragment {//app包下3.0以后才可以使用 private OnBtnClickListener onBtnClickListener; public void setOnBtnClickListener(OnBtnClickListener onBtnClickListener) { this.onBtnClickListener = onBtnClickListener; } public interface OnBtnClickListener{ void onBtnClick(); } @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { //别遗忘第三个false参数,否则会出现 java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first. View view=inflater.inflate(R.layout.fragment_index,container,false); view.findViewById(R.id.btn_in_fragment).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { // FragmentDemo fragmentDemo=(FragmentDemo) getActivity(); // fragmentDemo.modify(); if(onBtnClickListener!=null){ onBtnClickListener.onBtnClick(); } } }); return view; } public void modify(){ TextView textView=(TextView) getView().findViewById(R.id.tv_in_fragment); textView.setText("已经修改fragment里面的UI元素的值"); } }
<?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/btn_addd" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="增加" /> <Button android:id="@+id/btn_deletee" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="减少" /> <FrameLayout android:id="@+id/frame_layout" android:layout_width="match_parent" android:layout_height="200dp"></FrameLayout> <Button android:id="@+id/btn_modify" android:text="修改fragment的界面" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/tv_in_activity" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="哈哈哈"/> </LinearLayout>
package com.ouc.wkp.ui1; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v4.app.FragmentActivity; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentTransaction; import android.view.View; import android.widget.TextView; /** * Created by wkp on 2016/8/25. */ public class FragmentDemo extends FragmentActivity{ FragmentIndex fragmentIndex; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.fragment_demo); fragmentIndex=new FragmentIndex(); //接口回调 fragmentIndex.setOnBtnClickListener(new FragmentIndex.OnBtnClickListener(){ @Override public void onBtnClick() { TextView textView=(TextView)findViewById(R.id.tv_in_activity); textView.setText("修改activity中的textView(通过方法回调)"); } }); findViewById(R.id.btn_addd).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { FragmentManager fm=getSupportFragmentManager(); FragmentTransaction ft=fm.beginTransaction(); ft.replace(R.id.frame_layout,fragmentIndex); ft.commit(); } }); findViewById(R.id.btn_deletee).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { FragmentManager fm=getSupportFragmentManager(); FragmentTransaction ft=fm.beginTransaction(); ft.remove(fragmentIndex); ft.commit(); } }); findViewById(R.id.btn_modify).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { //从Activity修改Fragment的值 fragmentIndex.modify();; } }); } public void modify(){ TextView textView=(TextView) findViewById(R.id.tv_in_activity); textView.setText("修改activity中的textView"); } }