android开发,使用kotlin学习Fragment

1.Fragment的介绍


Android在3.0版本引入了Fragment功能,它非常类似于Activity,可以像Activity一样包含布局。

它出现的初衷是为了适应大屏幕的平板电脑,使用Fragment我们可以把屏幕划分成几块,合理利用屏幕空间。

Fragment通常是嵌套在Activity中使用。

2.静态加载


步骤:

(1)定义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"
    tools:context=".LeftFragment">
    
    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="这是左边" />

</FrameLayout>
<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"
    tools:context=".RightFragment">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="这是右边" />

</FrameLayout>

(2)自定义Fragment类,继承自Fragment类或者子类,同时实现onCreateView()方法,在方法中,通过inflater.inflate加载布局文件,接着返回其View。

class LeftFragment : Fragment() {
    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return inflater.inflate(R.layout.fragment_left, container, false)
    }
}
class RightFragment : Fragment() {
    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_right, container, false)
    }
}

(3)在需要加载Fragment控件的Activity对应的布局文件中添加Fragment标签,并设置name属性为自定义fragment。

<LinearLayout 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:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
    <fragment
        android:id="@+id/leftFrag"
        android:name="com.hui.fragment.LeftFragment"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"/>
    <fragment
        android:id="@+id/rightFrag"
        android:name="com.hui.fragment.RightFragment"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"/>

</LinearLayout>

(4)最后在Activity的onCreate()方法中调用setContentView()加载布局。

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
    }
}

3.动态加载


步骤:
(1)通过getSupportFragmentManager()方法获得FragmentManager对象。

  val fragmentManager=supportFragmentManager

(2)开启事务,通过beginTransaction()方法获得FragmentTransaction对象。

val transaction=fragmentManager.beginTransaction()

(3)调用add()方法或者repalce()方法加载Fragment。

 transaction.replace(R.id.rightLayout,fragment)
//replace()方法需要传入容器的id和待添加的Fragment实例

(4)最后调用commit()方法提交事务。

 transaction.commit()

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值