Kotlin Android Studio 中未显示片段

发布于 2025-01-14 01:19:03 字数 5829 浏览 0 评论 0原文

我正在 Kotlin Android Studio 中学习 Fragment。我不确定为什么片段没有显示。

问题:

  • 没有显示片段
  • 默认情况下未加载片段一

是相关代码:

MainActivity.kt

class MainActivity : AppCompatActivity() {

// boolean to know which fragment is currently active
var isFragmentOneLoaded = true
val manager = supportFragmentManager

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

    val changeFragmentButton = findViewById<Button>(R.id.changeFragmentButton)

    // so by default Fragment One will be loaded
    ShowFragmentOne()
    
    changeFragmentButton.setOnClickListener{
        if (isFragmentOneLoaded)
            ShowFragmentTwo()
        else
            ShowFragmentOne()
    }
}

fun ShowFragmentOne() {
    val transaction =
        manager.beginTransaction()
    val fragment = FragmentOne()
    transaction.replace(R.id.fragment_holder, fragment)
    transaction.addToBackStack(null)
    transaction.commit()
    isFragmentOneLoaded = true
}

fun ShowFragmentTwo() {
    val transaction =
        manager.beginTransaction()
    val fragment = FragmentTwo()
    transaction.replace(R.id.fragment_holder, fragment)
    transaction.addToBackStack(null)
    transaction.commit()
    isFragmentOneLoaded = false
}

}

FragmentOne.kt

class FragmentOne : Fragment(){

    val TAG = "FragmentOne"

    override fun onAttach(context: Context) {
        Log.d(TAG, "onAttach")
        super.onAttach(context)
    }

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

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        //return super.onCreateView(inflater, container, savedInstanceState)
        Log.d(TAG,"onCreateView")
        return inflater!!.inflate(R.layout.fragment_one,container,false) // !! ---> means it will not be null
    }

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

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

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

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

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

    override fun onDestroyView() {
        Log.d(TAG, "onDestroyView")
        super.onDestroyView()
    }

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

    override fun onDetach() {
        Log.d(TAG, "onDetach")
        super.onDetach()
    }

}

FragmentTwo.kt

class FragmentTwo : Fragment(){

// this TAG will be used to display the log message
val TAG = "FragmentTwo"

... same as Fragment One ...

}

Activity_main.xmlfragment_one.xmlfragment_two.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="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">


    <Button
        android:id="@+id/changeFragmentButton"
        android:layout_width="346dp"
        android:layout_height="wrap_content"
        android:layout_margin="30dp"
        android:text="Change Fragment" />

    <FrameLayout
        android:id="@+id/fragment_holder"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
</LinearLayout>

内容

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

    <TextView
        android:id="@+id/textView"
        android:layout_width="93dp"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_alignParentEnd="true"
        android:layout_alignParentBottom="true"
        android:layout_marginStart="158dp"
        android:layout_marginTop="298dp"
        android:layout_marginEnd="160dp"
        android:layout_marginBottom="414dp"
        android:text="@string/fragment_one" />

</RelativeLayout>

以下

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

    <TextView
        android:id="@+id/textView"
        android:layout_width="93dp"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_alignParentEnd="true"
        android:layout_alignParentBottom="true"
        android:layout_marginStart="160dp"
        android:layout_marginTop="325dp"
        android:layout_marginEnd="158dp"
        android:layout_marginBottom="387dp"
        android:text="@string/fragment_two" />
</RelativeLayout>

这是我单击按钮时 logcat 显示的 当我首先点击按钮当我再次单击按钮

I am learning Fragments in Kotlin Android Studio. I am unsure why the fragments are not showing.

Issues:

  • No Fragments are shown
  • Fragment One not loaded by default

Here are the relevant codes:

MainActivity.kt

class MainActivity : AppCompatActivity() {

// boolean to know which fragment is currently active
var isFragmentOneLoaded = true
val manager = supportFragmentManager

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

    val changeFragmentButton = findViewById<Button>(R.id.changeFragmentButton)

    // so by default Fragment One will be loaded
    ShowFragmentOne()
    
    changeFragmentButton.setOnClickListener{
        if (isFragmentOneLoaded)
            ShowFragmentTwo()
        else
            ShowFragmentOne()
    }
}

fun ShowFragmentOne() {
    val transaction =
        manager.beginTransaction()
    val fragment = FragmentOne()
    transaction.replace(R.id.fragment_holder, fragment)
    transaction.addToBackStack(null)
    transaction.commit()
    isFragmentOneLoaded = true
}

fun ShowFragmentTwo() {
    val transaction =
        manager.beginTransaction()
    val fragment = FragmentTwo()
    transaction.replace(R.id.fragment_holder, fragment)
    transaction.addToBackStack(null)
    transaction.commit()
    isFragmentOneLoaded = false
}

}

FragmentOne.kt

class FragmentOne : Fragment(){

    val TAG = "FragmentOne"

    override fun onAttach(context: Context) {
        Log.d(TAG, "onAttach")
        super.onAttach(context)
    }

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

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        //return super.onCreateView(inflater, container, savedInstanceState)
        Log.d(TAG,"onCreateView")
        return inflater!!.inflate(R.layout.fragment_one,container,false) // !! ---> means it will not be null
    }

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

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

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

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

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

    override fun onDestroyView() {
        Log.d(TAG, "onDestroyView")
        super.onDestroyView()
    }

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

    override fun onDetach() {
        Log.d(TAG, "onDetach")
        super.onDetach()
    }

}

FragmentTwo.kt

class FragmentTwo : Fragment(){

// this TAG will be used to display the log message
val TAG = "FragmentTwo"

... same as Fragment One ...

}

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="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">


    <Button
        android:id="@+id/changeFragmentButton"
        android:layout_width="346dp"
        android:layout_height="wrap_content"
        android:layout_margin="30dp"
        android:text="Change Fragment" />

    <FrameLayout
        android:id="@+id/fragment_holder"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
</LinearLayout>

fragment_one.xml

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

    <TextView
        android:id="@+id/textView"
        android:layout_width="93dp"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_alignParentEnd="true"
        android:layout_alignParentBottom="true"
        android:layout_marginStart="158dp"
        android:layout_marginTop="298dp"
        android:layout_marginEnd="160dp"
        android:layout_marginBottom="414dp"
        android:text="@string/fragment_one" />

</RelativeLayout>

fragment_two.xml

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

    <TextView
        android:id="@+id/textView"
        android:layout_width="93dp"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_alignParentEnd="true"
        android:layout_alignParentBottom="true"
        android:layout_marginStart="160dp"
        android:layout_marginTop="325dp"
        android:layout_marginEnd="158dp"
        android:layout_marginBottom="387dp"
        android:text="@string/fragment_two" />
</RelativeLayout>

Here's what my logcat shows when I click the button
When I first click the button
When I click the button again

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

失眠症患者 2025-01-21 01:19:03

请尝试此代码删除动画部分。

fun setCurrentFragment(fragment: Fragment) {
        supportFragmentManager.beginTransaction().apply {
            setCustomAnimations(
                R.anim.slide_in,
                R.anim.slide_out,
                R.anim.slide_in,
                R.anim.slide_out
            )
            replace(R.id.fragment_container, fragment)
            commit()
        }

如果您不需要,

Try this code

fun setCurrentFragment(fragment: Fragment) {
        supportFragmentManager.beginTransaction().apply {
            setCustomAnimations(
                R.anim.slide_in,
                R.anim.slide_out,
                R.anim.slide_in,
                R.anim.slide_out
            )
            replace(R.id.fragment_container, fragment)
            commit()
        }

Remove the animations part if you don't want it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文