Remove scrollbar from android support design navigation drawer?

断了今生、忘了曾经 提交于 2020-02-01 17:25:46

问题


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>



来源:https://stackoverflow.com/questions/32673303/remove-scrollbar-from-android-support-design-navigation-drawer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!