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。

扫描二维码关注公众号,回复: 15377974 查看本文章
<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()

猜你喜欢

转载自blog.csdn.net/weixin_63357306/article/details/128062466