gpt4 book ai didi

android - Material 设计中的底部抽屉

转载 作者:行者123 更新时间:2023-11-29 02:25:21 24 4
gpt4 key购买 nike

class BottomNavigationDrawerFragment: BottomSheetDialogFragment() {

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.fragment_bottomsheet, container, false)
}
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
navigation_view.setNavigationItemSelectedListener { menuItem ->
// Bottom Navigation Drawer menu item clicks
when (menuItem!!.itemId) {
R.id.nav1 -> context!!.toast("you clicked one")
R.id.nav2 -> context!!.toast("you clicked two")
R.id.nav3 -> context!!.toast("you clicked three")
}
true
}


}

// This is an extension method for easy Toast call
fun Context.toast(message: CharSequence) {
val toast = Toast.makeText(this, message, Toast.LENGTH_SHORT)
toast.setGravity(Gravity.BOTTOM, 0, 600)
toast.show()
}

}

我想要实现的是在单击底部应用栏中的导航图标后创建一个模态 Bottom Sheet 并在其中显示一个抽屉导航。在上面的代码中我保留了三个项目。到这里一切都还好但是当涉及到处理项目点击部分时,行:

navigation_view.setNavigationItemSelectedListener { menuItem -> 

显示错误。它告诉:

unresolved type:setNavigationItemSelectedListener

未解析的类型:menuItem

这是fragment_bottomsheet.xml:

<android.support.design.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">


<com.google.android.material.navigation.NavigationView
android:id="@+id/navigation_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:menu="@menu/bottom_nav_drawer_menu"/>

</android.support.design.widget.ConstraintLayout>

我在这里做错了什么?

最佳答案

问题似乎来自 Context.toast(message: CharSequence),您可以为此使用 anko:

implementation "org.jetbrains.anko:anko:0.10.6"

无论如何,您可以通过以下方式检查输出:

Toast.makeText(context, "Clicked", Toast.LENGTH_LONG).show()

你会发现它会起作用。

更新:

尝试在您的 BottomNavigationDrawerFragment 中使用它:

 override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)

navigation_view.setNavigationItemSelectedListener { menuItem ->
// Bottom Navigation Drawer menu item clicks
when (menuItem.itemId) {

R.id.home ->{

Toast.makeText(context, "Clicked", Toast.LENGTH_LONG).show()
}
// R.id.nav1 -> context!!.toast(getString(R.string.nav1_clicked))
}
// Add code here to update the UI based on the item selected
// For example, swap UI fragments here
true
}

close_imageview.setOnClickListener {
this.dismiss()
}

disableNavigationViewScrollbars(navigation_view)

}

关于android - Material 设计中的底部抽屉,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52536227/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com