单击事件不在列表视图项中的按钮上工作

时间:2015-01-02 13:43:53

标签: android listview android-custom-view custom-lists

我使用带有Listview的自定义Custom views作为列表元素,每个列表项都是单独的Custom view。我面临的问题是,当我们点击它时,ListView第一项中的按钮上的事件没有被触发。点击它后,如果我们点击屏幕上的其他位置,点击事件就会触发。我无法找到它的解决方案,我正在努力解决它。任何帮助将受到高度赞赏。

更新了代码:这是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返回CustomView。第一项将是英雄布局,之后所有Customviews都将添加到Convertview。点击事件对英雄后的第一个项目无效。

以我的回答更新: 我没有将Customview直接分配到Convertview,而是将FrameLayout夸大,并将Customview添加到FrameLayout。现在我没有点击问题。

3 个答案:

答案 0 :(得分:1)

试试这个会起作用

       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;
    }

   }

答案 1 :(得分:1)

 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

            }


        }
    }



   }

答案 2 :(得分:-1)

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