gridview cell view position changes after scrolling …color is set to different cells other than the clicked one

梦想与她 提交于 2019-12-02 04:43:25

Try this and let me know how it works.

public int p=-1;

 /*
...OTHER STUFF...

*/

gridView2 = (GridView) getView().findViewById(R.id.gridView2);

ListAdapter<String> adapter2 = new ListAdapter<String>(
            getActivity(), R.layout.custom_layout, stg1); //stg1-array
    gridView2.setAdapter(adapter2);         
    gridView2.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
            String message;

            int p = (int) Math.ceil(position / 5) * 5; //to color 5 cells from starting on click
        }
    });

/*
...OTHER STUFF...

*/

public class ListAdapter extends ArrayAdapter<String> {
private List<String> items;

public ListAdapter(Context context, int resource, List<Item> items) {
super(context, resource, items);
this.items = items;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

View v = convertView;

if (v == null) {
    LayoutInflater vi;
    vi = LayoutInflater.from(getContext());
    v = vi.inflate(R.layout.custom_layout, null);
    if(position==p) v.setBackgroundColor(Color.GREEN);
    if(position==p+1) v.setBackgroundColor(Color.GREEN);
    if(position==p+2) v.setBackgroundColor(Color.GREEN);
    if(position==p+3) v.setBackgroundColor(Color.GREEN);
    if(position==p+4) v.setBackgroundColor(Color.GREEN);
}      

return v;

}
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!