Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android gridview item change

I would like to change the GridView item background in onCreateView(), after setting the adapter. I tried this way:

final int numVisibleChildren = gv_categories.getAdapter().getCount();       

    for ( int i = 0; i < numVisibleChildren; i++ ) {
        int positionOfView = i + 1;

        try {   
            if (positionOfView == Integer.valueOf(m_transactionItem.getIcon())) { 
                View view = gv_categories.getChildAt(i);
                int nColor = Color.parseColor(m_transactionItem.getcolor());
                if(view != null) {
                    view.setBackgroundColor(nColor);
                }
            }
        } catch(Exception e) {
            Log.e("GridviewItemErr", e.toString());
        }
    }

But view is always null. This is my custom adapter that contains an ImageView and the TextView:

public class CategoryAdapter extends BaseAdapter {
private Context mContext;
int id = 0;
private LayoutInflater inflater;
ViewHolder holder;

public static class ViewHolder {
    private TextView text;
    private ImageView icon;
    private int mId;

    public int getId() {
        return mId;
    }
    public void setId(int id) {
        mId = id;
    }


}

public CategoryAdapter(Context c) {
    mContext = c;
    inflater = (LayoutInflater) c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

public int getCount() {
    return mAllIcons.length;
}

public Object getItem(int position) {
    return 0;
}

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

// create a new ImageView for each item referenced by the Adapter
@Override
public View getView(int position, View convertView, ViewGroup parent) {

    if (convertView == null) {
        id++;
        holder = new ViewHolder();
        convertView = inflater.inflate(R.layout.categories_grid_item, null);
        holder.icon = (ImageView) convertView.findViewById(R.id.ivGrid_Item_Category);
        //holder.icon.setId(id);
        holder.icon.setScaleType(ImageView.ScaleType.FIT_CENTER);
        holder.text = (TextView) convertView.findViewById(R.id.tvGrid_Item_Category);
        //holder.text.setId(id);
        holder.setId(id);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }

    holder.icon.setImageResource(mAllIcons[position]);
    holder.text.setText(mCategNames[position]);


    convertView.setTag(holder);
    return convertView;
}

private Integer[] mAllIcons = {
        R.drawable.icon_1_prijevoz_neselktirane,
        R.drawable.icon_2_putovanje_neselktirane,
        R.drawable.icon_3_hoteli_neselektirane,
        R.drawable.icon_4_odrzavanje_neselktirane,
        R.drawable.icon_5_zabava_neselktirane,
        R.drawable.icon_6_kucne_potrepstine_neselektirane,
        R.drawable.icon_7_odjeca_neselektirane,
        R.drawable.icon_8_trgovina_neselektirane,
        R.drawable.icon_9_rezije_neselktirane,
        R.drawable.icon_10_restorani_neselktirane,
        R.drawable.icon_11_slobodno_vrijeme_neselktirane,
        R.drawable.icon_12_luksuz_neselktirane,
        R.drawable.icon_13_odmor_i_rekreacija_neselktirane,
        R.drawable.icon_14_ostalo_neselektirane,
        R.drawable.icon_15_prihod_neselektirane,
        R.drawable.icon_16_stalni_prihodi_neselektirane,
        R.drawable.icon_17_default_neselektirane
};

private String[] mCategNames = {
        "Prijevoz", "Putovanje",
        "Hoteli", "Održavanje",
        "Zabava", "Kućne potrepštine",
        "Odjeća", "Trgovina",
        "Režije", "Restorani",
        "Slobodno vrijeme", "Luksuz",
        "Odmor i rekreacija",
        "Ostalo", "Prihod",
        "Stalni prihod", "Default",
};
}

I tried many other ways to do this but I cant figure out how to get "visible" GridView items.

NOTE:

The background needs to be changed when fragment is created, not in onItemClickedListener or onScrollListener or like...

like image 355
pavle Avatar asked May 06 '26 14:05

pavle


1 Answers

I think that your problem arises from the fact that you are trying to reference the grid's views before your ImageAdapter had created them, if you wish really to set the background color at the time of onCreateView() and not at the time of calling getView(), I would suggest that you use getViewTreeObserver().addOnGlobalLayoutListener , to make sure that the gridview had been properly inflated before calling getChildAt(i), your code would look something like this:

gv_categories.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
    @Override
    public void onGlobalLayout() {
        //At this point the layout is complete and the 
        //dimensions of myView and any child views are known.
        final int numVisibleChildren = gv_categories.getAdapter().getCount();       
        for ( int i = 0; i < numVisibleChildren; i++ ) {
            int positionOfView = i + 1;
            try {   
                if (positionOfView == Integer.valueOf(m_transactionItem.getIcon())) { 
                View view = gv_categories.getChildAt(i);
                int nColor = Color.parseColor(m_transactionItem.getcolor());
                    if(view != null) {
                        view.setBackgroundColor(nColor);
                    }
                }
            } catch(Exception e) {
            Log.e("GridviewItemErr", e.toString());
        }
    }
});
like image 163
Amer Elhabbash Avatar answered May 09 '26 02:05

Amer Elhabbash



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!