第一行代码第三版,学习到Fragment的动态添加,由于button.setOn的插件方法已经废弃了,然后像往常一样
使用findViewById(R.id.button),
启动就闪退,不要button相关代码就能正常启动,试着在LeftFragent类里添加了viewbinding插件,
binding.button.setOnClickListener{},binding显示红色没能正常引用.
MainActivity.kt
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
binding.button.setOnClickListener {
replaceFragment(AnotherRightFragment())
}
replaceFragment(RightFragment())
}
private fun replaceFragment(fragment: Fragment) {
val fragmentManager = supportFragmentManager
val transaction = fragmentManager.beginTransaction()
transaction.replace(R.id.rightLayout, fragment)
transaction.commit()
}
}
LeftFragment.kt
class LeftFragment : Fragment() {
private var _binding: LeftFragmentBinding? = null
private val binding get() = _binding!!
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View {
_binding = LeftFragmentBinding.inflate(inflater, container, false)
return binding.root
}
override fun onDestroyView() {
super.onDestroyView()
_binding = null
}
}
RightFragment.kt
class RightFragment: Fragment() {
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
return inflater.inflate(R.layout.right_fragment, container, false)
}
}
AnotherRightFragment.kt
class AnotherRightFragment: Fragment() {
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
return inflater.inflate(R.layout.another_right_fragment, container, false)
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.LinearLayoutCompat xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<androidx.fragment.app.FragmentContainerView
android:id="@+id/leftFrag"
android:name="android.example.fragmenttest.LeftFragment"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent" />
<FrameLayout
android:id="@+id/rightLayout"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent" >
</FrameLayout>
</androidx.appcompat.widget.LinearLayoutCompat>
left_fragent.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.LinearLayoutCompat xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:backgroundTint="#868686"
android:text="@string/button"/>
</androidx.appcompat.widget.LinearLayoutCompat>
right_fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.LinearLayoutCompat xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#00ff00">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="@string/right_fragment"
android:textSize="24sp"/>
</androidx.appcompat.widget.LinearLayoutCompat>
another_right_fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.LinearLayoutCompat xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#ffff00">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/another_right_fragment"
android:textSize="24sp"
android:layout_gravity="center_horizontal"/>
</androidx.appcompat.widget.LinearLayoutCompat>
请教各位,如果分别用findViewById和viewbinding两种方法,该如何实现呢。
表头 | 表头 |
---|---|
单元格 | 单元格 |
单元格 | 单元格 |