我正在Kotlin Android Studio学习片段。我不知道为什么碎片没有显示出来。
问题:
以下是有关守则:
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>
以下是我点击按钮时logcat显示的内容
发布于 2022-03-14 21:53:19
试试这段代码
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()
}
删除动画部分,如果你不想要的话。
https://stackoverflow.com/questions/71472418
复制相似问题