8

I'm doing some AsyncTask work after user clicks an item in my ListView. I'd like to disable the item so it can't be clicked twice. I've simplified the click listener to contain only this method, but it still doesn't do anything for me, the view looks the same and it lets itself be happily clicked again, much to my annoyance.

public void onItemClick(AdapterView<?> parent, View clickedView,
  int position, long id) {
  item = (Episode) parent.getItemAtPosition(position);
  clickedView.setClickable(false);
  clickedView.setEnabled(false);
  clickedView.invalidate();
}

My View for each row is a custom LinearLayout with two TextViews.

Axarydax
  • 16,353
  • 21
  • 92
  • 151

4 Answers4

20

So, you may be using a custom adapter too. If you do, override these methods:

public boolean areAllItemsEnabled() {
    return false;
}

public boolean isEnabled(int position) {
    // return false if position == position you want to disable
}

Then, when you receive a click tell the adapter what was the last item clicked and return false on isEnabled for that position. For instance, you can have a method like this in your adapter:

private int mLastClicked;
public void setLastClicked(int lastClicked){
    mLastClicked = lastClicked;
}
Cristian
  • 198,401
  • 62
  • 356
  • 264
  • hi im having a hard time getting the position of the last item clicked can you enlighten me up for this thanks – NoobMe May 19 '14 at 03:07
18

If you want disabling item click in list view use clickedView.setClickable(true);

Andro Selva
  • 53,910
  • 52
  • 193
  • 240
user622689
  • 201
  • 2
  • 4
  • 9
    This sounds very wrong, but for whatever reason, it works. Strange, maybe a pro can enlighten us why setClickable(true) will actually have the desired effect of making the view non-clickable. – Bachi Jul 19 '11 at 16:54
  • 1
    indead! Looks like a bug in the system. – user717572 Apr 20 '12 at 15:20
  • 4
    I think this is not a bug. This way `clickedView` intercepts the click. The list element will not respond to any click because `clickedView` intercepts them, hence the list element won't highlight. – mneri Jul 28 '12 at 00:25
  • OMG! I have just spent hours and hours trying to achieve this! I was looking at creating custom selectors, or background colour to hide the selector effect... Thanks heaps! – FMCorz Oct 02 '13 at 17:50
  • It is really weird... setClickable(true) means not clickable. hope this will not be fixed in the future...XDD – derjohng Feb 17 '14 at 15:43
  • This comment is years later, but it might help beginners so here goes: the `onClick` event is handled by the child `View` itself, and the `OnItemClick` is handled by the parent `ListView`. so by enabling `OnClick` of the child `View`, you are overriding the `OnItemClick` of the `ListView`. Although this will work in practice, It's better not to do it this way, and Implement the methods of `ListAdapter` as shown by some answers below. hope that helps – ColonD Oct 06 '17 at 07:42
  • This still works like this. I must say, unintuitive! And an amazing sentence construction. – ZooMagic Oct 25 '17 at 10:05
5

Your problem is not completely clear. I am interpreting your question as that you are expecting onItemClick() to not be called based upon your setEnabled() and setClickable() calls.

I'm not surprised that doesn't work, as onItemClick() is something ListView does, not the child view. Try overriding areAllItemsEnabled() and isEnabled() in your ListAdapter instead.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
4

There are multiple reasons why your approach will not work.

1) onItemClick is only called due to keyboard events. Specifically KeyKevent.KEYCODE_ENTER. It is not called via any other code path. So, handling that even is only useful if you are attempting to provide keyboard/trackball support.

Android source code for AbsListView relevant methods:

public boolean onKeyUp(int keyCode, KeyEvent event) {
    switch (keyCode) {
    case KeyEvent.KEYCODE_DPAD_CENTER:
    case KeyEvent.KEYCODE_ENTER:
        if (!isEnabled()) {
            return true;
        }
        if (isClickable() && isPressed() &&
                mSelectedPosition >= 0 && mAdapter != null &&
                mSelectedPosition < mAdapter.getCount()) {

            final View view = getChildAt(mSelectedPosition - mFirstPosition);
            if (view != null) {
                performItemClick(view, mSelectedPosition, mSelectedRowId);
                view.setPressed(false);
            }
            setPressed(false);
            return true;
        }
        break;
    }
    return super.onKeyUp(keyCode, event);
}

public boolean performItemClick(View view, int position, long id) {
    if (mOnItemClickListener != null) {
        playSoundEffect(SoundEffectConstants.CLICK);
        mOnItemClickListener.onItemClick(this, view, position, id);
        return true;
    }

    return false;
}

2) You are setting the clickable information directly on the view. The views displayed via any AdapterView are ethereal. They are created at the request of the AdapterView and only exist as long as the AdapterView needs them. You should not set any data on them that you want to keep. You can call setEnabled and setClickable on them for immediate effect but if you want that information to persist you need to store it somewhere the Adapter has access to so it can be recreated when the AdapterView recreates the View for that position.

3) You need to handle the onClick event for the actual View being clicked. Where you handle this is up to you. The best place is probably your Adapter which then may or may not pass it up to your Activity depending on what your design requirements are. That is where you need to handle your touch events.

See this code for a simple Activity:

public class PhoneTesting extends Activity {
    private static final String TAG = "PhoneTesting";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Log.d(TAG, "onCreate()");

        List<String> strings = new ArrayList<String>();
        for(int i = 0 ; i < 20 ; i++) {
            strings.add(Integer.toString(i));
        }

        ListView list = (ListView) this.findViewById(R.id.list);

        list.setAdapter(new SimpleAdapter(this, 0, 0, strings));
        list.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Log.d(TAG, "onItemClick: " + id);
            }
        });

    }

    class SimpleAdapter extends ArrayAdapter<String> implements OnClickListener {
        SimpleAdapter(Context context, int resource, int textViewResourceId, List<String> objects) {
            super(context, resource, textViewResourceId, objects);
        }

        SimpleAdapter(Context context, int resource, int textViewResourceId, String[] objects) {
            super(context, resource, textViewResourceId, objects);
        }

        SimpleAdapter(Context context, int resource, int textViewResourceId) {
            super(context, resource, textViewResourceId);
        }

        SimpleAdapter(Context context, int textViewResourceId, List<String> objects) {
            super(context, textViewResourceId, objects);
        }

        SimpleAdapter(Context context, int textViewResourceId, String[] objects) {
            super(context, textViewResourceId, objects);
        }

        SimpleAdapter(Context context, int textViewResourceId) {
            super(context, textViewResourceId);
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            TextView b = position % 2 == 0 ? new Button(this.getContext()) : new TextView(this.getContext());
            b.setText(this.getItem(position));

            b.setOnClickListener(this);

            return b;
        }

        @Override
        public void onClick(View v) {
            TextView t = (TextView) v;
            Log.d(TAG, "onClick: " + t.getText());
        }

        @Override
        public boolean isEnabled(int position) {
            return position % 2 == 0 ? false : true;
        }

    }
}

If you execute this code and click on any of the Views in the ListView you will notice in the logcat output that only onClick is being called. onItemClick is never called.

Also note that isEnabled in the adapter does not seem to effect if the View is clickable or not. I am not sure how to interpret that. What that means though is that if you want to control that property of the View the Adapter needs set that when the View is created and to somehow maintain that information.

Rich Schuler
  • 41,814
  • 6
  • 72
  • 59