Set selected View highlighted - random changing of the View while scrolling
What I tried
Hello Guys, I have made a GridView which I've filled over my database. Now I made an onItemClickListner, from the Google GridView tutorial. Now I tried to make that when the user clicks on a picture in the GridView, it keeps selected and gets a backgroundcolor so the user sees which Image he chooses. But exactly this isn't working how it should. I made a If with a boolean which allways when the User clicks on the next View, that the old View gets transperent again and the new clicked View got a backgroundcolor. But after I selected a View and I scroll up and down. The highlighted View changes sometimes randomstyled.
Question
So my question is what I have to change in my onItemClickListner, that this random View changing stops (?), I didn't found o开发者_高级运维ut a way to do so. And is there a easier way to highlight the selected View? For the understanding "selected View" = User clicks on it, than its selected and should be highlighted.
Code
Here's the Code of my onItemClickListner:
//Hier wird die Grindview gefüllt
final GridView gridview = (GridView) findViewById(R.id.SmileyGrind);
gridview.setAdapter(new ImageAdapter(this, info));
//Hier wird gemerkt welches bild
gridview.setOnItemClickListener(new OnItemClickListener() {
boolean color = false;
View vv;
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
Toast.makeText(SFilterConfigActivity.this, "" + position, Toast.LENGTH_SHORT).show();
// gridview.setSelection(position);
if ( color == false){
v.setBackgroundColor(0xFF00FF00);
vv = v;
color = true;
}
else {
vv.setBackgroundColor(0x00000000);
v.setBackgroundColor(0xFF00FF00);
vv = v;
}
}
});
Thx for you help in Advance!
I had the same issue but it was with ListView .follow this it will work for GridView too.
if(position == grid.getSelectedItemPosition())
holder._linear.setBackgroundColor(Color.GRAY);
else
holder._linear.setBackgroundColor(Color.BLACK);
and get the Parent Layout of the Layout that you are using for your GridView and on onClick method of layout just add this .
holder._linear.setBackgroundColor(Color.GRAY);
This will change the Background color of the layout and the above code will solve your random background problem.
精彩评论