Search code examples
androidandroid-linearlayoutandroid-gridviewbaseadapter

gridview change item background by position in Android


I have Custom BaseAdapter. Which i used in my GridView . I trying to change Item background color by position(also i change Textview's text color) this is a my baseadapter source

public class ServiciesGridAdapter extends BaseAdapter {
private Context context;

private List<ServicesListItem> itemList;

public ServiciesGridAdapter(Context context, List<ServicesListItem> mobileValues) {
    this.context = context;
    this.itemList = mobileValues;

}
public View getView(final int position, View convertView, ViewGroup parent) {

    LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View gridView;
    if (convertView == null) {
        gridView = new View(context);
        gridView = inflater.inflate(R.layout.view_servisies_gridview_items, null);

        final LinearLayout mainLayout = (LinearLayout) gridView.findViewById(R.id.gridview_items_layout);


        final TextView serviciesID = (TextView) gridView.findViewById(R.id.servicies_grid_id);
        final TextView serviciesName = (TextView) gridView.findViewById(R.id.servicies_grid_name);
        serviciesID.setText(itemList.get(position).getName());
        serviciesName.setText(itemList.get(position).getIdentifier() + " EUR");



        mainLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mainLayout.setBackgroundResource(R.drawable.rounded_corners_blue);
                serviciesID.setTextColor(Color.parseColor("#ffffff"));
                serviciesName.setTextColor(Color.parseColor("#ffffff"));


            }
        });

    } else {
        gridView = (View) convertView;
    }

    return gridView;
}

@Override
public int getCount() {
    return itemList.size();
}

@Override
public Object getItem(int position) {
    return null;
}

@Override
public long getItemId(int position) {
    return 0;
}


}

when i run my app in item click i can change background color, but when i checked another item,first item's background is a same. Simply, i want to change only one items background each click.

How i can solve my problem?


Solution

  • you have to reset all other view text color and background, so make a loop and do it.. try below code

        mainLayout.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            for(int i=0;i<parent.getChildCount();i++){
                View c = parent.getChildAt(i);
                c.findViewById(R.id.gridview_items_layout).setBackgroundResource(defaultResource);
                (TextView) c.findViewById(R.id.servicies_grid_id).setTextColor(defaultColor);
                ((TextView) c.findViewById(R.id.servicies_grid_name)).setTextColor(defaultColor);
            }
            mainLayout.setBackgroundResource(R.drawable.rounded_corners_blue);
            serviciesID.setTextColor(Color.parseColor("#ffffff"));
            serviciesName.setTextColor(Color.parseColor("#ffffff"));
    
    
        }
    });