xamarin自定义ListView在选择一个项目时不执行任何操作

时间:2016-05-03 15:24:28

标签: listview xamarin xamarin.android listviewitem

public class ListViewFaixaAdapter : BaseAdapter<Model> {
    LayoutInflater _inflater;
    List<Model> _faixas;

    public ListViewFaixaAdapter(LayoutInflater inflater) : base(){
        _faixas = new List<Model>();
        for(int i = 0; i < 15; i++) {
            Model faixa = new Model();
            faixa.descricao = "Descrição " + i;
            faixa.letra = Convert.ToChar('A' + i).ToString() ;
            _faixas.Add(faixa);
        }
        _inflater = inflater;
    }

    public override Model this[int position] {
        get {
            return _faixas[position];
        }
    }

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

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

    public override View GetView(int position, View linhaView, ViewGroup parent) {
        var item = _faixas[position];            
        linhaView = linhaView ?? _inflater.Inflate(Resource.Layout.AuditoriaFaixaLinha, null);
        linhaView.Selected = false;   
        /* when uncomment something itens are click event duplicate
        linhaView.Click += (sender, e) => {
            //linhaView.Selected = !linhaView.Selected;
 /*((TextView)linhaView.FindViewWithTag("lblAuditoriaFaixaNota")).Selected = linhaView.Selected;
            ((TextView)linhaView.FindViewWithTag("lblAuditoriaFaixaLabel")).Selected = linhaView.Selected;
            ((TextView)linhaView.FindViewWithTag("lblAuditoriaFaixaDescricao")).Selected = linhaView.Selected;               

        };*/
        return linhaView;
    }
}
// using adapter
var listView = view.FindViewById<ListView>(Resource.Id.Item);
listView.Adapter = new ListViewFaixaAdapter(inflater);
listView.ItemClick += (sender, e) => {
    // do show when I clicked
    Toast.MakeText(Activity, "test", ToastLength.Short).Show();
};

显示ListView后,当我点击一个项目时,只有一个项目触发了很多事件。我没有找到在ListView上显示自定义xamarin实现的完整示例。

1 个答案:

答案 0 :(得分:1)

如果您的视图已存在,则不应为其分配其他事件处理程序

public override View GetView(int position, View linhaView, ViewGroup parent) {

    var item = _faixas[position];  

    if (linhaView == null) {
      linhaView = _inflater.Inflate(Resource.Layout.AuditoriaFaixaLinha, null);

      linhaView.Click += (sender, e) => {
        // handler code goes here...
      }
    }

    linhaView.Selected = false;   

    return linhaView;
}