Search code examples
androidlistviewandroid-viewlistitemviewgroup

Android renabling listitem in listview after disabling it


I have a BaseActivity which extends Activity and all the other activities extends this BaseActivity.

What I do is in a state of my application, I want to disable all the items on the screen from the BaseActivity then enable them again by using the following code:

private void DisableChildClickEvents(ViewGroup viewGroup, boolean enabled) {
    for (int i = 0; i < viewGroup.getChildCount(); i++) {
        View childView = viewGroup.getChildAt(i);
        childView.setClickable(enabled);
        childView.setEnabled(enabled);
        if (childView instanceof ViewGroup) {
            DisableChildClickEvents((ViewGroup)childView, enabled);
        }
    }
}

The problem that I have a ListView in one of the activities which this code disables it and all clicks on it's items, but it can't enable clicking on them again. Which ListItem shows that it can be clicked and its color changes while clicking but the action that is supposed to do is not reachable after disabling them.


Solution

  • Try this:

    if (childView instanceof ListView) {
                   if (enabled == true) {
                       ListView list = (ListView) childView;
                       list.getOnItemClickListener();
                   }