Skip to content Skip to sidebar Skip to footer

Setonitemclicklistener Event Doesn't Work In Fragment

I am trying to get onItemClick on ListItems to work from a fragment. Here is my code: public class MyBudgetPageMenuFragment extends Fragment { private Context context; private Lis

Solution 1:

Note that the ListView blocks clicks of an item that contains at least one focusable descendant but it doesn’t make the content focus-reachable calling setItemsCanFocus(true). One workaround is by disabling focusability of descendants, using

android:descendantFocusability="blocksDescendants"

in the layout defining your list item. (First learned of this myself from http://cyrilmottier.com/2011/11/23/listview-tips-tricks-4-add-several-clickable-areas/) Does your layout contain buttons? If so, you'd fall into this case.

Solution 2:

Just go the simple way.

In your Adapter class, between getView method, initialize View v = convertView; then

v.setOnClickListener(newView.OnClickListener() {
    @OverridepublicvoidonClick(View v) {
        Toast.makeText(context, "test "+ list.get(position).getS_name(), Toast.LENGTH_SHORT).show();
    }
});

Post a Comment for "Setonitemclicklistener Event Doesn't Work In Fragment"