Question

I want to set Background resource of selected item in gridview. When I select an item, it's ok, but the program is not change background resource of only selected item. It changes another item's background too. I can not solve this problem. I have researched, but I couldn't find my problem's solution. Here is my code:

grid_similar_items.setOnItemClickListener(new OnItemClickListener() {

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) 
    {
        for (int i = 0; i < grid_similar_items.getChildCount(); i++)
        {
           View v = grid_similar_items.getChildAt(i);
           v.setBackgroundColor(Color.TRANSPARENT);

        }
grid_similar_items.getChildAt(position).setBackgroundResource(R.drawable.selected_item_bg_color);
}

Note: I have a year list of items for example: 2012 - 2013 - 2014 and assume that there are 8 items in 2012, 52 items in 2013, and 52 items in 2014. in short gridview list(2012), I did not faced this problem. However, in large gridview list(2013 and 2014) I have faced this problem.

Thank you for your answers.

Was it helpful?

Solution

One Way is to create custom adapter class which return view of your choose, or you can use this link gridview

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top