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);
}
};
:
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);
label.setBackgroundColor(
context.getResources().getColor(R.color.list_item_selected_color));
} else {
}
}
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.