ListView item coloring on selection not working properly in android API version 8/9

 ̄綄美尐妖づ 提交于 2019-12-21 05:49:09

问题


I'm currently using a ListView to display a sort of items. I've implemented an action mode to select multiple items and to delete massively that works well in android 4.x. But when I tried with API version 8 or 9 (android 2.2.x/2.3.x), selection works internally as expected but row items are colored randomly.

If user selects first row, internally first row is selected, but row number 4 is colored. When I click another row, this row and first one is colored. It's a strange behaviour which I expect to work normally as on 4.x devices.

Long-click overriding to activate action mode and check long-clicked item of ListView:

@Override
public boolean onItemLongClick(AdapterView<?> adapterView, View view, int position, long id) {
    if (actionMode == null) {
        listView.setOnItemClickListener(new CABClickListener());
        actionMode = startActionMode(new ListActionMode());
        // Check item pressed with long click
        listView.setItemChecked(position, true);
        view.setBackgroundColor(checkedColor);
        logger.debug("Item at pos. " + position + ", checked.");
    }
    return true;
}

CABClickListener, responsible to check/uncheck items of ListView, marking them internally and changing its background color:

private final class CABClickListener implements AdapterView.OnItemClickListener {
    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
        if (listView.isItemChecked(position)) {
            view.setBackgroundColor(checkedColor);
            logger.debug("Item at pos. " + position + ", checked.");
        } else {
            view.setBackgroundColor(uncheckedColor);
            logger.debug("Item at pos. " + position + ", unchecked.");
        }
    }
}

Those classes/methods are inside the Activity, listView is declared in top of it.

More considerations:

  • Using ActionBarSherlock (it shows the CAB but I think this is not important here) and Roboguice, but I haven't any problem with that.
  • I was always developing with the emulator. In addition, I couldn't try my app with android 3.x (got problems with this version, emulator doesn't launch), so I don't know if the problem persists in these versions. UPDATE: Tested in android 3.0 API 11, works well as on 4.x.
  • I debugged the code and Views in both methods are ok, but when I call view.setBackgroundColor(checkedColor);, another View is colored.

Any suggestion? Hope anyone can help!


回答1:


Wow, the problem was in my own implementation of ArrayAdapter, where I tried to apply view holder pattern. I discarded this initially because I tested the same with a simple ArrayAdapter and problem was still there.

The difference between android APIs is that when item is clicked in the 8-10 API, all list is repainted, reusing existing views. Therefore, when you click in a item (View), this is colored but immediatly android repaints all list, reusing the views, and making the colored one to be in other position. When a list view item gets clicked in >11 API, anything gets repainted (yes, great performance improvement between versions) and the correct item view was painted succesfully (calling properly view.setBackgroundColor(checkedColor)).

Finally, I solved this strange behaviour, storing checked state in the entities. With this, when the view has to be recycled, checked value can be recovered and list item can be colored without problems.

I post my GenericListAdapter.getView() method and related for anyone interested.

GenericListAdapter<T>.getView():

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ItemViewHolder<T> viewHolder = null;

    if (convertView == null || !(convertView.getTag() instanceof ItemViewHolder<?>)) {
        logger.debug("New view: " + convertView + " at position: " + position);
        LayoutInflater mInflater = LayoutInflater.from(context);
        convertView = mInflater.inflate(resource, null);

        viewHolder = GenericViewHolderFactory.createInstance(clazz);
        viewHolder.setContext(context);
        viewHolder.saveViewContents(convertView);

        convertView.setTag(viewHolder);
    } else {
        logger.debug("Reusing view: " + convertView + ", at position: " + position);
        viewHolder = (ItemViewHolder<T>) convertView.getTag();
    }

    T entity = getItem(position);
    viewHolder.setViewFields(entity, convertView);

    return convertView;
}

And the ViewHolder implementation which refreshes the recycled view:

public class EventItemViewHolder implements ItemViewHolder<Event> {

...

    @Override
    public void setViewFields(Event event, View convertView) {
        name.setText(event.getName());
        amount.setText(event.getTotalAmount().toString());

        if (event.isChecked()) {
            convertView.setBackgroundColor(checkedColor);
        } else {
            convertView.setBackgroundColor(uncheckedColor);
        }
    }
}

I hope I've explained myself well.



来源:https://stackoverflow.com/questions/11510841/listview-item-coloring-on-selection-not-working-properly-in-android-api-version

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