【问题标题】:Clickable image inside listview列表视图内的可点击图像
【发布时间】:2018-11-25 14:44:18
【问题描述】:

如何在列表视图中放置图像以及单击时执行某些操作?我可以使用图像按钮,但图像不会填充按钮。 该图像是一个垃圾桶图标,单击该图标时必须删除与该列表视图单元格关联的数据。

列表视图布局:

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

【问题讨论】:

    标签: c# listview xamarin xamarin.android


    【解决方案1】:

    您需要做的是在您的 Adapter 类中设置一个点击事件到您希望可点击的图像。

    在您的ListViewAdapter 中找到这样的图片按钮:

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

    然后将点击事件设置为类似这样:

    deletebutton.Click+= HandleTouchEvent;
    

    并添加它的方法如下:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-10-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-13
      • 1970-01-01
      • 2012-03-23
      • 2017-02-01
      • 1970-01-01
      相关资源
      最近更新 更多