我试图通过两种方式在片段中显示 toast,但它不起作用..我已在 GitHub 上附加了该项目的链接
https://github.com/Mussa-Adolai/MyRecycler/tree/edit_project
enter code here
从主要活动中
获取底部NavClick(){ BottomNavigationView?.setOnItemSelectedListener(){
when(it.itemId){
R.id.home -> {
chooseFragment(PersonFragment(),"HOME")
}
R.id.profile->{
chooseFragment(RecyclerFragment(),"PROFILE")
Toast.makeText(this,"recyclerFragment", Toast.LENGTH_SHORT)
}
}
true
}
}
来自片段类
class PersonFragment : Fragment() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
}
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
Toast.makeText(activity,"PersonFragment", Toast.LENGTH_SHORT)
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_person, container, false)
}
https://github.com/Mussa-Adolai/MyRecycler/tree/edit_project
enter code here
from main activity
fun bottomNavClick(){
bottomNavigationView?.setOnItemSelectedListener(){
when(it.itemId){
R.id.home -> {
chooseFragment(PersonFragment(),"HOME")
}
R.id.profile->{
chooseFragment(RecyclerFragment(),"PROFILE")
Toast.makeText(this,"recyclerFragment", Toast.LENGTH_SHORT)
}
}
true
}
}
from fragment class
class PersonFragment : Fragment() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
}
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
Toast.makeText(activity,"PersonFragment", Toast.LENGTH_SHORT)
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_person, container, false)
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论