代码之家  ›  专栏  ›  技术社区  ›  Javallion

gridview.setmultichoiceModelistener不在Android应用程序中工作

  •  0
  • Javallion  · 技术社区  · 6 年前

    我的代码如下:

    class MySitesActivity : AppCompatActivity() {
    
        val REQUEST_CODE = 3
        private val TAG = "MySitesActivity"
        lateinit var gridView: GridView
        lateinit var siteAdapter:BaseAdapter
        lateinit var sites:ArrayList<Site>
        lateinit var actionBarObject:ActionBar
    
    
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(R.layout.activity_my_sites)
            setSupportActionBar(findViewById(R.id.my_toolbar))
    
            sites = ArrayList(db.appDao().getAllSites()) //From Database
            gridView = findViewById<View>(R.id.gridview) as GridView
            siteAdapter = SitesAdapter(this@MySitesActivity, sites)
            gridView.adapter = siteAdapter
            gridView.choiceMode = GridView.CHOICE_MODE_MULTIPLE // CAN DO IN XML
            actionBarObject = supportActionBar!!
            actionBarObject.setDisplayHomeAsUpEnabled(true)
    
    
            Log.d(TAG, "* Setting MultiChoiceModeListener *")
            gridView.setMultiChoiceModeListener(object : AbsListView.MultiChoiceModeListener {
    
                override fun onItemCheckedStateChanged(mode: ActionMode, position: Int,
                                              id: Long, checked: Boolean) {
                    // Here you can do something when items are selected/de-selected,
                    // such as update the title in the CAB
                }
    
                override fun onActionItemClicked(mode: ActionMode, item: MenuItem): Boolean {
                    // Respond to clicks on the actions in the CAB
                    return false
                }
    
                override fun onCreateActionMode(mode: ActionMode, menu: Menu): Boolean {
                    // Inflate the menu for the CAB
                    Log.d(TAG,"Inflating menu")
                    mode.menuInflater.inflate(R.menu.delete_menu, menu)
                    return true
                }
    
                override fun onDestroyActionMode(mode: ActionMode) {
                    // Here you can make any necessary updates to the activity when
                    // the CAB is removed. By default, selected items are deselected/unchecked.
                }
    
                override fun onPrepareActionMode(mode: ActionMode, menu: Menu): Boolean {
                    // Here you can perform updates to the CAB due to
                    // an invalidate() request
                    return false
                }
            })
    }
    
    
    }
    

    为了简洁起见,我删除了databse函数。OnCreate操作模式永远不会被调用,日志语句也不会吐出数据。

    我的SiteAdapter类扩展了BaseAdapter,返回的视图很好,显示很好。我不会在SiteAdapter类中添加任何侦听器或类似的内容。只是长时间的点击不会发生任何事情。有什么我可能做错什么的想法吗?谢谢

    1 回复  |  直到 6 年前
        1
  •  0
  •   Javallion    6 年前

    好吧,所以我选错了模式。

    gridview.choice_mode_multiple而不是gridview.choice_mode_multiple_modal

    这个变化解决了最初的问题,并创建了一个新的问题,我认为我应该将它发布在一个单独的线程上。