SetSelected () works with ListView error

I am trying to just click ListView items to change the background. But it seems to me that this is not real. There are many posts with examples of this, but none of them work reliably. As I understand it, it is somehow connected with the "recycling".

I call view.setSelected () on the OnItemClickListener adapter, and it nicely applies a different background to the selected item according to my settings. But when I select an item due to which the ListView does not have enough space (no matter how), and the skin panel appears (or disappears) inside the ListView - the android forgets my choice and the default style is applied. The same error occurs when you rotate the screen - the item cancels the selection. Therefore, I think that when the getView () adapter is selected, a "deselection" occurs.

Interestingly, my onClick event triggers sending a json request to the background service and receiving and decoding the json response, so it takes some time between changing the contents of the element and changing the contents. Here's what it looks like:

  • I click on a ListView item. It changes the background to the “selected color”.
  • A few points that I'm waiting for.
  • The content of the activity changes according to the response of the service. The ListView displays a scroll bar. Change the background of the item to "default color" (no item selected).

Clicking on elements that do not lead to the appearance of the scroll bar works fine - the selected elements are not canceled after processing the response of the service.

setSelected() getView() . . getView() - : , , , , ( , getView()) !

, . OnItemClickListener:

private AdapterView.OnItemClickListener onCategoryClickListener =
        new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, final View view, final int position,
            long id) {
        categoriesAdapter.setSelectedPosition(position);
        view.setSelected(true);
        // More code here
    }
};

:

private int selectedPosition;
private boolean selectable = true;

public void setSelectedPosition(int position) {
    this.selectedPosition = position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    TextView label = (TextView) View.inflate(context, textViewResourceId, null);
    label.setText(getName(values.get(position)));
    if(selectable) {
        label.setBackgroundResource(R.drawable.list_selector);
        if(position == selectedPosition) {
            label.setSelected(true);  // This does not work. Why?
            label.setBackgroundColor(  // This gives strange results
                context.getResources().getColor(R.color.list_item_selected_color));
        } else {
            // Similar code here, but for deselecting items.
        }
    }
    return label;
}

:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:drawable="@color/list_item_default_color"
        android:state_selected="false" />
    <item
        android:drawable="@color/list_item_selected_color"
        android:state_selected="true"/>
</selector>

, , . , :

  • view.setSelected() view.post()
  • list.setSelection() - ? !
  • TextView , , convertView null. , - , ListView ( - ).
  • ViewHolder, Item, TextView.
+4
2

API 11 +:

1) :

<ListView
        android:choiceMode="singleChoice" />

2) :

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:background="@drawable/selector">

3) android:state_selected android:state_activated:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@color/list_item_selected_color"
        android:state_activated="true"/>
    <item android:drawable="@color/list_item_default_color"/>
</selector>

4) , listView.setItemChecked(index, true);

.. , state_activated, .; .

+10

. , OnItemClickListener getView:

OnItemClickListener:

public void onItemClick(AdapterView<?> parent, final View view, final int position,
        long id) {
    categoriesAdapter.setSelectedPosition(position);
    for (int j = 0; j < parent.getChildCount(); j++) {
        parent.getChildAt(j).setSelected(false);
        parent.getChildAt(j).setBackgroundColor(getContext().getResources().getColor(
            R.color.list_item_default_color));
    }
    view.setBackgroundColor(getContext().getResources().getColor(
        R.color.list_item_selected_color));

    // More code here
}

:

private Integer selectedPosition;

public void setSelectedPosition(int position) {
    this.selectedPosition = position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    TextView label = (TextView) View.inflate(context, textViewResourceId, null);
    label.setText(getName(values.get(position)));
    if(selectedPosition != null) {
        if(position == selectedPosition) {
            label.setBackgroundColor(context.getResources()
                .getColor(R.color.list_item_selected_color));
        } else {
            label.setBackgroundColor(context.getResources()
                .getColor(R.color.list_item_default_color));
        }
    }
    return label;
}

, <selector> , . , - "<selector> "?

0

All Articles