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.
This is common question for ListView
. I read source code about this.
Question: why onListItemClick
not be called?
Answer:
AbsListView
class override onTouchEvent method.
if (inList && !child.hasFocusable()) {
if (mPerformClick == null) {
mPerformClick = new PerformClick();
}
.....
}
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).
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
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
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;
}