Android development, using kotlin to learn Fragment

1. Introduction to Fragment


Android introduced the Fragment function in version 3.0, which is very similar to Activity and can contain layouts like Activity.

Its original intention is to adapt to large-screen tablet computers. Using Fragment, we can divide the screen into several pieces and make reasonable use of the screen space.

Fragment is usually nested in Activity.

2. Static loading


step:

(1) Define the layout file of the Fragment control.

<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) Customize the Fragment class, inherit from the Fragment class or subclass, and implement the onCreateView() method at the same time. In the method, load the layout file through inflater.inflate, and then return its 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) Add a Fragment tag to the layout file corresponding to the Activity that needs to load the Fragment control, and set the name attribute to a custom 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) Finally, call setContentView() in the Activity's onCreate() method to load the layout.

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

3. Dynamic loading


Steps:
(1) Obtain the FragmentManager object through the getSupportFragmentManager() method.

  val fragmentManager=supportFragmentManager

(2) Start the transaction and obtain the FragmentTransaction object through the beginTransaction() method.

val transaction=fragmentManager.beginTransaction()

(3) Call the add() method or the repalce() method to load the Fragment.

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

(4) Finally call the commit() method to commit the transaction.

 transaction.commit()

Guess you like

Origin blog.csdn.net/weixin_63357306/article/details/128062466