user2143831
user2143831

Reputation:

Disable Click Event on Android ListView Items

I'm trying to disable multiple click events on listview, say after pressing first click some media gets played from webservice, while it gets played, other items need to be clickable==false , after media got played,other list items can be clickable.

What I'm trying is calling setClickable(true) and setClickable(false) on ListView Object.

Upvotes: 28

Views: 41403

Answers (10)

Jayant Kapila
Jayant Kapila

Reputation: 87

listView.getChildAt(position).setEnabled(false);

listView.getChildAt(position).setClickable(false);

Upvotes: 2

Basher51
Basher51

Reputation: 1329

before onCreate:

private long mLastClickTimeListViewItem = 0; 

To prevent multiple clicks on ListView Items

After onCreate inside the listener for listView,in my case the following:

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

            if (SystemClock.elapsedRealtime() - mLastClickTimeListViewItem < 1000){
                return ;
            }
            mLastClickTimeListViewItem = SystemClock.elapsedRealtime();

            //Do your remaining code magic below...
            ....
            ....

         } // end of onItemClick method   
    }); // end of setOnItemClickListner

Upvotes: 2

Nasz Njoka Sr.
Nasz Njoka Sr.

Reputation: 1118

If what you want is just to diable items being clickable and show the appropriate selector color just use the line

android:listSelector="@android:color/transparent"

in you listview in the layout file(xml)

Upvotes: -1

shyam.y
shyam.y

Reputation: 1491

the above said answers didn't worked for me, so I used list.setEnabled(false) Its worked for me

Upvotes: 2

MD SHAHIDUL ISLAM
MD SHAHIDUL ISLAM

Reputation: 14523

In your custom ArrayAdapter overide isEnabled method as following

@Override
public boolean isEnabled(int position) {
    return false;
}

Upvotes: 71

Ben
Ben

Reputation: 3862

create Adapter for that list, and there override this method

public boolean isEnabled(int position);

then return false when you want to disable the click

Upvotes: 3

CoDe
CoDe

Reputation: 11146

Or in simple way to un-register and register OnItemClickListener can be a better idea.

Upvotes: 1

DropAndTrap
DropAndTrap

Reputation: 1630

Add this to the xml

android:listSelector="@android:color/transparent"

Upvotes: 19

Arvind Kanjariya
Arvind Kanjariya

Reputation: 2097

Manage Click event using flags.

While your media player is running set click to false by using this method.

setClickable(false);

When your media player is stop or not running or on complete set that flag to default value.

 setClickable(true);

Upvotes: 2

Ion Aalbers
Ion Aalbers

Reputation: 8030

Make your own subclass of ArrayAdapter that has AreAllItemsEnabled() return false, and define isEnabled(int position) to return false for a given item in your the ones you want to disable.

Upvotes: 36

Related Questions