为什么列表视图行项被多次点击?

时间:2017-11-11 14:34:39

标签: android listview xamarin xamarin.android

我已经在 Xamarin.Android 中实现了ListView,在行列表中有几个按钮,但是当我点击按钮时,它会被多次击中。我试过这种方式,但对我不起作用。 我在BaseAdapter<DA_ClassSectionAttendance>

中有这个方法
 public override View GetView(int position, View convertView, ViewGroup parent)
    {
        View view = convertView;

        // re-use an existing view, if one is available
        // otherwise create a new one
        if (view == null)
            view = context.LayoutInflater.Inflate(Resource.Layout.AttendanceCreateRowList, parent, false);
        var viewHolder = new ViewHolder(view);

        DA_ClassSectionAttendance attendance = this[position];

        view.Tag = viewHolder;
        viewHolder.ClassSection.Text = attendance.ClassName + "  " + attendance.SectionName;
        viewHolder.AttendanceEdit.Text = "Edit";
        viewHolder.AttendanceNew.Text = "New";
        viewHolder.AttendanceNew.Tag = viewHolder;
        viewHolder.AttendanceNew.Click += (object sender, EventArgs ex) =>
        {

            var obj = sender as Button;
            var listView = parent as ListView;
            //int objectPosition = (int)obj.Tag;
            var itemPosition = listView.GetPositionForView(obj);
            if (itemPosition != ListView.NoId )
            {

                CustomFragmentManager fragManager = new CustomFragmentManager();
                var fragment = new AttendanceCreateRecyclerFragment();
                Bundle args = new Bundle();
                var jsonData = JsonConvert.SerializeObject(this[itemPosition]).ToString();
                args.PutString(AttendanceCreateRecyclerFragment.DATA_RECEIVE, jsonData);
                fragment.Arguments = args;

                fragManager.ReplaceFragment(context, fragment);

            }

        };

        return view;
    }

和查看持有人如下

private class ViewHolder : Java.Lang.Object
    {
        public ViewHolder(View view)
        {
            ClassSection = view.FindViewById<TextView>(Resource.Id.class_section);
            AttendanceNew = view.FindViewById<Button>(Resource.Id.new_attendance);
            AttendanceEdit = view.FindViewById<Button>(Resource.Id.edit_attendance);
        }

        public TextView ClassSection { get; set; }
        public Button AttendanceNew { get; set; }
        public Button AttendanceEdit { get; set; }
    }

我想实现点击事件而不是点击监听器。如果任何示例代码以及解释将非常感激。谢谢。

1 个答案:

答案 0 :(得分:1)

原因是点击事件多次附加viewHolder.AttendanceNew.Click += (object sender, EventArgs ex) =>

您创建和使用视图持有者的方式无效。将您的代码更改为:

public override View GetView(int position, View convertView, ViewGroup parent)
{
    View view = convertView;

    // re-use an existing view, if one is available
    // otherwise create a new one
    if (view == null)
        view = context.LayoutInflater.Inflate(Resource.Layout.AttendanceCreateRowList, parent, false);

    var viewHolder = view.Tag as ViewHolder;

    if (viewHolder == null)
    {
        viewHolder = new ViewHolder(view);
        viewHolder.AttendanceNew.Click += (object sender, EventArgs ex) =>
        {
            var obj = sender as Button;
            var listView = parent as ListView;

            var itemPosition = listView.GetPositionForView(obj);

            if (itemPosition != ListView.NoId )
            {
                CustomFragmentManager fragManager = new CustomFragmentManager();
                var fragment = new AttendanceCreateRecyclerFragment();
                Bundle args = new Bundle();
                var jsonData = JsonConvert.SerializeObject(this[itemPosition]).ToString();
            args.PutString(AttendanceCreateRecyclerFragment.DATA_RECEIVE, jsonData);
               fragment.Arguments = args;

               fragManager.ReplaceFragment(context, fragment);
           }
        };
    }

    var attendance = this[position];

    viewHolder.ClassSection.Text = attendance.ClassName + "  " + attendance.SectionName;
    viewHolder.AttendanceEdit.Text = "Edit";
    viewHolder.AttendanceNew.Text = "New";

    view.Tag = viewHolder;

    return view;
}