Not able to change color on checked/uncheck item on gridview

时间:2015-09-14 15:19:12

标签: android android-listview android-gridview

i have written following code:

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

        if (position == 0) {

            gridview.setItemChecked(1, false);
            gridview.setItemChecked(2, false);

            TextView tv = (TextView) view.findViewById(R.id.indian_size);

            tv.setBackgroundColor(Color.parseColor("#FF9AD082"));

            tv.setTextColor(Color.BLUE);

        }
        if (position == 1 || position == 2 ) {

            gridview.setItemChecked(0, false);

            TextView tv = (TextView) view.findViewById(R.id.indian_size);

            tv.setBackgroundColor(Color.parseColor("#FF9AD082"));


            tv.setTextColor(Color.BLUE);


        }

    }
});

When i click the item at 0 position then it change the color and it also work for the position for 1 and 2 .

But now i want like when i click on position 1 and 2 then i need to clear the text color and background color of position 0 and vice versa.

How can i achieve this?

1 个答案:

答案 0 :(得分:0)

Determine if you already checked the item by calling getCheckedItemPositions():

SparseBooleanArray checkedPositions = gridView.getCheckedItemPositions();
if (checkedPositions != null && checkedPositions.get(position)) {
  // Position is already checked, un-check it
} else {
  // Position is un-checked, check it
}