Fragment的使用方式
Fragment的简单用法
在一个Activity中添加两个Fragment,并让这两个Fragment平分Activity空间
新建左侧Fragment的布局left_fragment.xml
<?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>
接着新建一个LeftFragment类,让他继承Fragment,这这里会存在两个包下的Fragment提供给我们使用
我们要选择的是AndroidX库中的Fragment,因为它可以让Fragment的特性在所有的Android系统版本中保持一致.
使用AndroidX库中的Fragment并不需要在build.gradle文件中添加额外依赖,只需要在创建新项目的时候勾选Use androidx.* artifacts选项,AS会自动帮助导入必要的AndoidX库
LeftFragmeng代码
class LeftFragment : Fragment ( ) {
override fun onCreateView (
inflater: LayoutInflater,
container: ViewGroup? ,
savedInstanceState: Bundle?
) : View? {
return inflater. inflate ( R. layout. left_fragment, container, false )
}
}
这里重写了Fragment类的onCreateView()方法,在这个方法当中通过LayoutInflater的inflater()方法将定义的left_fragment.xml布局动态加载进来
以同样的方式,创建RightFragment
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类名,需要注意的是一定要将类的包名也加上.
动态添加Fragment
Fragment真正强大之处在于它可以在程序运行时动态的添加到Activity中,根据具体情况来动态的添加Fragment,使得程序的定制变得多样化.
新建another_right_fragment.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: 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)
button. setOnClickListener {
replaceFragment ( AnotherRightFragment ( ) )
}
replaceFragment ( RightFragment ( ) )
}
private fun replaceFragment ( fragment: Fragment) {
val fragmentManager = supportFragmentManager
val transition = fragmentManager. beginTransaction ( )
transition. replace ( R. id. rightLayout, fragment)
transition. commit ( )
}
}
在Fragment中实现返回栈
实现了向Activity中动态添加了Fragment的功能后,发现通过点击按钮添加了一个Fragment之后这时候按下返回键,程序会直接退出
如果我们想要实现类似于返回栈的效果,按下返回键可以回到上一个Fragment,该如何实现呢?
其实很简单,在FragmentTransation中提供了一个addToBackStack(),可用于将一个事务添加到返回栈当中
修改MainActivity中的代码实现一下这个功能,只需要在replaceFragment()方法当中加入一行代码即可
private fun replaceFragment ( fragment: Fragment) {
val fragmentManager = supportFragmentManager
val transition = fragmentManager. beginTransaction ( )
transition. replace ( R. id. rightLayout, fragment)
transition. addToBackStack ( null )
transition. commit ( )
}
Fragment和Activity之间的交互
虽然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
}