工具栏菜单未膨胀

时间:2018-06-27 11:35:56

标签: android menu kotlin toolbar

通过在recyclerView的长按上修改工具栏(包含编辑和删除),我正在制作一个简化动作栏。如果我选择/取消选择视图,则可以正常工作,但是,如果单击“编辑”,则只有在我专注于其中一个编辑文本时,菜单才会出现。当我编辑任何条目时,它将再次起作用。工具栏textView正在更新,但菜单没有膨胀

//活动费用列表

class Expenses_list : AppCompatActivity(), View.OnLongClickListener{


lateinit var expenses_recycler : RecyclerView
var expense_list_items :MutableList<expense_input> = ArrayList()
var selection_list_items :MutableList<expense_input> = ArrayList()
var db = DataBaseHandler(this)
var categ:String = ""
var is_in_action_mode = false
var edit_mode = false
private var counter = 0
lateinit var toolbar: android.support.v7.widget.Toolbar

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_expenses_list)

    toolbar = findViewById(R.id.toolbar)
    setSupportActionBar(toolbar)

    supportActionBar?.setDisplayHomeAsUpEnabled(true)

    categ= intent.getStringExtra("cat")
    textView.text = categ.capitalize()

    loadData()

    expenses_recycler = findViewById(R.id.expense_list_item)
    expenses_recycler.layoutManager = LinearLayoutManager(this)
    expenses_recycler.adapter = 
    Expense_Adapter(expense_list_items,this,this@Expenses_list)

    }

fun addObject(i:Int) {
    selection_list_items.add(expense_list_items[i])
    counter=counter+1
    changeMenu()
    updateCounter(counter)
}

private fun updateCounter(counter:Int) {
    if(counter==0)
    {
        textToolbar.text = "0 item selected"
    }
    else
    {
        textToolbar.text = "" + counter + "item selected"
    }
}

private fun removeObject(adapterPosition: Int) {
    selection_list_items.remove(expense_list_items[adapterPosition])
    counter=counter-1
    changeMenu()
    updateCounter(counter)
}

fun loadData() {
    expense_list_items.clear()

    var data = db.readDataExpense(categ)

    for (i in 0..(data.size - 1))
    {
        expense_list_items.add(data[i])
    }
}

override fun onLongClick(p0: View?): Boolean {
    toolbar.menu.clear()
    textToolbar.visibility = View.VISIBLE
    is_in_action_mode = true
    expenses_recycler.adapter.notifyDataSetChanged()
    return true
}

fun changeMenu() {
    when (counter) {
        0 -> {
            Toast.makeText(this,"counter=0",Toast.LENGTH_SHORT).show()
            toolbar.menu.clear()
        }
        1 -> {
            Toast.makeText(this,"counter=1",Toast.LENGTH_SHORT).show()
            toolbar.menu.clear()
            toolbar.inflateMenu(R.menu.menu_toolbar_edit)
        }
        else -> {
            toolbar.menu.clear()
            toolbar.inflateMenu(R.menu.menu_toolbar)
        }
    }
}

override fun onOptionsItemSelected(item: MenuItem?): Boolean {
    when(item?.itemId)
    {
        R.id.edit->{
            edit_mode=true
            expenses_recycler.adapter.notifyDataSetChanged()
        }

        R.id.delete->{
            db.deleteExpense(selection_list_items)
            loadData()
           clearActionMode()
        }
    }
    return super.onOptionsItemSelected(item)
}

private fun clearActionMode() {
    is_in_action_mode=false
    edit_mode=false
    selection_list_items.clear()
    toolbar.menu.clear()
    textToolbar.visibility = View.GONE
    textToolbar.text = "0 item selected"
    expenses_recycler.adapter.notifyDataSetChanged()
}

override fun onBackPressed() {
    if(is_in_action_mode==true)
    {
        clearActionMode()
    }
    else
        super.onBackPressed()
}


    }

//适配器

     class Expense_Adapter(items 
:List<expense_input>,ctx:Context,el:Expenses_list) : 
RecyclerView.Adapter<Expense_Adapter.ViewHolder>() {

    var expl = el


    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        return ViewHolder(context,expl,LayoutInflater.from(context).inflate(R.layout.expense_list,parent,false))
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {

        holder.bindTo(list[position])
    }

    var list = items
    var context = ctx

    override fun getItemCount(): Int {
        return list.size
    }


    class ViewHolder(v:View) :RecyclerView.ViewHolder(v)
    {
        var mex :Expenses_list? = null
        var context : Context?= null
        var exp :expense_input ?= null

        constructor(ctx: Context,ex: Expenses_list,v: View):this(v){
            mex = ex
            context = ctx
            bl.setOnLongClickListener(mex)
            cb?.setOnCheckedChangeListener { buttonView, isChecked ->
                if (isChecked) {
                    mex!!.addObject(adapterPosition)
                }
                else {
                    mex!!.removeObject(adapterPosition)
                }
            }
}

        fun bindTo(l: expense_input)
        {
            exp = l
            name?.setText(l.item)
            //name?.text = l.item
            am.setText(l.amount.toString())
            //am.text = l.amount.toString()
            d.text = l.date


            if(mex?.is_in_action_mode==false) {
                cb.visibility = View.GONE
            }
            else {
                cb.visibility = View.VISIBLE
                cb.isChecked = false
            }


            if(mex?.edit_mode==false)
            {
                if(vs.displayedChild == 1)
                {
                    Log.d("action","here")
                    im.setImageResource(R.drawable.ic_expnote)
                    vs.showPrevious()
                }
            }
            if(mex!!.edit_mode)
            {
                Log.d("action","Edit")
                vs.showNext()
                cb.visibility = View.GONE
                ename.setText(exp?.item)
                eam.setText(exp?.amount.toString())
                im.setImageResource(R.drawable.ic_list)
            }

            im.setOnClickListener {
                val db = DataBaseHandler(context!!)
                if(vs.displayedChild==1)
                {
                    if(eam.text.toString().isNotEmpty() && ename.text.toString().isNotEmpty())
                    {
                        var ex = l
                        ex.amount = eam.text.toString().toFloat()
                        ex.item = ename.text.toString()
                        db.UpdateDataExpense(ex)
                        mex?.clearActionMode()
                    }
                    else
                    {
                        Toast.makeText(context,"Item is invalid",Toast.LENGTH_SHORT).show()
                    }
                }
                else
                {
                    val detailIntent = Intent(context, Exp_note::class.java)
                    detailIntent.putExtra("Note",l.id)
                    context!!.startActivity(detailIntent)
                }
            }
        }

        val name = v.textItem
        val am = v.textAmount
        val d = v.textDate
        val im = v.openNote
        val cb = v.cb
        val bl = v.biglinear
        val vs = v.viewSwitcher
        val ename = v.editItem
        val eam = v.EditAmount

    }
}

1 个答案:

答案 0 :(得分:0)

您没有尝试在一个活动中添加不同的菜单,而是尝试创建一个包含所有项目的菜单并将它们默认设置为不可见?然后,您可以覆盖onPrepareOptionsMenu(例如,如下所示),以根据定义的条件更改要显示的项目:

override fun onPrepareOptionsMenu(menu: Menu): Boolean {
    invalidateOptionsMenu()
    when {
        // more than one item selected
        mSelectedItems.isNotEmpty() && mSelectedItems.size > 1 -> {
            menu.findItem(R.id.action_edit).isVisible = false
            menu.findItem(R.id.action_delete).isVisible = true
        }
        // one item selected
        mSelectedItems.isNotEmpty() -> {
            menu.findItem(R.id.action_edit).isVisible = true
            menu.findItem(R.id.action_delete).isVisible = true
        }
        // none selected
        else -> {
            menu.findItem(R.id.action_edit).isVisible = false
            menu.findItem(R.id.action_delete).isVisible = false
        }
    }
    return super.onPrepareOptionsMenu(menu)
}

obove code produces following result