Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
247 views
in Technique[技术] by (71.8m points)

xml - How is posible to implement a DrawerLayout and a bottomNavigation in the same activity in Kotlin?

Honestly, what I'm looking for is for my app to look like the finance app from Yahoo!

Very recently I'm working on an App that requires a Bottom navigation that use 4 different fragments and a menu (sidesman_nav and bottom_navigation_menu) and a DrawerLayout with a different NavigationView and menu (mobile_navigation and main_drawer_menu) that connects to another fragment or intents. So far I have not been able to fully implement mobile_navigation that works fine at all.

I was able to implement the bottom navigation it works perfectly, in other hand I was able to implement the Drawer Layout with their menu, but when I try to navigate to the fragments of the drawerlayout it doesn't go back to the last state, in other words it doesn't go back to the fragment that shows the bottom navigation and close the app.
Now I'm using the fragment element in content_main.xml to show the fragments of bottom_navigation as NavHostFragment and to show the fragments of items related to the DrawerLayout I'm using the ConstratinLayout with id container. So I am able to go the fragments of the DrawerLayout but it is no possible to see the back button at the app bar and when I go back it close the app completely.
So may I need to implement another fragment but I don't know if it will be correct and also I no have idea of how it is possible to implement. Another idea I had was to implement the onBackPressed function in the DrawerLayout fragments or in the Main Activity itself, at the end I'am open to any other solution just, I need to navigate between the fragments of the bottom navigation without losing their state (that's done) and when the user goes to a fragment of the drawer layout, they can go back to the last state by one of the these three possible ways:

  1. Looking at the back button at the app bar
  2. Tapping the buttons of the bottom navigation
  3. just go back

Bottom navigation working
Drawer layout showing
Actual state showing the fragment but cant go back and the bottom butons not working.

This is my implementation:

Code

MainActivity.kt

class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelectedListener {  

    private val homeFragment = HomeFragment()
    private val newxFragment = NewxFragment()
    private val searchFragment = SearchFragment()
    private val marketsFragment = MarketsFragment()
    private val fragmentManager = supportFragmentManager

    private var activeFragment: Fragment = homeFragment

    private lateinit var appBarConfiguration: AppBarConfiguration

    private lateinit var fusedLocationClient: FusedLocationProviderClient

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        val toolbar: Toolbar = findViewById(R.id.toolbar)
        setSupportActionBar(toolbar)

        val drawerLayout: DrawerLayout = findViewById(R.id.drawer_layout)
        val navView: NavigationView = findViewById(R.id.nav_view)
        val navController = findNavController(R.id.nav_host_fragment)
        appBarConfiguration = AppBarConfiguration(navController.graph, drawerLayout)
        setupActionBarWithNavController(navController, appBarConfiguration)
        navView.setupWithNavController(navController)
        navView.setNavigationItemSelectedListener(this)


        fragmentManager.beginTransaction().apply {
            add(R.id.nav_host_fragment, newxFragment, getString(R.string.newxs)).hide(newxFragment)
            add(R.id.nav_host_fragment, searchFragment, getString(R.string.search)).hide(searchFragment)
            add(R.id.nav_host_fragment, marketsFragment, getString(R.string.markets)).hide(marketsFragment)
            add(R.id.nav_host_fragment, homeFragment, getString(R.string.home))
        }.commit()
        initListeners()

    }

    override fun onBackPressed() {
        if (drawer_layout.isDrawerOpen(GravityCompat.START)) {
            drawer_layout.closeDrawer(GravityCompat.START)
        }
        else{
            super.onBackPressed()
        }
    }


    private fun initListeners() {
        bottomNavigationView.setOnNavigationItemSelectedListener { menuItem ->
            when (menuItem.itemId) {
                R.id.homeFragment -> {
                    fragmentManager.beginTransaction().hide(activeFragment).show(homeFragment)
                        .commit()
                    activeFragment = homeFragment
                    toolbar.title = "Home"
                    true
                }
                R.id.newxFragment -> {
                    fragmentManager.beginTransaction().hide(activeFragment).show(newxFragment)
                        .commit()
                    activeFragment = newxFragment
                    toolbar.title = "Newx"
                    true
                }
                R.id.searchFragment -> {
                    fragmentManager.beginTransaction().hide(activeFragment).show(searchFragment)
                        .commit()
                    activeFragment = searchFragment

                    toolbar.title = "Search"
                    true
                }
                R.id.marketsFragment -> {
                    fragmentManager.beginTransaction().hide(activeFragment).show(marketsFragment)
                        .commit()
                    activeFragment = marketsFragment

                    toolbar.title = "Markets"
                    true
                }

                else -> false
            }
        }
    }


    override fun onNavigationItemSelected(item: MenuItem): Boolean {

        var fragment: Fragment? = null
        when (item.itemId) {

            R.id.info -> {
                fragment = InfoFragment()
                toolbar.title = "Info"
            }
            R.id.configuration -> {
                fragment = ConfigurationFragment()
                toolbar.title = "Configuration"
            }
            R.id.notification -> {
                fragment = NotificationFragment()
                toolbar.title = "Notification"
            }
            R.id.comments -> {
                fragment = CommentsFragment()
                toolbar.title = "Comments"
            }
            R.id.help -> {
                fragment = HelpFragment()
                toolbar.title = "Help"
            }
            R.id.cond_priv -> {
                fragment = CondPrivFragment()
                toolbar.title = "Conditions and Privacy"
            }
        }
        if (fragment != null) {
            fragmentManager.beginTransaction().replace(R.id.container, fragment)
                .commit()
        }
        drawer_layout.closeDrawer(GravityCompat.START)
        return true
    }

    override fun onSupportNavigateUp(): Boolean {
        val navController = findNavController(R.id.nav_host_fragment)
        return navController.navigateUp(appBarConfiguration) || super.onSupportNavigateUp()
    }

    public override fun onPause() {
        super.onPause()
    }

    public override fun onResume() {
        super.onResume()
    }

    public override fun onDestroy() {
        super.onDestroy()
    }
}

Layout

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout 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/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">
        <include
            layout="@layout/app_bar_main"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </androidx.constraintlayout.widget.ConstraintLayout>

    <com.google.android.material.navigation.NavigationView

        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header_main"
        app:menu="@menu/main_drawer_menu">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="@dimen/mdtp_date_picker_title_padding"
            android:paddingBottom="@dimen/activity_vertical_margin"
            android:layout_gravity="bottom"
            android:text="@string/developer"
            android:textAlignment="center"
            android:textColor="@color/colorThree"
            tools:ignore="RtlCompat" />
    </com.google.android.material.navigation.NavigationView>

</androidx.drawerlayout.widget.DrawerLayout>

app_bar_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout 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"
    tools:context=".MainActivity">

    <com.google.android.material.appbar.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <androidx.appcompat.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

    </com.google.android.material.appbar.AppBarLayout>

    <include layout="@layout/content_main"/>

</androidx.coordinatorlayout.widget.CoordinatorLayout>

content_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://s

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Instead of placing the bottom navigation in the activity, you have to put it inside the fragments which are showing according to the drawer menu. I am not going into detail as it is simple and you could just work it out.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

56.9k users

...