0

I want to use ripple effect for this I set to android:clickable="true" but when I do this onClick event is not working,but onLongClick is working.How can I resolve this ?

If I don't set android:clickable it is working.

My layout:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:paddingBottom="4dp"
              android:paddingTop="4dp"
              android:orientation="horizontal"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:clickable="true"
              android:longClickable="true"
              android:background="@drawable/ripple">

Code:

listView.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                Log.w("Item Clicked","Ok");
            }
        });
        listView.setOnItemLongClickListener(new OnItemLongClickListener() {
            public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
                    int pos, long id) {
                Log.w("Item Long Clicked","Ok");
                }
        }); 
Okan
  • 1,379
  • 3
  • 25
  • 38

2 Answers2

1

Instead of using setOnItemClickListener on the listview try using setOnClickListener to each of your views returned from your adapter. You'll need to use setItemsCanFocus setting up your list.

ListView OnItemClickListener Not Responding?

Your OnItemClickListener is disabled for your listview rows, because you have a clickable linearlayout in the row layout, which takes over the focus.

You can also take a look here: OnItemClickListener and OnClickListener not working for ListView

Community
  • 1
  • 1
A B
  • 338
  • 3
  • 10
0

This is an interesting problem because somewhere the click is not registering the event. I have a feeling it has something to do with this other answer related to your problem.

When you set the OnItemClickListener, the event onItemClicked will only be called if the child of the ListView does not have the OnClickListener set.

To be honest there isn't enough context with what you are doing to really tell you much except to set onClickListeners on the LinearLayout instead of the ListView. Alternatively, set the ripple effect on the ListView rows instead (but I don't know where this is being set).

Edit: To answer your question, it does work. Just doesn't work in ListView because of the focus.

Community
  • 1
  • 1
Andy
  • 10,553
  • 21
  • 75
  • 125
  • I am so confused now.My problem is same which you gave question,but i didn't understand what should i do – Okan Jan 29 '15 at 15:09
  • Hmm, well basically if the child Views of the ListView have clickable set to true, its not going to call your `onItemClickListener` or `onItemLongClickListener`. If its not set it works. So that we know is true. But why. Good question. I am going to look at the source real quick. I don't have the answer yet. But the way to get around it for now is to move around how you have the click listeners set. – Andy Jan 29 '15 at 15:12
  • The other answer to your question is one workaround to get things working. As is your code won't work because you have clickable set to true. Like I said. In order to get the ripple effect you have to change how you are setting things. – Andy Jan 29 '15 at 15:58
  • Can you explain more ? What should i do – Okan Jan 29 '15 at 15:58
  • Hmm, I am just curious, try setting your `LinearLayout`'s click listener to null. So `linearLayout.setOnClickListener(null);`. See what that does. – Andy Jan 29 '15 at 16:03
  • Look at the link A B provided to get your answer. The best way to solve your problem is to just not use `onItemClickListener`. You haven't given us enough of your code to give you an example. But the other StackO answers have the examples you need. – Andy Jan 29 '15 at 16:10