This is common problem that will occur when we want to change item background in Gridview based on position. Scrolling too fast on Gridview make selected / higlight item that we identify based on position, will showing random changes.
To solve this issue, we just need to define simple logic if..else on getView() in Adapter to set backgroundColor of item based on position. Here is the code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder holder; // to reference the child views for later actions View v = convertView; if (convertView == null) { LayoutInflater li = LayoutInflater.from(mContext); v = li.inflate(R.layout.item, parent, false); // cache view fields into the holder holder = new ViewHolder(); // Set Text holder.dateText = (TextView) v.findViewById(R.id.item_text); // Associate the holder with the view for latter lookup v.setTag(holder); } else { holder = (ViewHolder) v.getTag(); } holder.position = position; // Get day number by position int dayNumber = dates[position]; // Get day name CalendarUtils calendarUtils = new CalendarUtils(); String dayName = calendarUtils.getDateName(dayNumber, "SHORT"); // Set text resource on each position holder.dateText.setText(Integer.toString(dayNumber) + " – " + dayName); // mark current day as focused if (dates[position] == selectedDate.get(Calendar.DAY_OF_MONTH)) { v.setBackgroundColor(Color.parseColor("#48B93D")); Log.v(ANDROID_TAG, dates[position].toString()); } else { // Force v.setBackgroundColor(Color.parseColor("#484848")); } return v; } |