欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

02-Fragment+布局限定符

程序员文章站 2022-03-07 18:21:01
1.初级Fragment

1.初级Fragment

<?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">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:text="TextView"/>
</LinearLayout>

<!--右省略-->

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <!--必须有一个独一无二的id-->
    <fragment
        android:id="@+id/leftFragment"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:name="com.my.fragmenttest.LeftFragment"/>

    <fragment
        android:id="@+id/rightFragment"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:name="com.my.fragmenttest.RightFragment"/>
</LinearLayout>
class LeftFragment : Fragment() {

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

//右省略

2.动态替换Fragment

<?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">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:text="TextView"/>
</LinearLayout>

<!--右省略-->
<!--Another Fragment省略-->

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <!--必须有一个独一无二的id-->
    <fragment
        android:id="@+id/leftFragment"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:name="com.my.fragmenttest.LeftFragment"/>

    <FrameLayout
        android:id="@+id/rightFragment"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"/>
</LinearLayout>
class LeftFragment : Fragment() {

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

//右省略
//Another Fragment省略

class MainActivity : AppCompatActivity() {

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

        leftFragmentButton.setOnClickListener {
            replaceFragment(AnotherFragment())
        }
        replaceFragment(RightFragment())
    }

    /**
     * 替换Fragment
     */
    private fun replaceFragment(fragment : Fragment){
        val fragmentManager = supportFragmentManager
        val transaction = fragmentManager.beginTransaction()
        transaction.replace(R.id.rightFragment,fragment)
        transaction.commit()
    }
}

3.Fragment返回栈

private fun replaceFragment(fragment : Fragment){
    val fragmentManager = supportFragmentManager
    val transaction = fragmentManager.beginTransaction()
    transaction.replace(R.id.rightFragment,fragment)
    //将Fragment加入返回栈,不然通过按钮添加Fragment后,点击返回按钮,程序直接退出
    transaction.addToBackStack(null)
    transaction.commit()
}

4.Fragment和Activity的交互

//在Activity中获取Fragment
val rightFragment =
supportFragmentManager.findFragmentById(R.id.rightFragment) as RightFragment
//findFragmentById和findViewById相同,可以省略
val f = rightFragment as RightFragment

//Fragment中获取Activity
if (activity != null){
    val mainActivity = activity as MainActivity
}

5.Fragment声明周期

class AnotherFragment : Fragment() {

    companion object {
        const val TAG = "AnotherFragment"
    }

    /**
     * Fragment和Activity建立关系是调用
     */
    override fun onAttach(context: Context) {
        super.onAttach(context)
        Log.e(TAG,"onAttach")
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        Log.e(TAG,"onCreate")
    }

    /**
     * 为Fragment创建视图时调用
     */
    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        Log.e(TAG,"onCreateView")
        return inflater.inflate(R.layout.another_right_framgent,container,false)
    }

    /**
     * Activity被创建时调用
     */
    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        Log.e(TAG,"onActivityCreated")
    }

    override fun onStart() {
        super.onStart()
        Log.e(TAG,"onStart")
    }

    override fun onResume() {
        super.onResume()
        Log.e(TAG,"onResume")
    }

    override fun onPause() {
        super.onPause()
        Log.e(TAG,"onPause")
    }

    override fun onStop() {
        super.onStop()
        Log.e(TAG,"onStop")
    }

    /**
     * 当与Fragment相关联的Activity被移除时调用
     */
    override fun onDestroyView() {
        super.onDestroyView()
        Log.e(TAG,"onDestroyView")
    }

    override fun onDestroy() {
        super.onDestroy()
        Log.e(TAG,"onDestroy")
    }

    /**
     * 当Fragment和Activity接触关联时调用
     */
    override fun onDetach() {
        super.onDetach()
        Log.e(TAG,"onDetach")
    }
}

6.布局限定符

1:layout_large。
2:layout_sw600dp。

本文地址:https://blog.csdn.net/qq_44637792/article/details/107900738