Click事件不工作的按钮,在列表视图项 [英] Click event not working on button in listview item

查看:181
本文介绍了Click事件不工作的按钮,在列表视图项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是自定义的列表视图自定义视图作为列表元素,每个项目都是一个单独的自定义视图。我现在面临的问题是,点击ListView中的第一个项目按钮事件,当我们点击它是不是被解雇。点击它后,如果我们点击其他地方在屏幕上点击事件得到激发。我无法找到它的解决方案,我用它挣扎。任何帮助将是非常美联社preciated。

更新,其中code:这里是getview方法

 公众覆盖查看GetView(INT位置,查看convertView,父母的ViewGroup)
    {
        如果(位置== 0)
        {
            convertView = this.context.LayoutInflater.Inflate(Resource.Layout.home_hero_container,NULL);
            this.heroSection = convertView.FindViewById<&的FrameLayout GT;(Resource.Id.heroContainer);
            this.setHeroCard();
        }
        其他
        {
            convertView =(查看)的GetItem(位置 - 1);
        }
        返回convertView;
    }

的GetItem 返回CustomView。第一个项目将是英雄的布局,所有的后Customviews 将被添加到 Convertview 。 Click事件不工作的第一个项目后,英雄

更新我的回答:
而不是指定的 Customview 直接到 Convertview 我膨胀的的FrameLayout 添加 Customview 的FrameLayout 。现在,我没有点击的问题。


解决方案

 公共类ContactsAdapter延伸BaseAdapter {ArrayList的<&的ContactInfo GT; mlist;
上下文mcontext;
 公共BluetoothChatadpter(上下文的背景下,ArrayList的< ChatInfo> mchtlist){
    mlist = mchtlist;
    mcontext =背景;}@覆盖
公众诠释的getCount(){
    返回mlist.size();
}@覆盖
公共对象的getItem(INT位置){
    返回mlist.get(位置);
}@覆盖
众长getItemId(INT位置){
    返回的位置;
}@覆盖
    公共查看getView(INT位置,查看convertview,ViewGroup中的ViewGroup){
        查看查看= NULL;
        如果(convertview == NULL){
            LayoutInflater吹气= context.getLayoutInflater();
            鉴于= inflater.inflate(R.layout.contactrow,NULL);            ContactHolder持有人=新ContactHolder();            holder.txtviewfirstname =(TextView中)view.findViewById(R.id.firstname);
            holder.txtviewphone =(TextView中)view.findViewById(R.id.phone);
            holder.chkselected =(复选框)view.findViewById(R.id.check);            holder.chkselected.setOnCheckChangeListener(新CheckchangeListener());        view.setTag(保持器);    }
        其他{
            鉴于= convertview;
        }
        ContactHolder holder2 =(ContactHolder)view.getTag();
        holder2.txtviewfirstname.setText(list.get(位置).firstname);
        holder2.txtviewphone.setText(list.get(位置).phonenumber);
        holder2.chkselected.setChecked(list.get(位置).selected);
        返回视图。
    }    类CheckchangeListener实现OnCheckedChangeListener {
        公共CheckchangeListener(){
            // TODO自动生成构造函数存根        }        @覆盖
        公共无效onCheckedChanged(CompoundButton buttonView,
                布尔器isChecked){
            // TODO自动生成方法存根
            如果(器isChecked){
                 //做你的工作在这里            }其他{
                 //做你的工作在这里            }
        }
    }   }

I am using a custom Listview with Custom views as list elements, each list item is a separate 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. 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

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. 1st item will be the hero layout, after that all the Customviews will be added to 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. Now I don't have click issue.

解决方案

 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

            }


        }
    }



   }

这篇关于Click事件不工作的按钮,在列表视图项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆