具有更多ListView的AdapterView.OnItemClickListener

Tiz*_*ica 5 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.
Run Code Online (Sandbox Code Playgroud)

所以我想我可以选择不同的ListView调用onClick by Parent(AdapterView,其中点击发生)..

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

Tiz*_*ica 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;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)