mikepenz / MaterialDrawer

The flexible, easy to use, all in one drawer library for your Android project. Now brand new with material 2 design.
https://mikepenz.dev
Apache License 2.0
11.67k stars 2.05k forks source link

drawer does not clear previous selected items #2713

Closed integral0909 closed 3 years ago

integral0909 commented 3 years ago

Thanks for providing this great library.

I have created the slider by the following code;

        binding.slider.apply {
            itemAdapter.add(
                PrimaryDrawerItem().apply {
                    nameRes = R.string.quotes; iconRes = R.drawable.ic_quotes
                    identifier = 1; selectedIconRes = R.drawable.ic_quotes_red; isIconTinted = true
                },
                PrimaryDrawerItem().apply {
                    nameRes = R.string.chart; iconRes = R.drawable.ic_chart
                    identifier = 2; selectedIconRes = R.drawable.ic_chart_red; isIconTinted = true
                },
                PrimaryDrawerItem().apply {
                    nameRes = R.string.deals; iconRes = R.drawable.ic_deals
                    identifier = 3; selectedIconRes = R.drawable.ic_deals_red; isIconTinted = true
                },
                PrimaryDrawerItem().apply {
                    nameRes = R.string.net_deals; iconRes = R.drawable.ic_net_deals
                    identifier = 4; selectedIconRes = R.drawable.ic_net_deals_red; isIconTinted = true
                },
                PrimaryDrawerItem().apply {
                    nameRes = R.string.history; iconRes = R.drawable.ic_history
                    identifier = 5; selectedIconRes = R.drawable.ic_history_red; isIconTinted = true
                },
                PrimaryDrawerItem().apply {
                    nameRes = R.string.accounts; iconRes = R.drawable.ic_accounts
                    identifier = 6; selectedIconRes = R.drawable.ic_accounts_red; isIconTinted = true
                },
       }

but my slider does not clear previous selected items, so it is showing multiple selected items like the following. image

Please let me know the reason. Thanks.

mikepenz commented 3 years ago

@sweetdream0729 do you also set a click listener or similar? Trying a similar setup in the sample app does not result in this problem.

Could you please describe a little bit more on how it occurs? with simple clicks? or if you click around quick?

integral0909 commented 3 years ago

Yes, I have added a itemClickListener like the following

            onDrawerItemClickListener = { _, drawerItem, _ ->
                var intent: Intent? = null
                when (drawerItem.identifier) {
                    1L -> binding.bottomNav.selectedItemId = R.id.item_quotes

                    3L -> binding.bottomNav.selectedItemId = R.id.item_deals

                    5L -> binding.bottomNav.selectedItemId = R.id.item_history

                    else -> supportFragmentManager.beginTransaction().replace(
                        R.id.container,
                        QuotesFragment.newInstance()
                    ).commit()
                }
                if (intent != null) {
                    this@MainActivity.startActivity(intent)
                }
                false
            }

If I click item as normal speed, above duplication happens, but if I click the items as longer press, only last clicked item is selected. Thanks for your reply.

mikepenz commented 3 years ago

May you please be able to showcase the whole drawer configuration? the onDrawerItemClickListener returning false also looks fine.

it would be great if I was able to reproduce the problem

integral0909 commented 3 years ago

Here is a drawer configuration

    fun setDrawer(savedInstanceState: Bundle?){
        val profile = ProfileDrawerItem().apply { nameText = "Ark Demo"; descriptionText = "theteam@arktechltd.com"; iconRes = R.drawable.ic_logo }

        // Create the AccountHeader
        headerView = AccountHeaderView(this, compact = true).apply {
            attachToSliderView(binding.slider)
            addProfiles(
                profile,
            )
            withSavedInstance(savedInstanceState)
        }

        binding.slider.apply {
            itemAdapter.add(
                PrimaryDrawerItem().apply {
                    nameRes = R.string.quotes; iconRes = R.drawable.ic_quotes
                    identifier = 1; selectedIconRes = R.drawable.ic_quotes_red; isIconTinted = true
                },
                PrimaryDrawerItem().apply {
                    nameRes = R.string.chart; iconRes = R.drawable.ic_chart
                    identifier = 2; selectedIconRes = R.drawable.ic_chart_red; isIconTinted = true
                },
                PrimaryDrawerItem().apply {
                    nameRes = R.string.deals; iconRes = R.drawable.ic_deals
                    identifier = 3; selectedIconRes = R.drawable.ic_deals_red; isIconTinted = true
                },
                PrimaryDrawerItem().apply {
                    nameRes = R.string.net_deals; iconRes = R.drawable.ic_net_deals
                    identifier = 4; selectedIconRes = R.drawable.ic_net_deals_red; isIconTinted = true
                },
                PrimaryDrawerItem().apply {
                    nameRes = R.string.history; iconRes = R.drawable.ic_history
                    identifier = 5; selectedIconRes = R.drawable.ic_history_red; isIconTinted = true
                },
                PrimaryDrawerItem().apply {
                    nameRes = R.string.accounts; iconRes = R.drawable.ic_accounts
                    identifier = 6; selectedIconRes = R.drawable.ic_accounts_red; isIconTinted = true
                },
                PrimaryDrawerItem().apply {
                    nameRes = R.string.economic_calendar; iconRes = R.drawable.ic_calendar
                    identifier = 7; selectedIconRes = R.drawable.ic_calendar_red; isIconTinted = true
                },
                PrimaryDrawerItem().apply {
                    nameRes = R.string.news; iconRes = R.drawable.ic_news
                    identifier = 8; selectedIconRes = R.drawable.ic_news_red; isIconTinted = true
                },
                PrimaryDrawerItem().apply {
                    nameRes = R.string.mails; iconRes = R.drawable.ic_mails
                    identifier = 9; selectedIconRes = R.drawable.ic_mails_red; isIconTinted = true
                },
                PrimaryDrawerItem().apply {
                    nameRes = R.string.settings; iconRes = R.drawable.ic_settings
                    identifier = 10; selectedIconRes = R.drawable.ic_settings_red; isIconTinted = true
                },
                PrimaryDrawerItem().apply {
                    nameRes = R.string.about; iconRes = R.drawable.ic_about
                    identifier = 11; selectedIconRes = R.drawable.ic_about_red; isIconTinted = true
                },
                PrimaryDrawerItem().apply {
                    nameRes = R.string.share; iconRes = R.drawable.ic_share
                    identifier = 12; selectedIconRes = R.drawable.ic_share_red; isIconTinted = true
                },
            )
            onDrawerItemClickListener = { _, drawerItem, _ ->
                //check if the drawerItem is set.
                //there are different reasons for the drawerItem to be null
                //--> click on the header
                //--> click on the footer
                //those items don't contain a drawerItem

                var intent: Intent? = null
                when (drawerItem.identifier) {
                    1L -> binding.bottomNav.selectedItemId = R.id.item_quotes

                    3L -> binding.bottomNav.selectedItemId = R.id.item_deals

                    5L -> binding.bottomNav.selectedItemId = R.id.item_history

                    else -> supportFragmentManager.beginTransaction().replace(
                        R.id.container,
                        QuotesFragment.newInstance()
                    ).commit()
                }
                if (intent != null) {
                    this@MainActivity.startActivity(intent)
                }
                false
            }
            setSavedInstance(savedInstanceState)
        }

        binding.slider.multiSelect = false
        // set the selection to the item with the identifier 5
        if (savedInstanceState == null) {
            binding.slider.setSelection(1, false)
        }
    }
integral0909 commented 3 years ago

Hi, @mikepenz

Still looking forward to your help. Thanks.

mikepenz commented 3 years ago

@sweetdream0729 could you please make a sample app which can be compiled to test this out.

binding.slider.multiSelect = false

should not be required to be defined btw.

Also which version do you use?

mikepenz commented 3 years ago

@wakaztahir theoretically you should be able to enable multi select via

binding.slider.multiSelect = true

Also please do not spam other threads with questions.

mikepenz commented 3 years ago

Closing for inactivity. please re-open if still relevant