Disable Click Event on Android ListView Items

送分小仙女□ 提交于 2019-12-03 02:19:29

In your custom ArrayAdapter overide isEnabled method as following

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

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.

Add this to the xml

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

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

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);

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

Basher51

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

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

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)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!