How can i have a image inside a listview and when is clicked do something? I can use a image button but the the image will not fill the button. The image is a trash icon, that when clicked will have to delete the data that is associated to that listview cell.
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;
}
}
What you need to do is set a click event in your Adapter class to an image that you want to be clickable.
In your ListViewAdapter
find the image button like this:
var deleteButton = view.FindViewById<ImageButton>(Resource.Id.btnDeleteLv);
Then set a click event to the same something like this:
deletebutton.Click+= HandleTouchEvent;
And add its method like follows:
private void HandleTouchEvent(object sender, System.EventArgs e)
{
//On Click code
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.