listview item with button not responding to onListItemClick

前端 未结 4 2040
我在风中等你
我在风中等你 2021-01-26 02:52

I am working on an app where the list item are complex, TextView and two ImageButtons. I have looked at the around for a solution, and tried all that I have seen, still nothing.

相关标签:
4条回答
  • 2021-01-26 03:35

    This is common question for ListView. I read source code about this.
    Question: why onListItemClick not be called?
    Answer:

    • AbsListView class override onTouchEvent method.
    • Snip code come from onTouchEvent method.
       
      if (inList && !child.hasFocusable()) {
                          if (mPerformClick == null) {
                              mPerformClick = new PerformClick();
                          }
      .....
      }
      

      PerformClick will be call if child.hasFocusable() return false which child is you ListView item view;
    • Snip code come from hasFocusable method.
     
    @Override
        public boolean hasFocusable() {
            if ((mViewFlags & VISIBILITY_MASK) != VISIBLE) {return false; }
            if (isFocusable()) {return true;}
            final int descendantFocusability = getDescendantFocusability();
            if (descendantFocusability != FOCUS_BLOCK_DESCENDANTS) {
                final int count = mChildrenCount;
                final View[] children = mChildren;
                for (int i = 0; i 


    So solution:
    Solution A,set ListView item descendantFocusability property, let its getDescendantFocusability() is not equal FOCUS_BLOCK_DESCENDANTS.
    Solution B, ListView item all child views is not hasFocusable( hasFocusable()
    return false).

    0 讨论(0)
  • I think your ImageButton is stealing away the onItemClickLister event. Add this attribute to your layout

    android:descendantFocusability="blocksDescendants"

    <TextView
        android:id="@+id/medcine_info_txt"
        android:layout_width="fill_parent"
        android:layout_height="200dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:clickable="false"
        android:focusable="false"
        android:focusableInTouchMode="false"
        android:padding="3dp"
        android:textColor="@color/black" />
    
    <ImageButton
        android:id="@+id/item_edit"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        .......
    

    Thanks

    0 讨论(0)
  • 2021-01-26 03:44

    Make the ListFragment implement the View.OnClickListener interface, and implement the code you want to be called when the button are pressed in the method OnClick(View view), which is @Override

    0 讨论(0)
  • 2021-01-26 03:45

    you can create View.OnClickListner object which can listen your imagebutton click in getView. onListItemClick generally used to handle row click event not items in rows.

    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflator = (LayoutInflater) getContext()
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View listItem = inflator.inflate(R.layout.medcince_list_item, null);
    
        ImageButton mEdit = (ImageButton)listItem.findViewById(R.id.item_edit);
        mEdit.setOnClickListener(new View.OnClickListener(){
    
        @Override
        public void onClick(View v) {
            // HERE YOU CAN HANDLE BUTTON CLICK. POSITION YOU CAN HAVE FROM getView already.
        }
    
        });
        mEdit.setTag(getItem(position));
    
        ImageButton mHistory = (ImageButton)listItem.findViewById(R.id.item_history);
        mHistory.setOnClickListener(new View.OnClickListener(){
    
        @Override
        public void onClick(View v) {
            // HERE YOU CAN HANDLE BUTTON CLICK. POSITION YOU CAN HAVE FROM getView already.
        }
    
        });
        mHistory.setTag(getItem(position));
    
        return listItem;
    }
    
    0 讨论(0)
提交回复
热议问题