gridView.setMultiChoiceModeListener无法在android应用中工作

时间:2018-06-29 20:00:49

标签: android gridview kotlin

我的代码如下:

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动作模式,并且Log语句永远不会吐出数据。

我的SiteAdapter类扩展了Baseadapter,返回的视图很好,并且显示得很好。我没有在siteadapter类中添加任何侦听器或类似的东西。只是长按没有任何反应。有什么想法我可能做错了吗?谢谢

1 个答案:

答案 0 :(得分:0)

好,所以我选择了错误的模式。

GridView.CHOICE_MODE_MULTIPLE而不是GridView.CHOICE_MODE_MULTIPLE_MODAL

此更改解决了INITIAL问题,并创建了一个新的更改,我认为我应该将其发布在单独的线程上。