OnItemCLickListener not working in listview

前端 未结 24 1930
悲&欢浪女
悲&欢浪女 2020-11-22 11:21

Activity class code:

conversationList = (ListView)findViewById(android.R.id.list);
ConversationArrayAdapter conversationArrayAdapter=new  Conver         


        
相关标签:
24条回答
  • 2020-11-22 11:52

    In my case, I had to remove the next line from the Layout

    android:clickable="true"
    
    0 讨论(0)
  • 2020-11-22 11:54

    If you want to use both the simple click and long click on list view items better way to implement that would be to use context menu for long click. Avoid using setItemLongClickListener especially if you have multiple row layouts for your listview.

    0 讨论(0)
  • 2020-11-22 11:56

    Two awesome solutions were this, if your extending ListFragment from a fragment, know that mListView.setOnItemClickListener wont be called before your activity is created, this ensured it is set when activity has been created

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mListView.setOnItemClickListener(new OnItemClickListener() {
    
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long rowId) {
                // Do the onItemClick action
    
                Log.d("ROWSELECT", "" + rowId);
            }
        });
    }
    

    While looking at the source code for ListFragment, I came across this

    public class ListFragment extends Fragment {
        ...........................................
        ................................................
    
        final private AdapterView.OnItemClickListener mOnClickListener
                    = new AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
                onListItemClick((ListView)parent, v, position, id);
            }
        };
    
        ................................................................
        ................................................................
    
        public void onListItemClick(ListView l, View v, int position, long id) {
        }
    }
    

    An onItemClickListener object is attached and it calls onListItemClick() As such the other similar solution, which works in the exact same way is to override onListItemClick()

    @Override
    public void onListItemClick(ListView l, View v, int position, long rowId) {
        super.onListItemClick(l, v, position, id);
       // Do the onItemClick action
    
       Log.d("ROWSELECT", "" + rowId);
    } 
    
    0 讨论(0)
  • 2020-11-22 11:57

    For my lists, my rows have other things that can be clicked, like buttons, so doing a blanket blocksDescendants doesn't work. Instead I add a line in the button's xml:

        android:focusable="false"
    

    That keeps the buttons from blocking the clicks on the rows, but still lets the buttons take the clicks, too.

    0 讨论(0)
  • 2020-11-22 11:58
    1. Add this in main Layout

      android:descendantFocusability="blocksDescendants"
      
    2. Write this code into every button,Textview,ImageView etc which have onClick

      android:focusable="false"
      
      android:clickable="false"
      

    Hope it will work.

    0 讨论(0)
  • 2020-11-22 11:58

    I solved the problem by removing the clickable views from the list.

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