There is a GridView to which the adapter is connected. The data in the gridview is recorded normally. The problem is this: When I process the OnItemClickListener event in the GridView, the highlighted line is highlighted. However, when I scroll a GridView, then somehow the element indices go astray, and the element I click on is not highlighted, but the element below it is highlighted (depending on how far I scrolled the GridView). When I return the scroll to its original position, the string is allocated as needed.
Screenshots for better understanding:
Next, I scroll down one position and wipe out the same line as in the first screenshot, but the line below it is highlighted.
Result:
Adapter code, and method of clicking on the GridView:
Adapter
public View getView(int position, View convertView, ViewGroup parent) { Cat cat = getItem(position); String s = cat.Uch; if (convertView == null) { convertView = LayoutInflater.from(getContext()).inflate(R.layout.item,null); } ((TextView) convertView.findViewById(R.id.tt)).setText(cat.Hour); ((TextView) convertView.findViewById(R.id.tt1)).setText(cat.Uch); if(s.startsWith("Свободно")) ((LinearLayout) convertView.findViewById(lay)).setBackgroundResource(R.drawable.rect1); else ((LinearLayout) convertView.findViewById(lay)).setBackgroundResource(R.drawable.rect); return convertView; } Push method
gvMain.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View v, int position, long id) { if (gvmaincl==0) { fab1.startAnimation(animationbut); fab1.setVisibility(View.VISIBLE); fab2.startAnimation(animationbut); fab2.setVisibility(View.VISIBLE); } if (v1!=null) { l = (LinearLayout) v1.findViewById(R.id.lay); if (Objects.equals(t1.getText().toString(), "Свободно")) l.setBackgroundResource(R.drawable.rect1); else l.setBackgroundResource(R.drawable.rect); } v1 = gvMain.getChildAt(position); l = (LinearLayout) v1.findViewById(R.id.lay); t = (TextView) v1.findViewById(R.id.tt); t1 = (TextView) v1.findViewById(R.id.tt1); l.setBackgroundResource(R.drawable.rect2); } }); Tell me what the problem is and how it can be fixed.
findViewById()method is very resource intensive to pull them with each press. Get the links to the widgets once when creating the interface (inonCreate()) and assign them to the class fields, then refer to these fields, and do not look for the same thing each time for a new one. - pavlofff