笔记:实现简单fragment

SimpleFragment

主界面包含Button、Fragmentlayout控件,按钮按下,fragment出现。

1.主界面。

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    tools:context=".MainActivity"
    android:gravity="center">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:gravity="center"
        android:background="#66666686">
        <TextView
            android:id="@+id/textView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="activity_main" />
        <Button
            android:id="@+id/button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="button" />
    </LinearLayout>
    <FrameLayout
        android:id="@+id/fragmentlayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:backgroundTint="@color/black" />

</LinearLayout>

2.新建一个fragment_simple布局文件和SimpleFragment类:

fragment_simple.xml:

<?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"
    android:background="#34883444"
    android:gravity="center">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="this is a fragment"/>


</LinearLayout>

 SimpleFragment.kt:

package com.example.simplefragment

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
//继承自Fragment()
class SimpleFragment : Fragment() {
    //重写onCreateView方法,包含三个参数
    //LayoutInflater inflater:这个参数用于将布局XML资源转换为View对象。通常在方法内部使用它来膨胀(inflate)由container参数提供的布局资源。
    //ViewGroup container:这个参数是Fragment的UI应该附加到的父ViewGroup。它提供了Fragment所处的上下文(context)。
    //Bundle savedInstanceState:这个参数是一个Bundle对象,包含Fragment的保存状态。如果Fragment是从之前的保存状态重新创建的,则此Bundle包含该数据。否则,它为null。
    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        val inflater = inflater.inflate(R.layout.fragment_simple, container, false)
        return inflater
    }
}

3.MainActivity中编写监听事件。

MainActivity.kt:

package com.example.simplefragment

import android.os.Bundle
import android.widget.Button
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(R.layout.activity_main)
        val button = findViewById<Button>(R.id.button)
        button.setOnClickListener{
            showFragment(SimpleFragment())
        }
    }

    private fun showFragment(fragment: SimpleFragment) {
        // 获取 FragmentManager 实例
        val fragmentManager = supportFragmentManager
        // 开启一个事务
        val transaction = fragmentManager.beginTransaction()
        // 用传入的 fragment 替换指定的布局资源 ID(R.id.fragmentlayout)所在的位置
        transaction
            .replace(R.id.fragmentlayout, fragment)
            .commit()
    }
}

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值