Fragment与Activity的交互
<1>宿主:Fragment所依赖的Activity称为Fragment的宿主
<2>Fragment->Activity->Fragment 传值方式
在Fragment类中可以通过getActivity()方法获取其所依赖的Activity对象这样 我们就可以直接调用Activity中的方法了,但是这种与Activity交互的方式,会导致Fragment
直接依赖一个具体的Activity,在Android设计中,Fragment应该是可以附加到多个Activity上,
以重复使用相同的功能,所以直接依赖一个具体的Activity是一种不好的设计。
那么我们应该怎么办?
让Fragment依赖自己定义的内部接口,该接口需要由宿主来实现,这样的接口我们称回调接口
,所以,哪个Fragment想往外传值,就定义自己的回调接口。
两个Fragment传值示例:
<1>定义两个Fragment,TitleFragment和CityFragment:
public class TitleFragment extends Fragment implements View.OnClickListener{
private Button button_bj,button_sh;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.title_layout,null);
button_bj = (Button) view.findViewById(R.id.button2_bj);
button_sh = (Button) view.findViewById(R.id.button3_sh);
button_bj.setOnClickListener(this);
button_sh.setOnClickListener(this);
return view;
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.button2_bj:
//单击事件(需要给另一个Fragment传值)
break;
case R.id.button3_sh:
//单击事件(需要给另一个Fragment传值)
break;
}
}
}
TitleFragment的布局文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:gravity="center_horizontal"
android:background="#f7c1c1"
android:padding="16dp"
android:layout_height="wrap_content">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="北京"
android:id="@+id/button2_bj" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="上海"
android:id="@+id/button3_sh" />
</LinearLayout>
public class CityFragment extends Fragment {
private TextView textView_city;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.city_layout,null);
textView_city = (TextView) view.findViewById(R.id.textView_city);
return view;
}
}
CityFragment的布局文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="not data"
android:textSize="30sp"
android:gravity="center"
android:id="@+id/textView_city"
android:layout_gravity="center_horizontal" />
</LinearLayout>
<2>在Activity中使用两个Fragment:
public class Main4Activity extends AppCompatActivity{
private TitleFragment titleFragment;
private CityFragment cityFragment;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main4);
titleFragment = (TitleFragment) getFragmentManager().findFragmentById(R.id.title_fragment);
cityFragment = (CityFragment) getFragmentManager().findFragmentById(R.id.city_fragment);
}
}
Main4Activity的布局文件:
<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="com.moliying.fragment.Main4Activity">
<fragment
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:id="@+id/title_fragment"
android:name="com.moliying.fragment.TitleFragment"/>
<fragment
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="5"
android:id="@+id/city_fragment"
android:name="com.moliying.fragment.CityFragment"/>
</LinearLayout>
<3>在TitleFragment类里定义接口,并在按钮的单击事件中调用,该接口由宿主Activity来实现:
public static interface OnSetValueListener{
public void setValue(String value);
}
private OnSetValueListener onSetValueListener;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//必须要求宿主实现
onSetValueListener = (OnSetValueListener) getActivity();
}
public void onClick(View v) {
switch (v.getId()){
case R.id.button2_bj:
onSetValueListener.setValue("北京");
break;
case R.id.button3_sh:
onSetValueListener.setValue("上海");
break;
}
}
<4>宿主Activity实现Fragment的回调接口,并调用其它Fragment的方法把值传过去:
public class Main4Activity extends AppCompatActivity implements TitleFragment.OnSetValueListener{
//....
@Override
public void setValue(String value) {
cityFragment.setCity(value);
}
}
<5>在CityFragment中定义一个setCity方法来接收参数,并设置到TextView组件中
public void setCity(String city){
textView_city.setText(city);
}