Remove scrollbar from android support design navig

2020-08-09 17:59发布

问题:

I just updated the support design library from 22.2.1 to 23.0.1 and immediately noticed the presence of a scrollbar in the navigation drawer. I tried to use

android:scrollbars="none"

But that didn't fix it. Is there any other way to remove the scrollbar?

回答1:

Unfortunately the scrollbar is set in the NavigationMenuView layout not in the NavigationView, for this reason if you use android:scrollbars="none" the scrollbar is still present.

You can do it programmatically calling this method:

private void disableNavigationViewScrollbars(NavigationView navigationView) {
    if (navigationView != null) {
        NavigationMenuView navigationMenuView = (NavigationMenuView) navigationView.getChildAt(0);
        if (navigationMenuView != null) {
            navigationMenuView.setVerticalScrollBarEnabled(false);
        }
    }
}


回答2:

You can also use following style in your style.xml

<item name="android:scrollbarThumbVertical">@color/transparent</item>
if you don't have a transparent color defined in colors.xml, use the android library "transparent" with: <item name="android:scrollbarThumbVertical">@android:color/transparent</item>


回答3:

I tried doing this in Kotlin, hope it will be a help. first, create different fragments and any navigation you want to after that create a function which will be used to load fragments into the activity.

    when (item.getItemId()) {
            R.id.home -> {
                //this is the name of the method I am using for adding fragments 
                //with bottom navigation bar you can use it with any type o navigation.
                loadFragment(getString(R.string.home_fragment), HomeFragment());
                appBar.title = "Home"
                return true
            }
            R.id.jobs -> {
                loadFragment(getString(R.string.jobs_fragment), JobsFragment());
                appBar.title = "Jobs"
                return true
            }

after that here is the method

        private fun loadFragment(tag: String,loadFragment: Fragment) {
           val fManager = supportFragmentManager
            val fTransaction = fManager.beginTransaction()
            val fragment = fManager.findFragmentByTag(tag)

            if (fragment == null) {
                fTransaction.replace(R.id.activity_main_content_main, loadFragment,tag);
            } else { // re-use the old fragment
                fTransaction.replace(R.id.activity_main_content_main, fragment, tag);
            }

            fTransaction.addToBackStack(tag);
            fTransaction.commit();

        }

first val fragment = fManager.findFragmentByTag(tag) this will search if the fragment is already loaded then else statement will be executed and the preloaded fragment will be displayed but if not then loadFragment parameter we passed contains the fragment you want to load then if statement will be executed which will load the passed fragment.



回答4:

you can use it in apptheme in style :

<item name="android:scrollbarThumbVertical">@android:color/transparent</item>