C# ListView中的ImageButton不可单击

C# ListView中的ImageButton不可单击,c#,android,listview,C#,Android,Listview,我目前正在学习在Cwith xamarin中开发一个android应用程序,问题是我列表中的ImageButtons不可点击,而且我似乎无法找到问题所在 我在其他帖子中找到的解决方案对我也不起作用 这是我的密码: 我的片段: public class MenuFragment : Fragment { private ListView menuListView; private List<MenuCategory> menuCategories; publi

我目前正在学习在Cwith xamarin中开发一个android应用程序,问题是我列表中的ImageButtons不可点击,而且我似乎无法找到问题所在

我在其他帖子中找到的解决方案对我也不起作用

这是我的密码:

我的片段:

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);
    }

}
我的适配器:

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;
    }

}
我的布局:

<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"/>
我有两个建议:

在GetView中添加setOnClickListener。我的例子是Java,没有C。 删除布局中的子体焦点。这可能没有必要。但如果设置为BlocksDescents,那么像ImageButton这样的布局的子项是不可单击的,我相信

如果您希望ImageButton和父Listview都可以单击,那么请尝试将BlocksDescents设置为Before子代,而我从来没有这样做过。谷歌文档@

GetView的代码段:

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

如果您将属性android:clickable=true添加到项目线性布局中会怎么样?您的意思是在my ImageButton中将clickable设置为true?我试过了,但没用……不,我是说你的线性布局,真的。它可能会忽略单击,因此您的ImageButton也不会收到该单击。@joao2fast4u尝试在my LinearLaouts中添加clickable,但也不起作用..:然后,将onClickListener添加到MenuCategoryAdapter GetView方法中的ImageButton。
public override View GetView (int position, View convertView, ViewGroup parent)
{
   ...
   button.setOnClickListener(new View.OnClickListener() {
         public void onClick(View v) {
         ...