#android #android-layout #bottomnavigationview #android-architecture-navigation #navigationview
Вопрос:
Я использовал NavigationView
и BottomNavigationView
вместе с навигационным компонентом все работает нормально, кроме значка humburger. Когда приложение запускается и я сначала нажимаю на значок humburger, боковая панель не открывается, но если попытаться сдвинуть ее с левой стороны и закрыть, значок humburger начнет работать. Я прикрепил GIF процесса,
Также ниже приводится MainActivity.kt
@AndroidEntryPoint
class MainActivity : AppCompatActivity() {
private lateinit var binding: ActivityMainBinding
private lateinit var navController: NavController
private lateinit var appBarConfiguration: AppBarConfiguration
private var arrayList: ArrayList<TopModel> = arrayListOf()
private lateinit var reviewManager: ReviewManager
private val viewModel by viewModels<SliderViewModel>()
private lateinit var fab: FloatingActionButton
private lateinit var headerBinding: NavHeaderMainBinding
override fun attachBaseContext(newBase: Context?) {
val lang = newBase?.let { Session(it).getLanguage() }
super.attachBaseContext(ContextWrapper(lang?.let { newBase.setAppLocale(it) }))
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
fab = binding.followDone
reviewManager = ReviewManagerFactory.create(this)
binding.apply {
navController = findNavController(R.id.nav_host_fragment)
appBarConfiguration = AppBarConfiguration.Builder(
R.id.nav_post, R.id.nav_center,
R.id.nav_communication, R.id.nav_notification,
R.id.nav_profile, R.id.aboutUs, R.id.privacy
).setOpenableLayout(drawer).build()
setSupportActionBar(toolbar)
setupActionBarWithNavController(navController, appBarConfiguration)
navigationView.setupWithNavController(navController)
navBottomView.setupWithNavController(navController)
val session = Session(this@MainActivity)
headerBinding = NavHeaderMainBinding.inflate(LayoutInflater.from(this@MainActivity))
headerBinding.apply {
if (session.isUserLoggedIn()) {
userName.text = session.getUserName()
userMobile.text = session.getUserPhone()
Glide.with(userProfile).asBitmap()
.circleCrop()
.load(session.getUserImage())
.into(userProfile)
} else {
userName.text = getString(R.string.app_name)
userMobile.text = ""
userProfile.setImageResource(R.mipmap.ic_launcher_foreground)
}
}
navigationView.addHeaderView(headerBinding.root)
navigationViewLogin()
navigationView.setNavigationItemSelectedListener { item ->
when (item.itemId) {
R.id.nav_communication -> {
val navigationOption: NavOptions = NavOptions.Builder()
.setLaunchSingleTop(true)
.setPopUpTo(R.id.nav_communication, false)
.setEnterAnim(R.anim.nav_default_enter_anim)
.setExitAnim(R.anim.nav_default_exit_anim)
.setPopEnterAnim(R.anim.nav_default_pop_enter_anim)
.setPopExitAnim(R.anim.nav_default_pop_exit_anim)
.build()
navController.navigate(R.id.nav_communication, null, navigationOption)
}
R.id.aboutUs -> {
val bundle = Bundle()
bundle.putString("url", "https://starlinedemo.com/khedut_putra/about_us.php")
bundle.putString("displayName", "About us")
navController.navigate(R.id.bajarBhav, bundle)
}
R.id.shareApp -> {
shareApp()
}
R.id.rateUs -> {
showRateApp()
}
R.id.changeLan -> {
if (session.getLanguage().equals("gu", true)) {
item.title = getString(R.string.gujarati)
session.setLanguage("en")
changeLanguage("en")
} else {
item.title = getString(R.string.english)
session.setLanguage("gu")
changeLanguage("gu")
}
}
R.id.privacy -> {
val bundle = Bundle()
bundle.putString("url", "https://starlinedemo.com/khedut_putra/privacy_policy.php")
bundle.putString("displayName", "Privacy Policy")
navController.navigate(R.id.bajarBhav, bundle)
}
R.id.logOut -> {
if (session.isUserLoggedIn()) {
session.setUserLoggedIn(false)
session.setUserName("")
session.setUserPhone("")
session.setUserImage("")
session.setUserId("")
item.title = getString(R.string.login)
navigationViewLogin()
} else {
item.title = getString(R.string.loginout)
navController.navigate(R.id.loginBottomSheet)
}
}
}
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START)
}
return@setNavigationItemSelectedListener true
}
//other code not related to navigationview....
}
override fun onCreateOptionsMenu(menu: Menu?): Boolean {
menuInflater.inflate(R.menu.language_menu, menu)
if (Session(this).getLanguage().equals("gu", true)) {
menu?.findItem(R.id.changeLng)?.title = getString(R.string.english)
} else {
menu?.findItem(R.id.changeLng)?.title = getString(R.string.gujarati)
}
menu?.findItem(R.id.changeLng)?.isVisible = false
return true
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
if (item.itemId == R.id.changeLng) {
if (Session(this).getLanguage().equals("gu", true)) {
item.title = getString(R.string.english)
Session(this).setLanguage("en")
changeLanguage("en")
} else {
item.title = getString(R.string.gujarati)
Session(this).setLanguage("gu")
changeLanguage("gu")
}
return true
}
if (binding.drawer.isDrawerOpen(GravityCompat.START)) {
binding.drawer.closeDrawer(GravityCompat.START)
}
return item.onNavDestinationSelected(navController) || super.onOptionsItemSelected(item)
}
override fun onSupportNavigateUp(): Boolean {
return findNavController(R.id.nav_host_fragment).navigateUp(appBarConfiguration) || super.onSupportNavigateUp()
}
override fun onPause() {
super.onPause()
if (isFinishing) {
binding.unbind()
}
}
override fun onBackPressed() {
if (binding.drawer.isDrawerOpen(GravityCompat.START)) {
binding.drawer.closeDrawer(GravityCompat.START)
} else {
super.onBackPressed()
}
}
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<layout 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">
<androidx.drawerlayout.widget.DrawerLayout
android:id="@ id/drawer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.coordinatorlayout.widget.CoordinatorLayout
android:id="@ id/coordinator"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@id/nav_bottom_view"
app:layout_scrollFlags="scroll|enterAlways|snap">
<com.google.android.material.appbar.AppBarLayout
android:id="@ id/appBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/background"
android:elevation="0dp"
app:elevation="0dp"
app:layout_behavior=".utils.MyScrollBehavior"
app:layout_insetEdge="top">
<androidx.appcompat.widget.Toolbar
android:id="@ id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/background"
app:contentInsetLeft="0dp"
app:contentInsetStart="0dp"
app:contentInsetStartWithNavigation="0dp"
app:logo="@drawable/khedutputra"
app:logoDescription="@string/app_name" />
<View
android:layout_width="match_parent"
android:layout_height="5dp"
android:background="@color/lightGray"
app:layout_scrollFlags="scroll|enterAlways|snap" />
<androidx.recyclerview.widget.RecyclerView
android:id="@ id/recyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:orientation="horizontal"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
app:layout_collapseMode="parallax"
app:layout_collapseParallaxMultiplier="0.3"
app:layout_scrollFlags="scroll|enterAlways|snap"
app:reverseLayout="false"
app:stackFromEnd="false"
tools:listitem="@layout/top_layout_view" />
<View
android:id="@ id/view"
android:layout_width="match_parent"
android:layout_height="5dp"
android:layout_marginTop="5dp"
android:background="@color/lightGray"
app:layout_scrollFlags="scroll|enterAlways|snap" />
</com.google.android.material.appbar.AppBarLayout>
<fragment
android:id="@ id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:layout_behavior="com.google.android.material.appbar.AppBarLayout$ScrollingViewBehavior"
app:navGraph="@navigation/navigation" />
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@ id/followDone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_margin="16dp"
android:contentDescription="@string/follow_done"
android:src="@drawable/ic_baseline_check"
android:visibility="gone"
app:backgroundTint="@color/colorPrimary"
app:fabSize="normal"
app:layout_anchor="@id/nav_host_fragment"
app:layout_anchorGravity="bottom|end"
app:layout_behavior="com.google.android.material.floatingactionbutton.FloatingActionButton$Behavior" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@ id/nav_bottom_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_gravity="bottom"
android:background="?android:attr/windowBackground"
app:itemTextAppearanceInactive="@style/TextAppearance.MaterialComponents.Subtitle2"
app:labelVisibilityMode="labeled"
app:layout_anchorGravity="bottom"
app:layout_insetEdge="bottom"
app:menu="@menu/bottom_nav_menu" />
</RelativeLayout>
<com.google.android.material.navigation.NavigationView
android:id="@ id/navigationView"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
android:visibility="gone"
app:itemBackground="?attr/background"
app:menu="@menu/side_nav_menu" />
</androidx.drawerlayout.widget.DrawerLayout>
</layout>
Navigation.xml:
<?xml version="1.0" encoding="utf-8"?>
<navigation 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:id="@ id/navigation"
app:startDestination="@id/nav_communication">
<fragment
android:id="@ id/nav_communication"
android:name="com.shvet.khedutputra.fragment.CommunicationFragment"
android:label="@string/title_communication"
tools:layout="@layout/fragment_communication">
<action
android:id="@ id/communication_to_full_blog"
app:destination="@id/showFullBlogFragment"
app:enterAnim="@anim/nav_default_enter_anim"
app:exitAnim="@anim/nav_default_exit_anim"
app:launchSingleTop="false"
app:popEnterAnim="@anim/nav_default_pop_enter_anim"
app:popExitAnim="@anim/nav_default_pop_exit_anim"
app:popUpTo="@id/nav_communication"
app:popUpToInclusive="false" />
<action
android:id="@ id/communication_to_following_post"
app:destination="@id/showFollowingPost"
app:enterAnim="@anim/nav_default_enter_anim"
app:exitAnim="@anim/nav_default_exit_anim"
app:launchSingleTop="false"
app:popEnterAnim="@anim/nav_default_pop_enter_anim"
app:popExitAnim="@anim/nav_default_pop_exit_anim"
app:popUpTo="@id/nav_communication"
app:popUpToInclusive="false" />
</fragment>
<fragment
android:id="@ id/nav_center"
android:name="com.shvet.khedutputra.fragment.products.ProductFragment"
android:label="@string/title_center"
tools:layout="@layout/fragment_product">
<action
android:id="@ id/action_nav_center_to_showProduct"
app:destination="@id/showProduct" />
</fragment>
<fragment
android:id="@ id/showProduct"
android:name="com.shvet.khedutputra.fragment.products.ShowProductFragment"
android:label="@string/product"
tools:layout="@layout/fragment_show_product">
<argument
android:name="productData"
app:argType="com.shvet.khedutputra.fragment.products.model.ProductsModel$Data"
app:nullable="false" />
<action
android:id="@ id/action_showProduct_to_cartListFragment"
app:destination="@id/cartListFragment"
app:enterAnim="@anim/nav_default_enter_anim"
app:exitAnim="@anim/nav_default_exit_anim"
app:launchSingleTop="false"
app:popEnterAnim="@anim/nav_default_pop_enter_anim"
app:popExitAnim="@anim/nav_default_pop_exit_anim"
app:popUpTo="@id/showProduct"
app:popUpToInclusive="false" />
</fragment>
<fragment
android:id="@ id/cartListFragment"
android:name="com.shvet.khedutputra.fragment.products.CartListFragment"
android:label="@string/cart" />
<fragment
android:id="@ id/showFullBlogFragment"
android:name="com.shvet.khedutputra.fragment.blog.ShowFullBlogFragment"
android:label="@string/blog_details"
tools:layout="@layout/fragment_show_full_blog">
<action
android:id="@ id/blog_to_comments"
app:destination="@id/showCommentListFragment"
app:enterAnim="@anim/nav_default_enter_anim"
app:exitAnim="@anim/nav_default_exit_anim"
app:launchSingleTop="false"
app:popEnterAnim="@anim/nav_default_pop_enter_anim"
app:popExitAnim="@anim/nav_default_pop_exit_anim"
app:popUpTo="@id/showFullBlogFragment"
app:popUpToInclusive="false" />
<argument
android:name="isBlog"
android:defaultValue="false"
app:argType="boolean" />
<argument
android:name="fullPost"
android:defaultValue="@null"
app:argType="com.shvet.khedutputra.model.AgriInfo$Post"
app:nullable="true" />
<argument
android:name="blogPost"
app:argType="com.shvet.khedutputra.fragment.blog.model.BlogModel$Data"
app:nullable="true" />
</fragment>
<fragment
android:id="@ id/showFollowingPost"
android:name="com.shvet.khedutputra.fragment.follow.ShowFollowingPost"
android:label="@string/title_post"
tools:layout="@layout/fragment_show_following_post">
<argument
android:name="followingPost"
app:argType="com.shvet.khedutputra.fragment.follow.model.FollowingPostModel$Data"
app:nullable="false" />
</fragment>
<fragment
android:id="@ id/nav_post"
android:name="com.shvet.khedutputra.fragment.AskQuestionFragment"
android:label="@string/title_post"
tools:layout="@layout/fragment_ask_question" />
<fragment
android:id="@ id/nav_notification"
android:name="com.shvet.khedutputra.fragment.buysell.BuySellFragment"
android:label="@string/title_notification"
tools:layout="@layout/fragment_buy_sell">
<action
android:id="@ id/buySellToComments"
app:destination="@id/showCommentListFragment"
app:enterAnim="@anim/nav_default_enter_anim"
app:exitAnim="@anim/nav_default_exit_anim"
app:launchSingleTop="false"
app:popEnterAnim="@anim/nav_default_pop_enter_anim"
app:popExitAnim="@anim/nav_default_pop_exit_anim"
app:popUpTo="@id/nav_notification"
app:popUpToInclusive="false" />
<action
android:id="@ id/buySellToSendPost"
app:destination="@id/buySellSendPost"
app:enterAnim="@anim/nav_default_enter_anim"
app:exitAnim="@anim/nav_default_exit_anim"
app:launchSingleTop="false"
app:popEnterAnim="@anim/nav_default_pop_enter_anim"
app:popExitAnim="@anim/nav_default_pop_exit_anim"
app:popUpTo="@id/nav_notification"
app:popUpToInclusive="false" />
</fragment>
<fragment
android:id="@ id/nav_profile"
android:name="com.shvet.khedutputra.fragment.user.ProfileFragment"
android:label="@string/title_profile"
tools:layout="@layout/fragment_profile" />
<fragment
android:id="@ id/showCommentListFragment"
android:name="com.shvet.khedutputra.fragment.ShowCommentListFragment"
android:label="@string/comment"
tools:layout="@layout/fragment_show_comment_list">
<argument
android:name="postId"
android:defaultValue="0"
app:argType="integer"
app:nullable="false" />
<argument
android:name="fromWhere"
app:argType="string"
app:nullable="false" />
</fragment>
<fragment
android:id="@ id/weather"
android:name="com.shvet.khedutputra.fragment.weather.WeatherFragment"
android:label="@string/weather"
tools:layout="@layout/fragment_weather" />
<fragment
android:id="@ id/bajarBhav"
android:name="com.shvet.khedutputra.fragment.BajarBhavFragment"
android:label="{displayName}"
tools:layout="@layout/fragment_bajar_bhav">
<argument
android:name="url"
app:argType="string"
app:nullable="false" />
<argument
android:name="displayName"
app:argType="string"
app:nullable="false" />
</fragment>
<fragment
android:id="@ id/buySellSendPost"
android:name="com.shvet.khedutputra.fragment.buysell.CategoryFragment"
android:label="Choose Category"
tools:layout="@layout/fragment_buy_sell_category">
<action
android:id="@ id/action_buySellSendPost_to_createPost"
app:destination="@id/createPost"
app:enterAnim="@anim/nav_default_enter_anim"
app:exitAnim="@anim/nav_default_exit_anim"
app:launchSingleTop="false"
app:popEnterAnim="@anim/nav_default_pop_enter_anim"
app:popExitAnim="@anim/nav_default_pop_exit_anim"
app:popUpTo="@id/buySellSendPost"
app:popUpToInclusive="false" />
</fragment>
<fragment
android:id="@ id/createPost"
android:name="com.shvet.khedutputra.fragment.buysell.CreatePostFragment"
android:label="Post Here"
tools:layout="@layout/fragment_buy_sell_create_post">
<argument
android:name="categoryId"
app:argType="string"
app:nullable="false" />
</fragment>
<dialog
android:id="@ id/loginBottomSheet"
android:name="com.shvet.khedutputra.utils.LoginBottomSheet"
android:label="@string/login"
tools:layout="@layout/login_bottom_sheet">
<action
android:id="@ id/action_loginBottomSheet_to_otpVerifyFragment"
app:destination="@id/otpVerifyFragment"
app:enterAnim="@anim/nav_default_enter_anim"
app:exitAnim="@anim/nav_default_exit_anim"
app:launchSingleTop="true"
app:popEnterAnim="@anim/nav_default_pop_enter_anim"
app:popExitAnim="@anim/nav_default_pop_exit_anim"
app:popUpTo="@id/loginBottomSheet"
app:popUpToInclusive="true" />
</dialog>
<fragment
android:id="@ id/otpVerifyFragment"
android:name="com.shvet.khedutputra.fragment.user.OtpVerifyFragment"
android:label="@string/otp_verify"
tools:layout="@layout/fragment_otp_verify">
<argument
android:name="mobileNumber"
android:defaultValue="0"
app:argType="string"
app:nullable="false" />
</fragment>
</navigation>
Я пробовал другие решения на stackoverflow. Не работал ни с одним из них. Также попытался создать новый проект и добавить как navigationview, так и bottomnavigationview и получил ту же проблему.Что касается версии навигации-пользовательский интерфейс ares 2.3.5
.
implementation "androidx.navigation:navigation-fragment-ktx:$nav_version"
implementation "androidx.navigation:navigation-ui-ktx:$nav_version"