gpt4 book ai didi

Android gridview 项目更改

转载 作者:行者123 更新时间:2023-12-02 03:39:39 25 4
gpt4 key购买 nike

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

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 项目。

注意:

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

最佳答案

我认为你的问题是因为你试图在你的 ImageAdapter 创建它们之前引用网格的 View ,如果你真的希望在 onCreateView() 时设置背景颜色> 而不是在调用 getView() 时,我建议您使用 getViewTreeObserver().addOnGlobalLayoutListener ,以确保在调用之前 gridview 已正确膨胀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());
}
}
});

关于Android gridview 项目更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20891828/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com