ListView中的Xamarin Android单击按钮并获取项目位置

时间:2018-04-08 19:08:42

标签: c# android xamarin

我想在OnCreateView“Fragment”上点击一下按钮

第一个类是 HomeScreenAdapter:BaseAdapter

是一个简单的适配器后退视图

  

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

片段

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

我想点击此按钮而不是Lv.ItemClick

AXML

我希望make theies按钮独立于Listview

任何人都可以帮助我吗?

1 个答案:

答案 0 :(得分:2)

嗨开发者我找到了一个对我有用的解决方案:

  • 如果您有适配器类,请将 GetView 功能更改为:

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