简体   繁体   English

单击事件不适用于列表视图项中的按钮

[英]Click event not working on button in listview item

I am using a custom Listview with Custom views as list elements, each list item is a separate Custom view . 我使用自定义Listview并将Custom views作为列表元素,每个列表项都是一个单独的Custom view The problem which I am facing is , click event on button in 1st item of ListView is not getting fired when we click on it. 我面临的问题是,单击ListView的第一项中的按钮上的click事件不会被触发。 After clicking on it if we click somewhere else in the screen the click event get fires. 单击它后,如果我们单击屏幕上的其他位置,则会触发点击事件。 I am not able to find the solutions for it and I am struggling with it. 我找不到它的解决方案,并且正在为此而苦苦挣扎。 Any help will be highly appreciated. 任何帮助将不胜感激。

Updated with code: Here is the getview method 使用代码更新:这是getview方法

public override View GetView(int position, View convertView, ViewGroup parent)
    {
        if (position == 0)
        {
            convertView = this.context.LayoutInflater.Inflate(Resource.Layout.home_hero_container, null);
            this.heroSection = convertView.FindViewById<FrameLayout>(Resource.Id.heroContainer);
            this.setHeroCard();
        }
        else
        {
            convertView = (View)GetItem(position - 1);
        }
        return convertView;
    }

GetItem returns CustomView. GetItem返回CustomView。 1st item will be the hero layout, after that all the Customviews will be added to Convertview . 第一项是英雄布局,之后所有Customviews将添加到Convertview Click event is not working on the 1st item after hero. 单击事件不适用于英雄之后的第一个项目。

Update with my answer: Instead of assigning Customview directly on to Convertview I inflated a FrameLayout and add Customview to FrameLayout . 更新我的回答:不是指定的Customview直接在Convertview我膨胀的FrameLayout ,并添加CustomviewFrameLayout Now I don't have click issue. 现在,我没有点击问题。

try this it will work 试试这个会起作用

       public class ContactsAdapter extends BaseAdapter {

ArrayList<ContactInfo> mlist;
Context mcontext;


 public BluetoothChatadpter(Context context,ArrayList<ChatInfo> mchtlist) {      
    mlist =  mchtlist;
    mcontext = context;

}

@Override
public int getCount() {
    return mlist.size();
}

@Override
public Object getItem(int postion) {
    return mlist.get(postion);
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
    public View getView(int position, View convertview, ViewGroup viewgroup){
        View view = null;
        if(convertview == null){
            LayoutInflater inflater = context.getLayoutInflater();
            view = inflater.inflate(R.layout.contactrow, null);

            ContactHolder holder = new ContactHolder();

            holder.txtviewfirstname = (TextView)view.findViewById(R.id.firstname);
            holder.txtviewphone = (TextView)view.findViewById(R.id.phone);
            holder.chkselected = (CheckBox)view.findViewById(R.id.check);

            setOnClickListener(new OnClickListener() {
    @Override
        public void onClick(View arg0) {
        // to open the selected file in resp

              // do your work here
             }});


chkselected .setOnClickListener(new OnClickListener() {
    @Override
public void onClick(View v) {
// Toast.makeText(context,// "checked is clicke="+pos, 12).show();
        if (chkselected.isChecked())          
                   {            

                    // do your work here
        } else {

 // do your work here                               
        }
    }
});



        view.setTag(holder);

    }
        else{
            view = convertview;
        }
        ContactHolder holder2 = (ContactHolder) view.getTag();
        holder2.txtviewfirstname.setText(list.get(position).firstname);
        holder2.txtviewphone.setText(list.get(position).phonenumber);
        holder2.chkselected.setChecked(list.get(position).selected);
        return view;
    }

   }
 public class ContactsAdapter extends BaseAdapter {

ArrayList<ContactInfo> mlist;
Context mcontext;


 public BluetoothChatadpter(Context context,ArrayList<ChatInfo> mchtlist) {      
    mlist =  mchtlist;
    mcontext = context;

}

@Override
public int getCount() {
    return mlist.size();
}

@Override
public Object getItem(int postion) {
    return mlist.get(postion);
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
    public View getView(int position, View convertview, ViewGroup viewgroup){
        View view = null;
        if(convertview == null){
            LayoutInflater inflater = context.getLayoutInflater();
            view = inflater.inflate(R.layout.contactrow, null);

            ContactHolder holder = new ContactHolder();

            holder.txtviewfirstname = (TextView)view.findViewById(R.id.firstname);
            holder.txtviewphone = (TextView)view.findViewById(R.id.phone);
            holder.chkselected = (CheckBox)view.findViewById(R.id.check);

            holder.chkselected.setOnCheckChangeListener(new CheckchangeListener() );



        view.setTag(holder);

    }
        else{
            view = convertview;
        }
        ContactHolder holder2 = (ContactHolder) view.getTag();
        holder2.txtviewfirstname.setText(list.get(position).firstname);
        holder2.txtviewphone.setText(list.get(position).phonenumber);
        holder2.chkselected.setChecked(list.get(position).selected);
        return view;
    }

    class CheckchangeListener implements OnCheckedChangeListener {


        public CheckchangeListener() {
            // TODO Auto-generated constructor stub



        }

        @Override
        public void onCheckedChanged(CompoundButton buttonView,
                boolean isChecked) {
            // TODO Auto-generated method stub
            if (isChecked) {
                 // do your work here

            } else {
                 // do your work here

            }


        }
    }



   }

您可以尝试在自定义适配器中设置onClick事件,如果有时间,请查看本教程以供参考-http://androidforbeginners.blogspot.it/2010/03/clicking-buttons-in-listview-row.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM