5

我在一个片段上有 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 调用 onClick by Parent (发生点击的AdapterView)..

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

4

1 回答 1

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;
        }
    }
}
于 2013-07-25T10:47:56.970 回答