Hugo
Hugo

Reputation: 1672

Onclicklistener for a child view inside a ListView item only fired second time after onitemclicklistener

This is the situation: I have a Listview with some items. Inside each item there are two clickable views that are only shown after user clickes in their parent item.

It works like this:

productList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(final AdapterView<?> parent, final View view, final int position, long id) {

                TextView txtAdd = (TextView) view.findViewById(R.id.txt_add_units);
                txtAdd.setVisibility(View.VISIBLE);
                txtAdd.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        ...
                    }
                });

                TextView txtRemove = (TextView) view.findViewById(R.id.txt_remove_units);
                txtRemove.setVisibility(View.VISIBLE);
                txtRemove.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        ...
                    }
                });

                basketProductsListAdapter.notifyDataSetChanged();

            }

        });

The first time a listview item is clicked, its txtAdd and txtRemove child views are shown and then if user clicks them, code inside their onclickListener is executed.

The problem appears ONLY IN THE FIRST listview item. First time it is clicked txtAdd and txtRemove are shown, BUT THE FIRST TIME user clicks over the child views, their parent's ItemClickListener is fired again, and then the child views (txtAdd and txtRemove) are not receiving the clickListener. Following times they are work perfectly.The weird thing is that it only happens first time on the first item in the listView.

Any ideas? thanks in advance!

Upvotes: 2

Views: 1029

Answers (1)

Jai
Jai

Reputation: 3310

Add this line to your listview xml android:descendantFocusability="blocksDescendants"

The Thing is our child view may be focusable so the first click will be for the focus and after that the itemClick will work. to avoide focusable add that line in the xml

Still if it does not work then try adding

android:focusable="false"
android:focusableInTouchMode="false"

to the child View of the listview

Upvotes: 1

Related Questions