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

前端 未结 1 1884
清歌不尽
清歌不尽 2021-01-24 01:48

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

相关标签:
1条回答
  • 2021-01-24 02:23

    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;
    
    }
    }
    
    0 讨论(0)
提交回复
热议问题