Fragment之间传值

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);
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值