I have a girdview with 5 columns and i want to color the whole row that means 5 cells on clicking. click works fine but when i scroll them on clicking color is set two or three
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;
}
}