Xamarin自定义多选列表视图

Lia*_*ack 2 c# android listview xamarin.android xamarin

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

 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;


    }
Run Code Online (Sandbox Code Playgroud)

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

    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;
        }
    }
Run Code Online (Sandbox Code Playgroud)

我现在需要的是弄清楚确认按钮如何保存我选择的东西.谢谢你先进的帮助.

pin*_*dax 5

我看到你CheckBox在ListView 中使用了.你可以得到Checked使用这样的东西的项目:

首先创建一个将保存项目数据和Checked状态的类,例如,让我们调用它

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

    public bool IsSelected { get; set; }

    public LifeStylesListItem(string name)
    {
        Name = name;
    }
}
Run Code Online (Sandbox Code Playgroud)

然后修改你的 ListViewAdapter

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

private List<LifeStylesListItem> _list;
Run Code Online (Sandbox Code Playgroud)

使用构造函数中传递的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;
}
Run Code Online (Sandbox Code Playgroud)

GetView方法中订阅CheckedChangeCheckBox 的事件.这样,当检查状态发生变化时,您将收到通知.您还需要根据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;
}
Run Code Online (Sandbox Code Playgroud)

添加事件处理程序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;
}
Run Code Online (Sandbox Code Playgroud)

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

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

现在在您的Activity中,您只需要GetCheckedItems在Confirm按钮上调用ListViewAdapter方法,然后单击:

private void Confirm(object sender, EventArgs e)
{
    var checkedItems = adapter.GetCheckedItems();
}
Run Code Online (Sandbox Code Playgroud)

请记住adapter在Activity中更改为私有字段

private ListViewAdapter adapter;
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助.-