7

Currently, I have a ListView row. Clicking any area on the row, will yield ListView click events.

    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.HONEYCOMB) {
        this.getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
        this.getListView().setMultiChoiceModeListener(new ModeCallback());
        this.getListView().setOnItemClickListener(new ListViewOnItemClickListener());
    } else {
        // http://stackoverflow.com/questions/9754170/listview-selection-remains-persistent-after-exiting-choice-mode
        this.getListView().setOnItemLongClickListener(new ListViewOnItemLongClickListener());
        this.getListView().setOnItemClickListener(new ListViewOnItemClickListener());
    }

Now, for each row, I would like to add a small button. Clicking on the small button will yield button click event, which is different from ListView original click event.

enter image description here

In my ArrayAdapter, I use to have

public View getView(int position, View convertView, ViewGroup parent) {
    View rowView = convertView;

    if (rowView == null) {
        LayoutInflater inflater = activity.getLayoutInflater();
        rowView = inflater.inflate(R.layout.watchlist_row_layout, null);
        ...
    }

    Button button = (Button)rowView.findViewById(R.id.button1);
    button.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            Utils.showLongToast("BUTTON CLICK");
        }
    });

enter image description here

Clicking on the button will yield button click event. However, clicking on area other than button, will not yield any ListView click event anymore.

May I know why is it so? Is there any way to resolve this?

2 Answers 2

15

If you want to make both Rows and Buttons clickable, set

android:focusable="false"

for your Button. The same goes for ImageButtons, RadioButtons, ...

Android has been primarily designed for a large set of input methods. The entire system is completely capable of working with no touch screen. To navigate through the UI, the user can use a directional pad which focuses Views after Views if and only if those Views are focusable. By default, all Android controls are focusable. In order to prevent having controls that are not focus-reachable, the ListView will simply prevent the selection (and click) of an itemview. By design, the ListView blocks clicks of itemview containing at least one focusable descendant but it doesn’t make the content focus-reachable calling setItemsCanFocus(true).

Here is the whole great explaination: Having several clickable areas in a ListView

0
0

It's worth noting that the preferred way (even according to the link provided in the accepted answer) is to set the android:descendantFocusability attribute to block focus from the root layout you're inflating within the adapter. There can be potential situations where simply setting the focusable attribute to false on an individual element won't solve the problem, but the descendantFocusability attribute will.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.