列表视图中的可点击图像

时间:2018-11-25 14:44:18

标签: c# listview xamarin xamarin.android

如何在列表视图中显示图像?单击该按钮时有什么作用?我可以使用图像按钮,但是图像不会填充按钮。 该图像是一个垃圾桶图标,单击该图标将必须删除与该列表视图单元格关联的数据。

ListViewLayout:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="8dp">
<TextView
    android:id="@+id/txtvNome"
    android:text="Nome"
    android:layout_weight="1.5"
    android:layout_height="match_parent"
    android:layout_width="0dp"
    android:textColor="#000000"
    android:textSize="15dp"
    android:textStyle="bold"
    android:paddingLeft="5dp" />

<ImageButton
    android:id="@+id/btnDeleteLv"
    android:src="@drawable/delete"
    android:layout_width="0dp"
    android:layout_weight="0.5"
    android:layout_height="wrap_content"    
    android:layout_marginBottom="5dp"
    android:layout_marginLeft="2dp"
    android:layout_marginRight="5dp"
    android:layout_marginTop="0dp"
    android:adjustViewBounds="true"
    android:padding="20dp"
    android:scaleType="fitCenter" />

ListViewAdapter:

public class ListViewAdapter : BaseAdapter
{
    private readonly Activity context;
    private readonly List<Jogador> jogadores;
    public ListViewAdapter(Activity _context, List<Jogador> _jogadores)
    {
        this.context = _context;
        this.jogadores = _jogadores;
    }
    public override int Count
    {
        get
        {
            return jogadores.Count;
        }
    }
    public override long GetItemId(int position)
    {
        return jogadores[position].Id;
    }
    public override View GetView(int position, View convertView, ViewGroup parent)
    {
        var view = convertView ?? context.LayoutInflater.Inflate(Resource.Layout.ListViewLayout, parent, false);
        var lvtxtNome = view.FindViewById<TextView>(Resource.Id.txtvNome);
        lvtxtNome.Text = jogadores[position].Nome;
        return view;
    }
    public override Java.Lang.Object GetItem(int position)
    {
        return null;
    }
}

1 个答案:

答案 0 :(得分:1)

您需要做的是将Adapter类中的click事件设置为您想要可单击的图像。

在您的ListViewAdapter中找到图像按钮,如下所示:

var deleteButton = view.FindViewById<ImageButton>(Resource.Id.btnDeleteLv);

然后将click事件设置为类似以下内容:

deletebutton.Click+= HandleTouchEvent;

并添加如下所示的方法:

private void HandleTouchEvent(object sender, System.EventArgs e)
    {
     //On Click code 
    }