Kotlin Android Studio 中未显示片段
我正在 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>
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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
请尝试此代码删除动画部分。
如果您不需要,
Try this code
Remove the animations part if you don't want it.