-1

I want to change text color of grid item when selected and one time only one item should be selected, This is done already but If gridview have many items that if I am selecting any Item and scrolling gridview then It will select random item in grid and also allowing to selecte multiple items. I tried many answers but not found any single solution. can anyone have any idea ??

I tried answer given by below link but it didn't solve my problem

When the GridView scrolls, it changes its view's activated status

I will share my code with adapter getView method and onItemSelected() method of grid view

 gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, final int position, long arg3) {

                view.setSelected(true);
                TextView tv = (TextView) view;                 // Get the current selected view as a TextView
                tv.setTextColor(Color.parseColor("#50d1e4"));  // Set the current selected item text color
                TextView previousSelectedView = (TextView) gridview.getChildAt(previousPosition);   // Get the last selected View from GridView

                // If there is a previous selected view exists
                if (previousPosition != -1 && previousPosition!=position) {
                    previousSelectedView.setSelected(false);                        // Set the last selected View to deselect
                    previousSelectedView.setTextColor(Color.parseColor("#162750")); // Set the last selected View text color as deselected item
                }
                previousPosition = position;

        }
    });

here initial value of previousPosition = -1 (it is type of int)

getView method of adapter is given below

public static class ViewHolder
{
    public TextView txt_time_slot;
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    final ViewHolder view;
    final Context context = parent.getContext();
    if (inflater == null)
        inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    if (convertView == null) {
        view = new ViewHolder();
        convertView = inflater.inflate(R.layout.grid_item, null);
        view.txt_time_slot = (TextView) convertView.findViewById(R.id.txt_item_time_slot);
        convertView.setTag(view);
        convertView.setId(0);
    } else {
        view = (ViewHolder) convertView.getTag();
    }

        if (listValues.get(position) != null)
        {
            view.txt_time_slot.setText(listValues.get(position));

        }

    return convertView;
}

here is grid_item.xml

 <?xml version="1.0" encoding="utf-8"?>
 <TextView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/txt_item_time_slot"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="10:20 AM"
        android:gravity="center_horizontal"
        android:padding="3dp"
        android:textColor="#162750"
        android:textStyle="bold"
        android:textSize="30sp"/>
Community
  • 1
  • 1
GS Nayma
  • 1,745
  • 19
  • 24

1 Answers1

0

No need to add previousPosition variable. Add one parameter in your model. You have list of any model, add one parameter isSelected (boolean value). when user click on it, set this as true.

In getView method of adapter, check isSelected value of item. You can get it from your list(listValue.get(position).isSelected()). Based on return value, you can change background. It also helps you to get selected value also.

for ex:

     Product product = mProcucts.get(position);
    itemViewHolder.textView.setText(product.getProductName());
    if(product.isSelected()){   itemViewHolder.textView.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.shape_button_checked));
    }else{
        itemViewHolder.textView.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.shape_button_unchecked));
    }