Home » Android » Android ListView child View setEnabled() and setClickable() do nothing

Android ListView child View setEnabled() and setClickable() do nothing

Posted by: admin June 15, 2020 Leave a comment

Questions:

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.

How to&Answers:

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

Answer:

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

Answer:

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.

Answer:

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.