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()
}
}
我想要实现的是单击底部应用程序栏中的导航图标后,创建了一个模态底部工作表并在其中显示了一个导航抽屉。在上面的代码中,我在其中保留了三项。到这里一切都还可以,但是当要处理项目单击部分时,则行:
navigation_view.setNavigationItemSelectedListener { menuItem ->
显示错误。它告诉:
unresolved type:setNavigationItemSelectedListener
还有unresolved type in: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>
我在这里做什么错了?
答案 0 :(得分:0)
问题似乎来自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)
}
答案 1 :(得分:0)
下面的方法对我有用。
const functions = require("firebase-functions");
const admin = require("firebase-admin");
admin.initializeApp();
exports.addAdmin = functions.https.onCall(async (data, context) => {
const result = await checkIfUserWithEmailExists(data.email);
if (result === true) {
if (context.auth.token.admin !== true) {
return {
error: "Request not authorized. User must be an admin to fulfill"
};
}
const email_1 = data.email;
return grantAdminRole(email_1).then(() => {
return {
result: `Request fulfilled! ${email_1} is now an admin`
};
});
} else {
return {
error: "No user with this email was found"
};
}
});
async function grantAdminRole(email_2) {
// get user and add custom claim (admin)
const user = await admin.auth().getUserByEmail(email_2);
if (user.customClaims && user.customClaims.admin === true) {
return;
}
return admin.auth().setCustomUserClaims(user.uid, {
admin: true
});
}
//Checks that the email passed in is an existing user
async function checkIfUserWithEmailExists(email) {
const userCollectionRef = admin.firestore().collection("users");
userCollectionRef
.where("email", "==", email)
.get()
.then(querySnapshot => {
if (querySnapshot.size === 1) {
return true;
} else {
return false;
}
});
}