简体   繁体   中英

ImageButton in ListView not clickable

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"/>

I have 2 suggestions:

  • Add the setOnClickListener in GetView . My example is in Java, don't have C#.
  • Remove descendantFocusability in your layout. This may not be necessary. But if it is set to blocksDescendants , then the child of the layout like ImageButton is not clickable, I believe.

  • If you want both ImageButton and the parent Listview to be clickable, then try setting blocksDescendants to beforeDescendants instead (I never had to do this). Google doc @ descendantFocusability

Code snippet for GetView:

public override View GetView (int position, View convertView, ViewGroup parent)
{
   ...
   button.setOnClickListener(new View.OnClickListener() {
         public void onClick(View v) {
         ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM