Android - 转动开关"开"在ListView项目上单击

时间:2018-05-19 08:43:25

标签: c# android visual-studio listview xamarin

我的C# Android应用中有自定义列表视图,每行包含textviewImageViewswitch。单击Listview项时,我想打开行的项目。

MainActivity:

List<TableList> list = = new List<TableList>();
list.Add(new TableList("Germany"));
list.Add(new TableList("France"));
list.Add(new TableList("Finland"));
listView.ItemClick += delegate (object sender, AdapterView.ItemClickEventArgs e)
        {
            string selected = t.Name;
            if (selected == "France")
            {
                 // Turn the proper switch for France row ON
            }
        };

Listview的ListAdapter和ListClass:

public class ListAdapter : BaseAdapter<TableList>
{
    List<TableList> items;
    Activity context;
    public ListAdapter(Activity context, List<TableList> items)
        : base()
    {
        this.context = context;
        this.items = items;
    }
    public override long GetItemId(int position)
    {
        return position;
    }
    public override TableList 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.CoinList, null);
        view.FindViewById<TextView>(Resource.Id.CoinName).Text = item.Name;
         view.FindViewById<ImageView>(Resource.Id.imageView1).SetImageResource(Resource.Drawable.n);

        If item is clicked set it on
        {
            view.FindViewById<Switch>(Resource.Id.switch).SetOn 
        }
        else
        {
            view.FindViewById<Switch>(Resource.Id.switch).SetOf
        }
        return view;
    }
}
public class TableList
{
    public string Name;
    public TableList(string Name)
    {
        this.Name = Name;
    }
}

我不知道应该在哪里设置开关(在listView.ItemClick事件或ListAdapter中)并且我不知道如何将其设置为开启。请帮我这样做。

1 个答案:

答案 0 :(得分:1)

Here是我的演示。

您可以选择一个来实现目标。我将通过ItemClick事件向您展示如何执行此操作:

  

单击Listview项目时,我想打开行的项目。

因为,Switch会抓住ViewGroup的焦点。因此,我从Switch中的MyAdapter删除了焦点:

holder.ms.Focusable = false;//ms is Switch

现在,这是我的ItemClick事件(单击项目时打开开关):

private void MListView_ItemClick(object sender, AdapterView.ItemClickEventArgs e)
{
        var ll = e.View as LinearLayout;
        var sw = ll.GetChildAt(1) as Switch;
        if (sw.Checked)
        {
            sw.Checked = false;
            adapter.changeState((int)sw.Tag,false);
        }
        else
        {
            sw.Checked = true;
            adapter.changeState((int)sw.Tag, true);
        }
}

众所周知,ListView有重用问题,因此,我添加了一个bool属性来控制Switch的状态:

public class MyData:Java.Lang.Object {
    public MyData(string p,bool b) {
        this.position = p;
        this.isCheck = b;
    }
    public string position { get; set; }
    public bool isCheck { get; set; }
}

以下是changeState方法:

internal void changeState(int position, bool v)
{
    mitems[position].isCheck = v;
    this.NotifyDataSetChanged();
}

这是CheckedChange事件:

private void Ms_CheckedChange(object sender, CompoundButton.CheckedChangeEventArgs e)
{
    var sm = sender as Switch;
    Log.Error("Ms_CheckedChange", (int)sm.Tag+"");
    if (e.IsChecked&&!mitems[(int)sm.Tag].isCheck)
    {
        mitems[(int)sm.Tag].isCheck = true;
        this.NotifyDataSetChanged();
    }
    else if(!e.IsChecked&& mitems[(int)sm.Tag].isCheck)
    {
        mitems[(int)sm.Tag].isCheck = false;
        this.NotifyDataSetChanged();
    }

}