ListView中的ImageButton无法点击

时间:2015-04-03 21:00:42

标签: c# android listview

我目前正在学习使用C#(使用xamarin)开发一个Android应用程序,我遇到的问题是我的列表中的ImageButtons无法点击,我似乎无法找到问题。

我在其他帖子中找到的解决方案对我来说也不起作用(Listview itemclick not work

以下是我的代码:

我的片段:

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

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

1 个答案:

答案 0 :(得分:0)

我有2条建议:

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

  • 如果您希望ImageButton和父Listview都可以点击,请尝试将blocksDescendants设置为 beforeDescendants (我从来不必这样做)。 Google doc @ descendantFocusability

GetView的代码段:

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