Xamarin自定义多选列表视图

时间:2017-08-13 11:36:52

标签: c# android listview xamarin xamarin.android

我想知道是否有办法制作实际上能够返回所选索引的多选列表视图。我已经能够使用预先制作的multiplechoicelistview适配器,但我需要能够编辑它的样式。所以我需要一个自定义列表视图。 这是我的创建代码

 protected override void OnCreate(Bundle savedInstanceState)
    {

        base.OnCreate(savedInstanceState);
        SetContentView(Resource.Layout.Options);
        outList = FindViewById<ListView>(Resource.Id.outList);
        var btnCheck = FindViewById<ImageButton>(Resource.Id.btnConfirm);
        var btnBack = FindViewById<ImageButton>(Resource.Id.btnBack);
        for (int i = 0; i < NewProfileVars.LifeStyles.Length; i++)
        {
            inList.Add(NewProfileVars.LifeStyles[i].Name);
        }


        //list contents end here

        ListViewAdapter adapter = new ListViewAdapter(this, inList);
        outList.Adapter = adapter;
        outList.ChoiceMode = ChoiceMode.Multiple;
        NewProfile main = new NewProfile();
        btnCheck.Click += Confirm;
        btnBack.Click += Back;


    }

这是我的列表视图适配器代码

    class ListViewAdapter: BaseAdapter<string>
    {
        public List<string> Items;
        public Context Context;

        public ListViewAdapter(Context context, List<string> items)
        {
            Items = items;
            Context = context;
        }


        public override int Count
        {
            get { return Items.Count; }
        }

        public override long GetItemId(int position)
        {
            return position;

        }
        public override string this[int position]
        {
            get { return Items[position]; }
        }

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

            if (row == null)
            {
                row = LayoutInflater.From(Context).Inflate(Resource.Layout.ListBox, null, false);

            }
            CheckBox txtName = row.FindViewById<CheckBox>(Resource.Id.cbName);
            txtName.Text = Items[position];
            return row;
        }
    }

我现在需要的是弄清楚确认按钮如何保存我选择的内容。 谢谢你的帮助。

1 个答案:

答案 0 :(得分:5)

我发现你在ListView中使用CheckBox。您可以使用以下内容获取Checked所在的项目:

首先创建一个将保存项目数据和Checked状态的类,例如我们称之为

public class LifeStylesListItem
{
    public string Name { get; set; }

    public bool IsSelected { get; set; }

    public LifeStylesListItem(string name)
    {
        Name = name;
    }
}

然后修改您的ListViewAdapter

添加一个包含LifeStylesListItem列表的新私有字段

private List<LifeStylesListItem> _list;

使用构造函数中传递的Items初始化列表。

public ListViewAdapter(Context context, List<string> items)
{
    Items = items;
    _list = new List<LifeStylesListItem>();

    //Your are creating a copy of your Items
    foreach (var item in items)
    {
        _list.Add(new LifeStylesListItem(item));
    }

    Context = context;
}

GetView方法中订阅CheckBox的CheckedChange事件。这样,当检查状态发生变化时,您将收到通知。您还需要根据Item IsSelected值设置Checked属性。当ListView将重用您的单元格时,这是必需的。

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

    if (row == null)
    {
        row = LayoutInflater.From(Context).Inflate(Resource.Layout.ListBox, null, false);

    }
    CheckBox txtName = row.FindViewById<CheckBox>(Resource.Id.cbName);
    txtName.Text = _list[position].Name;
    txtName.Checked = _list[position].IsSelected;

    txtName.CheckedChange -= TxtName_CheckedChange;
    txtName.CheckedChange += TxtName_CheckedChange;

    return row;
}

添加事件处理程序TxtName_CheckedChange方法

void TxtName_CheckedChange(object sender, CompoundButton.CheckedChangeEventArgs e)
{
    //These lines are used to get the position of the control that was clicked
    var obj = sender as CheckBox;
    var row = obj?.Parent as View; 
    var parent = row?.Parent as ListView;

    if (parent == null)
    {
        return;
    }

    var position = parent.GetPositionForView(row);

    // Once you have the position you can get the item and change
    // its IsSelected
    var item = _list[position];
    item.IsSelected = e.IsChecked;
}

然后在Adapter中添加的最后一个方法是返回所选Items的方法。在Linq的帮助下(需要添加using System.Linq),您可以查询所选项目。

public List<string> GetCheckedItems()
{
    return _list
            .Where(a => a.IsSelected)
            .Select(b => b.Name)
            .ToList();
}

现在,在您的活动中,您只需在确认按钮上点击ListViewAdapter方法调用GetCheckedItems

private void Confirm(object sender, EventArgs e)
{
    var checkedItems = adapter.GetCheckedItems();
}

请务必将adapter更改为您的活动中的私人字段

private ListViewAdapter adapter;

希望这会有所帮助.-