GridView动态加载数据情况下,选中状态的实现
GridView使用AdapterView动态加载数据情况下,无论是在onCreate、Onstart、OnResume方法中调用getChildCount()均为0,这说明数据并没有在Activity最初启动后立即加载为GridView的子View,那如果要把GridView实现为Tab风格的菜单,是必须要调用它的状态选中的,如果在Activity中调用的话就会抛nullpointerexception了,那要怎么操作呢?
/** * 构造菜单Adapter * @param menuNameArray 名称 * @param imageResourceArray 图片 * @return FootBarAdapter */ private FootBarAdapter getMenuAdapter(String[] menuNameArray, int[] imageResourceArray) { ArrayList<HashMap<String, Object>> data = new ArrayList<HashMap<String, Object>>(); for (int i = 0; i < menuNameArray.length; i++) { HashMap<String, Object> map = new HashMap<String, Object>(); map.put("itemImage", imageResourceArray[i]); map.put("itemText", menuNameArray[i]); data.add(map); } FootBarAdapter footbarAdapter = new FootBarAdapter(this, data, R.layout.item_menu, new String[] { "itemImage", "itemText" }, new int[] { R.id.item_image, R.id.item_text }); return footbarAdapter; } public class FootBarAdapter extends SimpleAdapter { public FootBarAdapter(Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to) { super(context, data, resource, from, to); } @Override public View getView(int position, View convertView, ViewGroup parent) { View v = super.getView(position, convertView, parent); if (position == TOOLBAR_ITEM_PAGEHOME) { v.setBackgroundResource(R.drawable.bg_toolbar_item_pressed); } return v; } }
我们继承了SimplerAdapter,然后Override getView方法,在里边进行选中状态的判断和置背景色为选中色。
摘自:http://blog.csdn.net/xjanker2/article/details/6267515
相关推荐
chenjinlong 2020-01-29
vickyjfr 2013-08-27
luoj 2011-08-17
huohu00 2015-04-22
omguare 2012-05-07
SuperMenyII 2019-06-26
AngelicaA 2011-05-02
AABBbaby 2018-07-27
DrZOU 2017-12-21
wuqiong 2017-04-10
Lzn0 2017-03-27
fengyeezju 2015-12-25
superxlcr 2015-05-27
rookieding 2015-04-22
familynt 2015-04-06
mishaki 2015-02-13
江南以北 2014-12-28