禁用ListView上的特定项

时间:2014-12-07 00:58:04

标签: c# android listview android-listview android-arrayadapter

我创建了SimpleAdapter,我将其用于我的ListView

这是我做的:

JavaList<IDictionary<string, object>> menuItemNames = new JavaList<IDictionary<string, object>> ();

JavaDictionary<string, object> sideItem1 = new JavaDictionary<string, object> ();
sideItem1.Add ("text", "Home");
sideItem1.Add ("icon", Resource.Drawable.menu_icon_home);

JavaDictionary<string, object> sideItem2 = new JavaDictionary<string, object> ();
sideItem2.Add ("text", "Sign Out");
sideItem2.Add ("icon", Resource.Drawable.menu_icon_signOut);

JavaDictionary<string, object> sideItem3 = new JavaDictionary<string, object> ();
sideItem3.Add ("text", "Help");
sideItem3.Add ("icon", Resource.Drawable.menu_icon_help);

menuItemNames.Add (sideItem1);
menuItemNames.Add (sideItem2);
menuItemNames.Add (sideItem3);

string[] sideMenuFromArr = { "text", "icon" };
int[] sideMenuToArr = { Resource.Id.textSideMenuItem, Resource.Id.imgViewSideMenuItemIcon };

// this is where I created the adaper
SimpleAdapter sideMenuListAdapter = new SimpleAdapter (context, menuItemNames, Resource.Layout.item_menu, sideMenuFromArr, sideMenuToArr);

在此之后,我只是将此列表应用于我的ListView

this.absHomeSideMenuList.Adapter = sideMenuListAdapter;

如果我想在某个时候禁用其中一个项目,我该怎么办? 我想将它保留在列表中但删除click事件并将文本颜色更改为灰色以使其看起来已禁用?

1 个答案:

答案 0 :(得分:2)

覆盖BaseAdapter#isEnabled(int position)方法以指定启用/禁用哪个列表项。它应该使用适配器保存的基础数据来确定特定的列表项。您必须将BaseAdapter或其任何后代子类化为子类。根据列表项的状态覆盖getView(int position, View convertView, ViewGroup parent)方法指定列表项的外观。当适配器保留的基础数据发生更改并且您希望与适配器连接的列表视图反映该调用BaseAdapter#notifyDataSetChanged()时。