美文网首页
Fragment的使用

Fragment的使用

作者: 别看后面有人 | 来源:发表于2021-06-27 23:11 被阅读0次

Fragment是一种可以嵌入在Activity当中的UI片段,它能让程序更加合理充分利用大屏的空间。示例一个Fragment的简单写法,在Activity中添加两个Fragment,并让这俩个Fragment平分Activity的空间。
1.通过布局添加
新建一个左侧的Fragment的布局:left_fragment.xml,代码如下:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="match_parent"
    android:orientation="vertical"
    android:layout_height="match_parent">
<Button
    android:id="@+id/button"
    android:layout_gravity="center"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Button"/>
</LinearLayout>

新建一个左侧的Fragment的布局:right_fragment.xml,代码如下:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:background="#00ff00"
    android:layout_height="match_parent">
<TextView
    android:layout_gravity="center_horizontal"
    android:textSize="24sp"
    android:text="this is right fragment"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>
</LinearLayout>

左边编写的LeftFragment代码如下:

class LeftFragment: Fragment() {
    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return inflater.inflate(R.layout.left_fragment,container,false)
    }
}

右边编写的RightFragment代码如下:

class RightFragment :Fragment() {
    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return inflater.inflate(R.layout.right_fragment,container,false)
    }
}

activity中引用:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:orientation="horizontal"
    android:layout_height="match_parent">
    <fragment
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:name="com.app.activitytest.fragment.LeftFragment"/>
    <FrameLayout
        android:id="@+id/right_fragment"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
       />
</LinearLayout>

2.动态添加步骤:
1>创建待添加Fragment的实例
2>获取FragmentManager,在Activity中可以直接调用getSupportFragmentManage()方法获取
3>开启一个事务,通过调用beginTransation()方法开启
4>向容器添加或者替换Fragment,一般是通过replce方法实现,需要传入容器的id和待添加的Fragment实例
5>提交事务,调用commit()方法来完成
如果想要程序在按下back键之后不直接退出,可以使用addtoBackStack()方法,将一个事务添加到返回栈中,该方法中可以需要一个用于描述栈返回状态的名字,一般用null

  private fun replaceFragment(fragment:Fragment){
        val fragmentManager=supportFragmentManager
        val transaction=fragmentManager.beginTransaction()
        transaction.replace(R.id.right_fragment,fragment)
        transaction.addToBackStack(null)
        transaction.commit()
    }

相关文章

网友评论

      本文标题:Fragment的使用

      本文链接:https://www.haomeiwen.com/subject/ltexultx.html