OnItemCLickListener not working in listview

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

Activity class code:

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


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

    I've tried all the above and NOTHING worked.

    I solved the problem as follows:

    First I define a custom Button called ListButton

    public class ListButton extends android.widget.Button
    {
    
    private ButtonClickedListener clickListener;
    
    public ListButton(Context context)
    {
        this(context, null);
    }
    
    public ListButton(Context context, AttributeSet attrs)
    {
        this(context, attrs, 0);
    }
    
    public ListButton(Context context, AttributeSet attrs, int defStyle)
    {
        super(context, attrs, defStyle);
    }
    
    public void setClickListener(ButtonClickedListener listener) {
        this.clickListener = listener;
    }
    
    @Override
    public boolean isInTouchMode() {
        return true;
    }
    
    @Override
    public boolean onTouchEvent(MotionEvent event) {
    
        return false;
    }
    
    @Override
    public boolean dispatchTouchEvent(MotionEvent event) {
    
        switch (event.getAction()) 
          {
              case MotionEvent.ACTION_DOWN:
                  break;
              case MotionEvent.ACTION_UP:
    
                  eventClicked();
    
                  break;
              case MotionEvent.ACTION_CANCEL:
                  break;
              case MotionEvent.ACTION_MOVE:
                  break;
              default :
    
          }
        return true;
    }
    
    private void eventClicked() {
        if (this.clickListener!=null) {
            this.clickListener.ButtonClicked();
        }
    }
    
    }
    

    The XML looks like:

    <dk.example.views.ListButton
    android:id="@+id/cancel_button"
    android:layout_width="125dp"
    android:layout_height="80dp"
    android:text="Cancel"
    android:textSize="20sp" 
    android:layout_margin="10dp"
    android:padding="2dp"
    android:background="#000000"
    android:textColor="#ffffff"
    android:textStyle="bold"  
    />
    

    Then I define my own ButtonClicked Listener interface:

    public interface ButtonClickedListener {
        public void ButtonClicked();
    }
    

    Then I use my own listener just as if it was the normal OnClickListener:

    final ListButton cancelButton = (ListButton) viewLayout.findViewById(R.id.cancel_button);
    
        cancelButton.setClickListener(new ButtonClickedListener() {
    
            @Override
            public void ButtonClicked() {
                //Do your own stuff here...
            }
    
        });
    
    0 讨论(0)
  • 2020-11-22 11:49
    private AdapterView.OnItemClickListener onItemClickListener;
    
    @Override
    public View getView(final int position, View convertView, final ViewGroup parent) {
     .......
    
    final View view = convertView;
    convertView.setOnClickListener(new View.OnClickListener() {
        @Override
         public void onClick(View v) {
             if (onItemClickListener != null) {
                 onItemClickListener.onItemClick(null, view, position, -1);
             }
          }
     });
    
    return convertView;
    }
    
    public void setOnItemClickListener(AdapterView.OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }
    

    Then in your activity, use adapter.setOnItemClickListener() before attaching it to the listview.

    Copied from github its worked for me

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

    Even I was having the same problem, I am having checkbox, did the following to masker itemClickListener work,

    Added the following properties to the checkbox,

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

    and ItemClickListner started working.

    For detailed example you can go through the link,

    http://knowledge-cess.com/android-itemclicklistner-with-checkbox-or-radiobutton/

    Hope it helps Cheers!!

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

    I had the same problem and tried all of the mentioned solutions to no avail. through testing i found that making the text selectable was preventing the listener to be called. So by switching it to false, or removing it my listener was called again.

    android:textIsSelectable="false"
    

    hope this helps someone who was stuck like me.

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

    I had the same problem and I just saw I had accidentally set:

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

    on my CustomListViewAdapter class.

    By changing this to:

    return true;
    

    I've managed to fix the problem. Just in case if someone has done the same mistake...

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

    in my case none of xml layout properties was not helpful.

    I just add a single line of code like this: convertView.setClickable(false);

    @NonNull
    @Override
    public View getView(final int position, View convertView, @NonNull ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null || convertView.getTag() == null) {
            LayoutInflater inflater = LayoutInflater.from(context);
            convertView = inflater.inflate(R.layout.my_layout_id, parent, false);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        ...
        convertView.setClickable(false);
        return convertView;
    }
    

    so basically it do the same thing as setting up properties in xml layout but it was only thing which works in my case.

    It is not perfect timing but maybe it will helps somebody Happy coding

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