单击菜单中的按钮后如何切换到使用数据绑定的片段
how to switch after clicking on the button in the menu to a fragment in which data binding is used
enter image description here
enter image description here
class MainActivity : AppCompatActivity() {
lateinit var 绑定:ActivityMainBinding
重写乐趣 onCreate(savedInstanceState: Bundle?) {
super.onCreate(已保存实例状态)
绑定 = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
}
override fun onCreateOptionsMenu(menu: Menu?): Boolean {
menuInflater.inflate(R.menu.bottom_nav_bar_main , menu)
return true
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
val intent : Intent
when(item.itemId) {
R.id.itemCourses -> {
val fragment = CoursesFragment.newInstance()
}
}
return true
}
}
class CoursesFragment : Fragment() {
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val binding = FragmentCoursesBinding.inflate(inflater , container , false)
return binding.root
}
}
第 1 步:将 FragmentContainerView 添加到您的 activity xml
<androidx.fragment.app.FragmentContainerView
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
第 2 步:在您的 MainActivity.class 文件中,声明 FragmentManager
private FragmentManager manager;
第 3 步:在 onCreate() 中初始化 FragmentManager
manager = getSupportFragmentManager();
第 4 步:在您的 onOptionsItemSelected 中开始此片段
Bundle bundle = new Bundle();
manager.beginTransaction()
.replace(R.id.container, YourFragment.class, bundle, "TAG")
.setReorderingAllowed(true)
//.setCustomAnimations(R.anim.anim_enter, R.anim.anim_exit)
.addToBackStack("TAG")
.commit();
enter image description here
enter image description here
class MainActivity : AppCompatActivity() { lateinit var 绑定:ActivityMainBinding 重写乐趣 onCreate(savedInstanceState: Bundle?) { super.onCreate(已保存实例状态) 绑定 = ActivityMainBinding.inflate(layoutInflater) setContentView(binding.root) }
override fun onCreateOptionsMenu(menu: Menu?): Boolean {
menuInflater.inflate(R.menu.bottom_nav_bar_main , menu)
return true
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
val intent : Intent
when(item.itemId) {
R.id.itemCourses -> {
val fragment = CoursesFragment.newInstance()
}
}
return true
}
}
class CoursesFragment : Fragment() {
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val binding = FragmentCoursesBinding.inflate(inflater , container , false)
return binding.root
}
}
第 1 步:将 FragmentContainerView 添加到您的 activity xml
<androidx.fragment.app.FragmentContainerView
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
第 2 步:在您的 MainActivity.class 文件中,声明 FragmentManager
private FragmentManager manager;
第 3 步:在 onCreate() 中初始化 FragmentManager
manager = getSupportFragmentManager();
第 4 步:在您的 onOptionsItemSelected 中开始此片段
Bundle bundle = new Bundle();
manager.beginTransaction()
.replace(R.id.container, YourFragment.class, bundle, "TAG")
.setReorderingAllowed(true)
//.setCustomAnimations(R.anim.anim_enter, R.anim.anim_exit)
.addToBackStack("TAG")
.commit();