【问题标题】:What is wrong with my OnNavigationItemSelectedListener kotlin android?我的 OnNavigationItemSelectedListener kotlin android 有什么问题?
【发布时间】:2019-05-11 17:46:11
【问题描述】:

我正在尝试在我的应用程序中使用 bottomNavigation。现在我想将其中一个类从 java 移动到 kotlin,在这个类中我有用于导航的底部导航菜单。在 Java 我有这样的听众:

bottomNavigationView.setOnNavigationItemSelectedListener(menuItem -> {
            FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
            Bundle bundle = new Bundle();
            switch (menuItem.getItemId()) {

                case R.id.full_jobAgent:

                    Objects.requireNonNull(getSupportActionBar()).show();
                    JobList jobList = new JobList();
                    bundle.putInt("offset", 1);
                    jobList.setArguments(bundle);
                    textViewTitle.setText("Jobagent");
                    tvSubTitle.setText(R.string.all_jobs);
                    textViewTitle.setTypeface(custom_font);
                    tvSubTitle.setTypeface(custom_font);
                    disableShowHideAnimation(getSupportActionBar());
                    transaction.replace(R.id.contentContainer, jobList).addToBackStack(null).commit();
                    return true;
                case R.id.received_mess:
                    disableShowHideAnimation(getSupportActionBar());
                    Objects.requireNonNull(getSupportActionBar()).show();


                    MessageList messageList = new MessageList();
                    bundle.putInt("type1", 0);
                    messageList.setArguments(bundle);
                    transaction.replace(R.id.contentContainer, messageList).addToBackStack(null).commit();
                    textViewTitle.setTypeface(custom_font);
                    tvSubTitle.setTypeface(custom_font);
                    textViewTitle.setText(R.string.title_activity_message_center);
                    tvSubTitle.setText(R.string.received);
                    return true;
                case R.id.home_screen:
                    disableShowHideAnimation(getSupportActionBar());
                    Objects.requireNonNull(getSupportActionBar()).hide();
                    transaction.replace(R.id.contentContainer, new PersonalData()).addToBackStack(null).commit();
                    return true;
                case R.id.more:
                    disableShowHideAnimation(getSupportActionBar());
                    textViewTitle.setText(R.string.more_bottom_nav);
                    textViewTitle.setTypeface(custom_font);
                    tvSubTitle.setVisibility(View.GONE);
                    Objects.requireNonNull(getSupportActionBar()).show();
                    transaction.replace(R.id.contentContainer, new MoreScreen()).addToBackStack(null).commit();
                    return true;
                case R.id.notespec:
                    disableShowHideAnimation(getSupportActionBar());
                    textViewTitle.setText(R.string.notepad_bottom_nav);
                    textViewTitle.setTypeface(custom_font);
                    tvSubTitle.setVisibility(View.GONE);
                    Objects.requireNonNull(getSupportActionBar()).show();
                    transaction.replace(R.id.contentContainer, new TestNotepadFragment()).addToBackStack(null).commit();
                    return true;
            }
            return false;
        });

我已经把这个方法改写成kotlin了:

 private val mOnNavigationItemSelectedListener = BottomNavigationView.OnNavigationItemSelectedListener { item ->
        val transaction = supportFragmentManager.beginTransaction()
        val bundle = Bundle()
        when (item.itemId) {
            R.id.full_jobAgent -> {
                ms.filter_data = HashMap<String, String>()
                disableShowHideAnimation(supportActionBar!!)

                val jobList = JobList()
                bundle.putInt("offset", 1)

                if (intent.getSerializableExtra("filter_data") != null) {
                    bundle.putSerializable("filter_data", intent.getSerializableExtra("filter_data"))
                }
                bottomNavigationView.selectedItemId = R.id.full_jobAgent
                transaction.replace(R.id.contentContainerT, jobList).addToBackStack(null).commit()
                textSetter("Jobagent", resources.getString(R.string.all_jobs))
                //return@OnNavigationItemSelectedListener true
            }

            R.id.received_mess -> {
                disableShowHideAnimation(supportActionBar!!)
                supportActionBar!!.show()
                val messageList = MessageList()
                bundle.putInt("type1", 0)
                messageList.arguments = bundle
                textSetter(resources.getString(R.string.title_activity_message_center), resources.getString(R.string.received))
                transaction.replace(R.id.contentContainerT, messageList).addToBackStack(null).commit()
                //return@OnNavigationItemSelectedListener true
            }

            R.id.home_screen -> {
                disableShowHideAnimation(supportActionBar!!)
                supportActionBar!!.hide()
                transaction.replace(R.id.contentContainerT, PersonalData()).addToBackStack(null).commit()
                //return@OnNavigationItemSelectedListener true
            }

            R.id.notespec -> {
                disableShowHideAnimation(supportActionBar!!)
                supportActionBar!!.show()
                textSetter(resources.getString(R.string.more_bottom_nav), "")
                transaction.replace(R.id.contentContainerT, NotepadScr()).addToBackStack(null).commit()
                //return@OnNavigationItemSelectedListener true
            }

            R.id.more ->{
                disableShowHideAnimation(supportActionBar!!)
                supportActionBar!!.show()
                textSetter(resources.getString(R.string.more_bottom_nav),"")
                transaction.replace(R.id.contentContainerT,MoreScreen()).addToBackStack(null).commit()
                //return@OnNavigationItemSelectedListener true
            }
        }
        false
    }

我的应用开始崩溃。我发现问题出在这里:

return@OnNavigationItemSelectedListener true

你们中的一些人会问我应用程序的日志,但我无法从 logcat 中获取它们,因为它重新加载非常快。所以......在java我们不必选择选定的项目,但是在kotlin,当我删除错误的选择行时,我看不到现在选择了哪个项目。也许我做错了什么?

【问题讨论】:

  • 在最后尝试使用return true
  • hm...我看到smb使用这个返回,有些使用return true这些语句之间的区别在哪里?
  • 您在使用return@OnNavigationItemSelectedListener true时需要添加else。关于 true 和 return true 我不知道它是如何工作的。
  • else when 声明?所以我必须为此添加一些条件?
  • 看起来像。尝试在 else 中放置一个简单的日志语句

标签: java android kotlin bottomnavigationview


【解决方案1】:
navigation.setOnNavigationItemSelectedListener(this)

override fun onNavigationItemSelected(item: MenuItem): Boolean {
    when (item.itemId) {
        R.id.navigation_catalog -> {
            swapFragments(item.itemId, "Catalog")
            return true
        }
        R.id.navigation_shopping_cart -> {
            swapFragments(item.itemId, "ShoppingCart")
            return true
        }
        R.id.navigation_account -> {
            swapFragments(item.itemId, "Account")
            return true
        }
    }
    return false
}


private fun swapFragments(@IdRes actionId: Int, key: String) {
    if (supportFragmentManager.findFragmentByTag(key) == null) {
        savedFragmentState(actionId)
        createFragment(key, actionId)
    }
}

private fun savedFragmentState(actionId: Int) {
    val currentFragment = supportFragmentManager.findFragmentById(R.id.fragment_home)
    if (currentFragment != null) {
        savedStateSparseArray.put(currentSelectItemId,
                supportFragmentManager.saveFragmentInstanceState(currentFragment)
        )
    }
    currentSelectItemId = actionId
}

private fun createFragment(key: String, actionId: Int) {
    when (actionId) {
        R.id.navigation_catalog -> {
            val fragment = CatalogFragment.newInstance(key)
            fragment.setInitialSavedState(savedStateSparseArray[actionId])
            supportFragmentManager.beginTransaction()
                    .replace(R.id.fragment_home, fragment, key)
                    .commit()
        }
        R.id.navigation_shopping_cart -> {
            val fragment = ShoppingcartFragment.newInstance(key)
            fragment.setInitialSavedState(savedStateSparseArray[actionId])
            supportFragmentManager.beginTransaction()
                    .replace(R.id.fragment_home, fragment, key)
                    .commit()
        }
        R.id.navigation_account -> {
            val fragment = AccountFragment.newInstance(key)
            fragment.setInitialSavedState(savedStateSparseArray[actionId])
            supportFragmentManager.beginTransaction()
                    .replace(R.id.fragment_home, fragment, key)
                    .commit()
        }
    }

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-11-13
    • 1970-01-01
    • 1970-01-01
    • 2021-11-27
    • 2019-06-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多