Fragment与activity的两种通信方式
Fragment与activity之间有多种通信方式,今天我们主要说两种通信方式,直接在一个activity传值给Fragment的方法和Fragment直接调用Activity中的public方法
Fragment直接调用Activity中的public方法
activity传值给fragment,在这其中我们首先得使用Bundle方法,首先我们得创建fragment
<FrameLayout 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:background="#123456"
tools:context="com.example.asus.mytest2.fragment.BenFragment">
<!-- TODO: Update blank fragment layout -->
<Button
android:layout_width="match_parent"
android:layout_height="70dp"
android:textSize="30sp"
android:gravity="center"
android:text="大笨妞"
android:id="@+id/ben"/>
</FrameLayout>
可以看到,我在fragment中加了一个按钮,接下来我将把按钮上的taxt取我在activity中传的值
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
android:orientation="vertical"
tools:context="com.example.asus.mytest2.ViewPagerActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="@+id/line"
android:layout_below="@+id/ll"
android:layout_al