Android gridview项目更改

时间:2014-01-02 21:03:28

标签: android gridview

我想在设置适配器后更改GridView中的onCreateView()项背景。我试过这种方式:

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());
        }
    }

view总是null。这是我的自定义适配器,其中包含ImageViewTextView

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",
};
}

我尝试了许多其他方法来做到这一点,但我无法弄清楚如何获得“可见”的GridView项目。

注意:

创建片段时需要更改背景,而不是onItemClickedListeneronScrollListener或类似...

3 个答案:

答案 0 :(得分:1)

我认为您的问题源于您在ImageAdapter创建它们之前尝试引用网格的视图,如果您希望在onCreateView()时真正设置背景颜色而不是在调用getView()时,我建议您使用getViewTreeObserver().addOnGlobalLayoutListener,以确保在调用getChildAt(i)之前网格视图已正确膨胀,您的代码看起来像这样:

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());
        }
    }
});

答案 1 :(得分:0)

根据我的理解,使用'Adapter',我认为您不需要在创建时直接触摸视图。只需更改适配器的数据集,以及系统调用

public View getView(int position, View convertView, ViewGroup parent)

您可以创建新视图或重复使用已转换的视图。

另一种情况是数据在运行时发生变化,请确保更新发生在工作线程中。并致电:

public void notifyDataSetChanged()

通知UI线程相应地更改UI。

答案 2 :(得分:0)

您应该根据位置更改ListAdapter方法内的getView()项。由于View已被回收,因此如果位置与您要更改的位置不匹配,请不要忘记还原更改(例如将背景重置为默认值)。

AdapterView,例如GridView根据布局和滚动位置从Adapter动态加载项目,因此在初始View期间不会填充它们人口。