【问题标题】:ImageButton in ListView not clickableListView中的ImageButton不可点击
【发布时间】:2015-04-03 21:00:41
【问题描述】:

我目前正在学习用 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"/>

【问题讨论】:

  • 如果您将属性android:clickable="true" 添加到您的项目LinearLayout 会怎样?
  • 您的意思是在我的 ImageButton 中将 clickable 设置为 true?我试过了,它没有用......
  • 不,我指的是您的 LinearLayout,确实如此。它可能会忽略点击,因此您的ImageButton 也不会收到该点击。
  • @joao2fast4u 尝试在我的 LinearLaouts 中添加可点击,但这也不起作用.. :(
  • 然后,在您的ImageButton 方法中将onClickListener 添加到您的MenuCategoryAdapter GetView() 方法中。

标签: c# android listview


【解决方案1】:

我有 2 条建议:

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

  • 如果您希望 ImageButton 和父 Listview 都可点击,请尝试将 blocksDescendants 设置为 beforeDescendants(我从来没有这样做过)。谷歌文档@descendantFocusability

GetView 的代码 sn-p:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-09-06
    • 1970-01-01
    • 1970-01-01
    • 2019-11-17
    • 2020-11-28
    • 1970-01-01
    • 1970-01-01
    • 2015-05-04
    相关资源
    最近更新 更多