Mustafa ALMulla
Mustafa ALMulla

Reputation: 900

listview item with button not responding to onListItemClick

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?

Upvotes: 0

Views: 2076

Answers (4)

boiledwater
boiledwater

Reputation: 10482

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

Upvotes: 1

Md. Monsur Hossain Tonmoy
Md. Monsur Hossain Tonmoy

Reputation: 11085

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

Upvotes: 2

Bharat Sharma
Bharat Sharma

Reputation: 3966

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

Upvotes: 0

Hitman
Hitman

Reputation: 598

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

Upvotes: 0

Related Questions