0

In order to handle different view clicking, I set listeners for views in getView(). If a lot of items added, it will be a little slow.

    public View getView(int position, View convertView, ViewGroup parent) {
            holder.icon_iv = (ImageView) convertView.findViewById(R.id.app_icon_iv);
            holder.icon_iv.setOnClickListener(new OnClickListener() {
                public void onClick(View v) {
                    ...
                }
            });

            holder.checkbox_iv = (CheckBox) convertView.findViewById(R.id.history_checkbox_iv);
            holder.checkbox_iv.setOnClickListener(new OnClickListener() {
                public void onClick(View v) {
                    ...
                }
            });
    }

So I wanna do it in onItemClick(), but View arg1 can't tell the difference on views clicking. Can anyone tell me what to do with it?

public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {

}

EDIT: Thank you, I decide do like this and it works perfectly:

        holder.item_ll.setTag(position);
        holder.item_ll.setOnClickListener(mItemClickListener);
        holder.checkbox_iv.setTag(position);
        holder.checkbox_iv.setOnClickListener(mItemClickListener);

public View.OnClickListener mItemClickListener = new View.OnClickListener() {
    public void onClick(View view) {
        if (view.getTag() instanceof Integer) {
            int position = (Integer) view.getTag();
            HistoryItem item = adapter.get(position);
            if (view.getId() == R.id.checkbox_iv) {
                ...
            } else if (view.getId() == R.id.item_ll) {
                ...
            }
        }
    }
};
4

4 回答 4

4

使用一个OnClickListener来处理一种事件

示例代码

在你的里面Adapter

public interface CustomListListener
{
    void onEventA(index int);
    void onEventB(index int);
}
public View.OnClickListener mButtonATypeClickListener = new View.OnClickListener(){
    public void onClick(View view)
    {
        int position = (Integer) view.getTag();
        mListener.onEventA(position);
    }
}
public View.OnClickListener mButtonBTypeClickListener = new View.OnClickListener(){
    public void onClick(View view)
    {
        int position = (Integer) view.getTag();
        mListener.onEventB(position);
    }
}

public View getView(int position, View convertView, ViewGroup parent) {
   ...
   buttonA.setTag(position);
   buttonA.setOnClickListener(mButtonATypeClickListener);
   buttonB.setTag(position);
   buttonB.setOnClickListener(mButtonBTypeClickListener);
   ...
}

现在你所要做的就是CustomListListener在你的Activity.

于 2013-09-03T10:55:33.217 回答
3

您可以使用 getId() 函数

 public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {

if(arg1.getId()==R.id.app_icon_iv){
             //Do something for that view
}
else{ 
             // Do something for another view
}

}
于 2013-09-03T10:42:34.480 回答
1

我认为您应该使用标签:您只需通过 setTag() 方法给每个视图一个标签,然后在 onClick 方法中检查已单击视图的标签

于 2013-09-03T10:41:06.757 回答
1

如果您对列表项的每个子项都有共同执行,那么您必须使用onItemClick()侦听器,否则每个子项都有自己的点击侦听器。另一件事是,当使用CheckBoxRadioButton时,最好使用它们的检查更改侦听器而不是单击侦听器。

于 2013-09-03T10:42:31.707 回答