ListView items are not clickable. why?

后端 未结 11 2431
借酒劲吻你
借酒劲吻你 2020-11-29 08:12

I have a ListView that uses a customized adapter, but I can\'t click on the ListView Item ..

Activity for list view ..

package com.adham         


        
相关标签:
11条回答
  • 2020-11-29 08:42

    I was using a view and button inside the list view so I have used:

    android:focusable="false"
    

    for <button> and <view> ...

    After that I used following code for my list view and it worked

    // Attach the adapter to a ListView
    ListView listView = (ListView) findViewById(R.id.shipping_item_list);
    listView.setAdapter(adapter);
    listView.setOnItemClickListener(listPairedClickItem);
    
    private AdapterView.OnItemClickListener listPairedClickItem = new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
    
        Toast.makeText(getBaseContext(), "Item ", Toast.LENGTH_LONG).show();
        }
    };
    
    0 讨论(0)
  • 2020-11-29 08:43

    I wanted to add a comment to rupps answer, but I do not have enough reputation.

    If you are using a custom adapter extending the ArrayAdapter you can overwrite with areAllItemsEnabled() and isEnabled(int position) in your class:

    @Override
    public boolean areAllItemsEnabled() {
        return true;
    }
    
    @Override
    public boolean isEnabled(int position) {
        return true;
    }
    

    The above fixed my non clickable list view. Maybe this comment also helps others as "android list not clickable" search term is quite high in Google.

    0 讨论(0)
  • 2020-11-29 08:44

    I'm pretty late but discovered something I think it's interesting about this:

    if your adapter descends from ArrayAdapter, as much as I have tried, onItemClickListener is not called.

    However, if your adapter descends from BaseAdapter (so you have to implement getCount() and getItem(), trivial for an array) it IS always called.

    0 讨论(0)
  • 2020-11-29 08:46

    List view items are clickable. To use it, you have to set item click listener on your list view. Then it will work.

    0 讨论(0)
  • 2020-11-29 08:47

    try this to get the focus: View.getFocus();

    0 讨论(0)
  • 2020-11-29 08:47

    For me, the problem was that my items within my ListView had clickable set to true.

    0 讨论(0)
提交回复
热议问题