I want to change text color of selected item in listview.
Main
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sub_alimentacao);
List<Tag> tags = getTagsSubAlimentacao();
final ListView listView = (ListView) findViewById(R.id.subAlimentacao);
listView.setAdapter(new TagSubAlimentacaoAdapter(this, tags));
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, final View view, final int position, long id) {
TextView c = (TextView) view.findViewById(R.id.local);
LikeButton lk = (LikeButton) view.findViewById(R.id.gostei);
lk.setEnabled(false);
//OBTEM A COR EM INTEIRO E CONVERTE PARA HEXADECIMAL
Integer intColor = c.getCurrentTextColor();
String hexColor = "#" + Integer.toHexString(intColor).substring(2);
if (hexColor.equalsIgnoreCase("#2196F3")){
c.setTextColor(Color.parseColor("#aaaaaa"));
lk.setLiked(false);
}else{
c.setTextColor(Color.parseColor("#2196F3"));
lk.setEnabled(true);
lk.setLiked(true);
}
}
});
}
Adapter
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Tag tag = tags.get(position);
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layoute = inflater.inflate(R.layout.item_sub, null);
TextView titulo = (TextView) layoute.findViewById(R.id.local);
LikeButton lk = (LikeButton) layoute.findViewById(R.id.gostei);
titulo.setText(tag.getTitulo());
lk.setLiked(tag.getAtivo());
return layoute;
}
it's working fine when was clicked. The color was changed. But i have one problem i.e.,
For example i am having 10 items in listview at first only 5 items visible(because of screen resolution) if i am scrolling i can visible the next 5 items.
When I select the first 5 elements, the colors are changed. However if rolling for next 5 elements
The colors of the 5 first elements are back to the original state.