Android工具栏后退箭头不起作用

wpx232ag  于 2022-11-20  发布在  Android
关注(0)|答案(2)|浏览(196)

我不知道为什么工具栏上的后退箭头没有任何作用,有人能帮助我吗?
我在这里保留了活动的onCreate、适配器和xml Im,使用带有约束布局的recyclerView
下面是Activity“onCreate”类的代码:

@Override
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)

    setContentView(com.owncloud.android.R.layout.accounts_layout)
    tintedCheck = ContextCompat.getDrawable(this, com.owncloud.android.R.drawable.ic_current_white)!!
    tintedCheck = DrawableCompat.wrap(tintedCheck)
    val tint = ContextCompat.getColor(this, com.owncloud.android.R.color.actionbar_start_color)
    DrawableCompat.setTint(tintedCheck, tint)

    val recyclerView: RecyclerView = findViewById(com.owncloud.android.R.id.account_list_recycler_view)
    recyclerView.run {
        filterTouchesWhenObscured = PreferenceUtils.shouldDisallowTouchesWithOtherVisibleWindows(applicationContext)
        adapter = accountListAdapter
        layoutManager = LinearLayoutManager(this@AccountManagementActivity)
    }

    setupStandardToolbar(
        getString(com.owncloud.android.R.string.prefs_manage_accounts),
        displayHomeAsUpEnabled = true,
        homeButtonEnabled = true,
        displayShowTitleEnabled = true
    )

    val accountList = AccountManager.get(this).getAccountsByType(accountType)
    originalAccounts = toAccountNameSet(accountList)
    originalCurrentAccount = AccountUtils.getCurrentOwnCloudAccount(this).name

    accountListAdapter.submitAccountList(accountList = getAccountListItems())

    account = AccountUtils.getCurrentOwnCloudAccount(this)
    onAccountSet(false)

    /**
    // added click listener to switch account
    recyclerView.onItemClickListener = OnItemClickListener { parent, view, position, id ->
    switchAccount(
    position
    )
    }
     */
}

适配器中实现recyclerView的代码:

class AccountManagementAdapter(private val accountListener: AccountManagementActivity) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {

    private var accountItemsList = listOf<AccountRecyclerItem>()

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        val inflater = LayoutInflater.from(parent.context)

        return if (viewType == AccountManagementRecyclerItemViewType.ITEM_VIEW_ACCOUNT.ordinal) {
            val view = inflater.inflate(R.layout.account_item, parent, false)
            view.filterTouchesWhenObscured = PreferenceUtils.shouldDisallowTouchesWithOtherVisibleWindows(parent.context)
            AccountManagementViewHolder(view)
        } else {
            val view = inflater.inflate(R.layout.account_action, parent, false)
            view.filterTouchesWhenObscured = PreferenceUtils.shouldDisallowTouchesWithOtherVisibleWindows(parent.context)
            NewAccountViewHolder(view)
        }
    }

    fun submitAccountList(accountList: List<AccountRecyclerItem>) {
        accountItemsList = accountList
        notifyDataSetChanged()
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        when (holder) {
            is AccountManagementViewHolder -> {
                val accountItem = getItem(position) as AccountRecyclerItem.AccountItem
                val account: Account = accountItem.account

                try {
                    val oca = OwnCloudAccount(account, holder.itemView.context)
                    holder.binding.name.text = oca.displayName
                } catch (e: Exception) {
                    Timber.w(
                        "Account not found right after being read :\\ ; using account name instead of display " +
                                "name"
                    )
                    holder.binding.name.text = AccountUtils.getUsernameOfAccount(account.name)
                }
                holder.binding.name.tag = account.name

                holder.binding.account.text = DisplayUtils.convertIdn(account.name, false)

                try {
                    val avatarUtils = AvatarUtils()
                    avatarUtils.loadAvatarForAccount(
                        holder.binding.icon,
                        account,
                        true,
                        20f
                    )
                } catch (e: java.lang.Exception) {
                    Timber.e(e, "Error calculating RGB value for account list item.")
                    // use user icon as a fallback
                    holder.binding.icon.setImageResource(R.drawable.ic_user)
                }

                if (AccountUtils.getCurrentOwnCloudAccount(holder.itemView.context).name == account.name) {
                    holder.binding.ticker.visibility = View.VISIBLE
                } else {
                    holder.binding.ticker.visibility = View.INVISIBLE
                }

                /// bind listener to refresh account
                holder.binding.refreshAccountButton.apply {
                    setImageResource(R.drawable.ic_action_refresh)
                    setOnClickListener { accountListener.refreshAccount(account) }
                }

                /// bind listener to change password
                holder.binding.passwordButton.apply {
                    setImageResource(R.drawable.ic_baseline_lock_reset_grey)
                    setOnClickListener { accountListener.changePasswordOfAccount(account) }
                }

                /// bind listener to remove account
                holder.binding.removeButton.apply {
                    setImageResource(R.drawable.ic_action_delete_grey)
                    setOnClickListener { accountListener.removeAccount(account) }
                }

                ///bind listener to switchAccount
                holder.binding.account.apply {
                    setOnClickListener { accountListener.switchAccount(position) }
                }
            }
            is NewAccountViewHolder -> {
                holder.binding.icon.setImageResource(R.drawable.ic_account_plus)
                holder.binding.name.setText(R.string.prefs_add_account)

                // bind action listener
                holder.binding.linearLayout.setOnClickListener {
                    accountListener.createAccount()
                }
            }
        }

    }

    override fun getItemCount(): Int = accountItemsList.size

    fun getItem(position: Int) = accountItemsList[position]

    sealed class AccountRecyclerItem {
        data class AccountItem(val account: Account) : AccountRecyclerItem()
        object NewAccount : AccountRecyclerItem()
    }

    class AccountManagementViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val binding = AccountItemBinding.bind(itemView)
    }

    class NewAccountViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val binding = AccountActionBinding.bind(itemView)
    }

    override fun getItemViewType(position: Int): Int {
        return when (getItem(position)) {
            is AccountRecyclerItem.AccountItem -> AccountManagementRecyclerItemViewType.ITEM_VIEW_ACCOUNT.ordinal
            is AccountRecyclerItem.NewAccount -> AccountManagementRecyclerItemViewType.ITEM_VIEW_ADD.ordinal
        }
    }

    enum class AccountManagementRecyclerItemViewType {
        ITEM_VIEW_ACCOUNT, ITEM_VIEW_ADD
    }

    /**
     * Listener interface for Activities using the [AccountListAdapter]
     */
    interface AccountAdapterListener {
        fun removeAccount(account: Account)
        fun changePasswordOfAccount(account: Account)
        fun refreshAccount(account: Account)
        fun createAccount()
        fun switchAccount(position: Int)
    }
}

最后,我给您留下两个xml文件,一个是声明所有项的文件,另一个是实现recyclerView的文件:
第一次
如果需要更多的代码,我可以添加它:)
我试着加上这样的话:

if(item.getItemId() ==android.R.id.home){
  onBackPressed();
}

像这样:

binding.toolbar.setNavigationOnClickListener { navController.popBackStack() }

但没有一个成功

kx5bkwkv

kx5bkwkv1#

在定义navController的位置添加以下代码:

override fun onSupportNavigateUp(): Boolean {
        return NavigationUI.navigateUp(yourNavController, null) || super.onSupportNavigateUp()
    }
dojqjjoe

dojqjjoe2#

我只是在活动类中添加了以下函数:

override fun onOptionsItemSelected(item: MenuItem): Boolean {
    var retval = true
    when (item.itemId) {
        R.id.home -> onBackPressed()
        else -> retval = super.onOptionsItemSelected(item)
    }
    return retval
}

相关问题