<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/button" android:layout_gravity="center_horizontal" android:text="Button"/> </LinearLayout>
新建右侧Fragment的布局right_fragment.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:background="#00FF00" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:textSize="24sp" android:text="This is right fragment"/> </LinearLayout>
class LeftFragment : Fragment() { override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle? ): View? { return inflater.inflate(R.layout.left_fragment, container, false) } }
class RightFragment : Fragment() { override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle? ): View? { return inflater.inflate(R.layout.right_fragment, container, false) } }
在activity_main.xml添加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:orientation="horizontal"> <fragment android:id="@+id/leftFrag" android:name="com.zb.fragmenttest.LeftFragment" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" /> <fragment android:id="@+id/rightFrag" android:name="com.zb.fragmenttest.RightFragment" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" /> </LinearLayout>
需要注意的是fragment需要通过android:name属性来显示的添加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:background="#FFFF00" android:orientation="vertical"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:text="This is another right fragment" android:textSize="24sp" /> </LinearLayout>
新建AnotherRightFragment类用于加载布局
class AnotherRightFragment : Fragment() { override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle? ): View? { return inflater.inflate(R.layout.another_right_fragment, container, false) } }
接下来我们动态的将它添加到Activity中,修改activity_main.xml当中的代码
<?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="horizontal"> <fragment android:id="@+id/leftFrag" android:name="com.zb.fragmenttest.LeftFragment" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" /> <FrameLayout android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:id="@+id/rightFrag"> </FrameLayout> </LinearLayout>
在代码当中向FrameLayout中添加内容,从而动态的实现Fragment的功能,修改MainActivity当中的代码
class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) //点击按钮的时候使用AnotherRightFragment button.setOnClickListener { //创建一个Fragment实例传入replaceFragment()方法进行处理 replaceFragment(AnotherRightFragment()) } //不点击按钮的时候使用RightFragment,从而实现一个动态的效果 replaceFragment(RightFragment()) } private fun replaceFragment(fragment: Fragment) { //调用getSupportFragmentManager()方法获取fragmentManager val fragmentManager = supportFragmentManager //fragmentManager调用beginTransaction()开启一个事务 val transition = fragmentManager.beginTransaction() //向容器添加或者Fragment transition.replace(R.id.rightLayout, fragment) //调用commit()方法进行事务提交 transition.commit() } }
private fun replaceFragment(fragment: Fragment) { //调用getSupportFragmentManager()方法获取fragmentManager val fragmentManager = supportFragmentManager //fragmentManager调用beginTransaction()开启一个事务 val transition = fragmentManager.beginTransaction() //向容器添加或者Fragment transition.replace(R.id.rightLayout, fragment) //给Fragment添加一个返回栈 //接受一个名字用于描述返回栈的状态,一般传入null transition.addToBackStack(null) //调用commit()方法进行事务提交 transition.commit() }
虽然Fragment是嵌入在Activity中进行显示的,但是它们之间的关系并不是那么亲密
实际上Activity和Fragment是各自存在于一个独立的类当中的,它们之间没有那么明显的方式来直接进行交互
为了方便Fragment和Activity之间进行交互,FragmentManager提供了类似于findViewById()的方法,专门用于从布局文件当中获取Fragment实例.代码如下所示
val fragment = supportFragment.findFragmentById(R.id.leftFrag) as LeftFragment
调用FragmentManager的findFragmentById()方法,可以在Activity中得到响应Fragment的实例,然后就能轻松调用Fragment中的方法了.
另外类似于findiewById(), kotlin-android-extensions 插件也对findFragmentById()方法进行了拓展,允许我们直接使用布局文件中定义的Fragment id名称来自动获取相应的Fragment实例
例如:
val fragment = leftFrag as LeftFragment
在Activity中调用Fragment的相关方法如上操作
在Fragment中调用Activity的方法也是十分的简单
在每个Fragment当中都可以使用getActivity()方法来获得和当前Fragment相关联的Activity实例
例如:
if (activity != null) { val mainActivity = activity as MainActivity }