ListView setSelection - select item but do not change the background

0

I have a problem, when wanting to select an item after filling the listview, it does not use the drawable that I have of background

<item
    android:state_selected="true"
    android:drawable="@color/colorTouch"/>
<item
    android:state_selected="false"
    android:drawable="@android:color/transparent"/>

This is my xml that I use for the listview

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/selecter">

<TextView
    android:id="@+id/title_item"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="15dp"
    android:layout_marginLeft="15dp"
    android:textSize="20sp"
    android:textStyle="bold"
    android:text=""
    android:textColor="#000"/>

<TextView
    android:id="@+id/artista_item"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="15dp"
    android:layout_marginBottom="15dp"
    android:textSize="15sp"
    android:text=""
    android:textColor="#000"/>

I need to select the item and use the background, this is my code

listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
listView.setAdapter(adapter);
listView.setSelection(pocision);
listView.setOnItemClickListener(listListener);
View v = getViewByPosition(pocision,listView);
v.setSelected(true);

This is the method I use getViewByPosition

private View getViewByPosition(int pos, ListView listView) {
    final int firstListItemPosition = listView.getFirstVisiblePosition();
    final int lastListItemPosition = firstListItemPosition + listView.getChildCount() - 1;

    if (pos < firstListItemPosition || pos > lastListItemPosition ) {
        return listView.getAdapter().getView(pos, null, listView);
    } else {
        final int childIndex = pos - firstListItemPosition;
        return listView.getChildAt(childIndex);
    }
}

The item is selected but it does not use the background I need, instead when I select it with the event if it works:

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

        view.setSelected(true);

    }

This is my adapter:

public class SongAdapter extends BaseAdapter{

public SongAdapter(Context context){
    this.context = context;
    ListaCanciones listaCanciones = new ListaCanciones();

    lista = listaCanciones.getLista();
}

@Override
public int getCount() {
    return lista.size();
}

@Override
public Object getItem(int position) {
    return lista.get(position);
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    if(convertView==null){
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView= inflater.inflate(R.layout.song_item, parent, false);
    }

    TextView txtTitle = (TextView)convertView.findViewById(R.id.title_item);
    TextView txtArtista = (TextView)convertView.findViewById(R.id.artista_item);

    Cancion cancion = (Cancion)getItem(position);

    txtTitle.setText(cancion.getNombre());
    txtArtista.setText(cancion.getAutor());

    return convertView;
}


private List<Cancion> lista;
private Context context;

}

When I call the event the background changes, what is my error?

    
asked by cristian giraldo carmen 21.06.2017 в 19:20
source

1 answer

0

Your selector has only one state which activates the color, and this is when you select it, for that reason the color is not maintained:

<item
    android:state_selected="true"
    android:drawable="@color/colorTouch"/>
    
answered by 23.06.2017 в 06:51