具有更多ListView的AdapterView.OnItemClickListener

时间:2013-07-25 10:27:50

标签: android android-listview onclicklistener android-adapterview

我在单个片段上有2个ListView,我想知道是否可以为实现AdapterView.OnItemClickListener的同一个类设置。

我的意思是,Android文档说:

public abstract void onItemClick (AdapterView<?> parent, View view, int position, long id)

Added in API level 1
Callback method to be invoked when an item in this AdapterView has been clicked.

Implementers can call getItemAtPosition(position) if they need to access the data associated with the selected item.

Parameters
parent  The AdapterView where the click happened.
view    The view within the AdapterView that was clicked (this will be a view provided by the adapter)
position    The position of the view in the adapter.
id  The row id of the item that was clicked.

所以我想我可以选择不同的ListView,它们通过Parent调用onClick(发现点击的AdapterView)..

现在我该如何识别ListView?有没有办法解决/案件?或者我需要创建实现onItemClickListener的不同类(也可以是匿名的)并设置为不同的ListView不同的AdapterView.onItemClickListener?

1 个答案:

答案 0 :(得分:13)

好的我已经解决了:

private class myClass implements AdapterView.OnItemClickListener {

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position,
            long id) {
        // TODO Auto-generated method stub

        switch(parent.getId()) {
        case R.id.listview1:            
            break;
        case R.id.listview2:
            break;
        }
    }
}