我想在设置适配器后更改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
。这是我的自定义适配器,包含ImageView
和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",
};
}
我尝试了很多其他方法来做到这一点,但我无法弄清楚如何获得“可见”的GridView项目。
注意:
创建片段时需要更改背景,而不是onItemClickedListener
或onScrollListener
或类似...
我认为你的问题源于你在ImageAdapter创建它们之前尝试引用网格的视图,如果你真的要在onCreateView()
时设置背景颜色而不是在调用getView()
时,我会建议您使用getViewTreeObserver().addOnGlobalLayoutListener
,以确保在调用getChildAt(i)之前Gridview已正确膨胀,您的代码看起来像这样:
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());
}
}
});
根据我的理解,使用'Adapter',我认为你不需要在创建时直接触摸视图。只需更改适配器的数据集,以及系统调用时
public View getView(int position, View convertView, ViewGroup parent)
您可以创建新视图或重用转换后的视图。
另一种情况是数据在运行时发生变化,请确保更新发生在工作线程中。并致电:
public void notifyDataSetChanged()
通知UI线程相应地更改UI。
您应该根据位置从ListAdapter
方法内部更改getView()
项目。由于View
s被回收,如果位置与您想要更改的位置不匹配,请不要忘记还原更改(例如将背景重置为默认值)。
AdapterView
s如GridView
根据布局和滚动位置从他们的Adapter
s动态加载他们的项目,所以他们没有在最初的View
人口中填充。