Я делаю контекстную панель действий, изменяя панель инструментов при длинном щелчке в recyclerView, которое содержит редактирование и удаление. Это работает нормально, если я выбираю / отменяю выбор видов, однако, если я нажимаю на редактировать, меню не появляется, пока я не сфокусируюсь на одном из текстов редактирования. Когда я редактирую любую запись, она снова работает. обновляется текстовая панель инструментов, но меню не раздувается
// Activity Expense_list
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()
}
}
// Adapter
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
}
}