jrmgx
jrmgx

Reputation: 729

OnItemClickListener with multiple viewType in adapter

I have an Adapter with multiple view type defined like that

public class ListAdapter extends BaseAdapter { 
    ...
    @Override
    public int getViewTypeCount() {
        return 2;
    }

    @Override
    public int getItemViewType(int i) {
        return (bool()) ? 1 : 0);
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        int type = getItemViewType(i);
        if (type == 1) {
            return viewType(R.layout.list_item_ads_alert, i, view, viewGroup);
        }
        else {
            return viewType(R.layout.list_item_ads, i, view, viewGroup);
        }
    }
}

then in my fragment I instantiate it and set it to a listView, plus I add an onItemClick listener to it

ListAdapter adapter = new ListAdapter();
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override 
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        Log.d("app", "pushed item " + i);
    }
});

The problem is that my ItemClickListener is NOT called when I click on an item of the second view type, but it works for the first view type

Do you know if there is a specific way to do ItemClick for this case? Of course I can add a onClick to the second view type but it seems not right (code separation)

Upvotes: 0

Views: 535

Answers (1)

Andrew Orobator
Andrew Orobator

Reputation: 8656

If your 2nd view type contains a checkbox or a button, that widget is stealing focus from the parent view, and not allowing the parent view to be clicked. In order to prevent this from happening, in your root layout of your 2nd view type you should put

android:descendantFocusability="blocksDescendants"

so that child views are not able to steal the focus of the parent.

Upvotes: 1

Related Questions