简体   繁体   中英

Xamarin Android Click Button in ListView And Get Item Position

I want to make a click button on OnCreateView "Fragment"

the first class is HomeScreenAdapter : BaseAdapter

is a simple Adapter retrun a view

HomeScreenAdapter

public class HomeScreenAdapter : BaseAdapter<Quotes>
{
    List<Quotes> items;
    Activity context;
    public HomeScreenAdapter(Activity context, List<Quotes> items)
        : base()
    {
        this.context = context;
        this.items = items;
    }
    public override long GetItemId(int position)
    {
        return position;
    }
    public override Quotes this[int position]
    {
        get { return items[position]; }
    }
    public override int Count
    {
        get { return items.Count; }
    }

    public override View GetView(int position, View convertView, ViewGroup parent)
    {
        var item = items[position];
        View view = convertView;
        if (view == null) // no view to re-use, create new
            view = context.LayoutInflater.Inflate(Resource.Layout.Quotes, null);
        view.FindViewById<TextView>(Resource.Id.Quotes).Text = item.s_Quotes;
        view.FindViewById<TextView>(Resource.Id.From).Text = item.s_From;
        view.FindViewById<TextView>(Resource.Id.Category).Text = item.Category;

        return view;
    }
}

Fragment

class CrimeFragments : Fragment
{
    ListView Lv;
    List<Quotes> tableItems = new List<Quotes>();
    static Quotes quotes;
    ClipboardManager Cbm;
    ClipData Cd;
    ImageView imageView;
    public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {

        View rootView = inflater.Inflate(Resource.Layout.crimelayout, container, false);
        Lv = rootView.FindViewById<ListView>(Resource.Id.crimeListView);
        Lv.VerticalScrollBarEnabled = false;
        Lv.Adapter = new HomeScreenAdapter(this.Activity, tableItems);
        Lv.ItemClick += Lv_ItemClick;
        return rootView;
    }

    private void Lv_ItemClick(object sender, AdapterView.ItemClickEventArgs e)
    {
        var t = tableItems[e.Position];
        string cpy = t.s_Quotes + " - " + t.s_From;
        Cbm = (ClipboardManager)Context.GetSystemService(Context.ClipboardService);
        Cd = ClipData.NewPlainText("text", cpy);
        Cbm.PrimaryClip = Cd;
        Toast.MakeText(this.Activity, "Text Copied", ToastLength.Short).Show();
    }
}

I Want to make this button click not Lv.ItemClick

AXML

I want make thies buttons independent from Listview

Any One Can Help Me Please ?

Hi Developers I found a solution this worked for me :

  • If You've a Adapter class change the GetView Function to this :

      public override View GetView(int position, View convertView, ViewGroup parent) { var item = items[position]; View view = convertView; if (view == null) { // no view to re-use, create new view = context.LayoutInflater.Inflate(Resource.Layout.Quotes, null); } view.FindViewById<TextView>(Resource.Id.Quotes).Text = item.s_Quotes; view.FindViewById<TextView>(Resource.Id.From).Text = item.s_From; view.FindViewById<TextView>(Resource.Id.Category).Text = item.Category; Button btnCopy = view.FindViewById<Button>(Resource.Id.btnCopy); btnCopy.Click += delegate { Toast.MakeText(this.context, items[position].s_From, ToastLength.Short).Show(); }; return view; } 

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM