listview item with button not responding to onListItemClick

家住魔仙堡 提交于 2019-12-02 15:52:38

问题


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.

The list is part of the ListFragment on I have Override onListItemClick.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#FFF" >

    <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"
        android:layout_alignBottom="@+id/medcine_info_txt"
        android:layout_alignParentLeft="true"
        android:contentDescription="@string/item_edit"
        android:focusable="false"
        android:focusableInTouchMode="false"
        android:src="@android:drawable/ic_menu_edit" />

    <ImageButton
        android:id="@+id/item_history"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/medcine_info_txt"
        android:layout_centerHorizontal="true"
        android:contentDescription="@string/item_history"
        android:focusable="false"
        android:focusableInTouchMode="false"
        android:src="@android:drawable/btn_star" />

</RelativeLayout>

This my adapter getView where I have on handle the buttons click, and it implements OnClickListener

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(this);
    mEdit.setTag(getItem(position));

    ImageButton mHistory = (ImageButton)listItem.findViewById(R.id.item_history);
    mHistory.setOnClickListener(this);
    mHistory.setTag(getItem(position));

    return listItem;
}

Any thoughts on why the onListItemClick is not handling the click?


回答1:


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;
}



回答2:


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




回答3:


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).




回答4:


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



来源:https://stackoverflow.com/questions/20187890/listview-item-with-button-not-responding-to-onlistitemclick

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!