I'm currently learning to develop an android app in C#(with xamarin), The problem I have is the ImageButtons in my list are not clickable and I can't seem the find the problem.
The solution I found in other post did not work for me either (Listview itemclick not work)
Here are my codes:
My Fragment:
public class MenuFragment : Fragment
{
private ListView menuListView;
private List<MenuCategory> menuCategories;
public override View OnCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
//Create mock menu category
menuCategories = new List<MenuCategory>();
menuCategories.Add(new MenuCategory(Resource.Drawable.specialsPromotions, "Promotion and Specials"));
menuCategories.Add(new MenuCategory(Resource.Drawable.dinner, "Dinner"));
var view = inflater.Inflate (Resource.Layout.MenuFragment, container, false);
menuListView = view.FindViewById<ListView> (Resource.Id.menuListView);
MenuCategoryAdapter adapter = new MenuCategoryAdapter (Activity, menuCategories);
menuListView.Adapter = adapter;
menuListView.ItemClick += menuListViewItemClick;
return view;
}
void menuListViewItemClick (object sender, AdapterView.ItemClickEventArgs e)
{
Console.WriteLine ("Menu Category selected: " + menuCategories [e.Position].CategoryName);
}
}
My Adapter:
public class MenuCategoryAdapter : BaseAdapter<MenuCategory>
{
List<MenuCategory> menuCategories;
private Context context;
public MenuCategoryAdapter (Context context, List<MenuCategory> menuCategories)
{
this.context = context;
this.menuCategories = menuCategories;
}
public override int Count
{
get{ return menuCategories.Count; }
}
public override long GetItemId(int position)
{
return position;
}
public override MenuCategory this[int position]
{
get{ return menuCategories [position];}
}
public override View GetView (int position, View convertView, ViewGroup parent)
{
View row = convertView;
if (row == null)
{
row = LayoutInflater.From (context).Inflate (Resource.Layout.MenuCategoryListView, null, false);
}
ImageButton button = row.FindViewById<ImageButton> (Resource.Id.menuCategoryName);
button.SetBackgroundResource (menuCategories [position].BackgroundImage);
return row;
}
}
My layouts:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView
android:id="@+id/menuListView"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:descendantFocusability="blocksDescendants"/>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageButton
android:layout_width="match_parent"
android:layout_height="100dp"
android:gravity="center"
android:id="@+id/menuCategoryName"
android:focusable="false"
android:focusableInTouchMode="false"/>