[英]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. 我目前正在学习用C#(使用xamarin)开发一个android应用程序,我遇到的问题是列表中的ImageButtons不可单击,而且我似乎找不到问题所在。
The solution I found in other post did not work for me either ( Listview itemclick not work ) 我在其他帖子中找到的解决方案也不适合我( Listview itemclick不起作用 )
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: 我有2条建议:
setOnClickListener
in GetView
. 在setOnClickListener
中添加GetView
。 My example is in Java, don't have C#. 我的示例在Java中,没有C#。 Remove descendantFocusability
in your layout. 在布局中删除descendantFocusability
。 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. 但我相信,如果将其设置为blocksDescendants ,则像ImageButton这样的布局的子级是不可单击的。
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). 如果你想两者的ImageButton和父列表视图是点击,然后尝试设置blocksDescendants
到beforeDescendants而不是(我从来没有这样做)。 Google doc @ descendantFocusability 谷歌文档@Descendant
Code snippet for GetView: GetView的代码段:
public override View GetView (int position, View convertView, ViewGroup parent)
{
...
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.