ListView中的SetOnClickListener

时间:2013-02-02 14:15:34

标签: android .net xamarin.android

早上好,我实现了一个listView,其中包含一些隐藏的信息和一个按钮,但是我遇到按钮的click事件有问题,我正在使用MonoDroid,我实现了接口{ {1}},我的问题是,如果我在OnClickListener ()适配器中设置button.setOnclickListener (new MyClassClick ()),每次滚动ListView时都会创建一个对象,并且无法重复使用以前创建的对象,可以检查按钮是否已经有与他相关联的动作?输入ListViewGetOnclickListener ()

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

        var item = produtos[position];
        ViewHolderProdutos viewHolder;
        var view = convertView;
        if (view == null)
        {
            view = context.LayoutInflater.Inflate(Resource.Layout.ProdutoListItem,parent, false);
            viewHolder = new ViewHolder();
            viewHolder.Inicializa(view);
            view.Tag = viewHolder;
        }
        ProductsBase prod = (ProductsBase) productList[position];
        viewHolder = (ViewHolder)view.Tag;
        viewHolder.Bind(prod);
        var btnIncluir = view.FindViewById<Button>(Resource.Id.btnIncluir);
        //Evento Click do Botão Incluir
        btnIncluir.SetOnClickListener(new OnClickButton(productList[position].pro_codigo.ToString(), parent.Context));
        return view;

}

对不起,如果文字混乱,我的英语不好

1 个答案:

答案 0 :(得分:0)

Adapter GetView方法中,您只需在OnClickListenerconvertView时设置null

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

    if (null == view)
    {
        //1. inflate or create your views here
        //2. Set OnClickListeners
    }

    //populate views with Adapter item data
}