如何在类级别实现OnItemClickListener

时间:2017-01-24 13:44:29

标签: android

我有两个listview内部类,我不想为每个OnClickListener实现listview,而是希望该类实现OnClickListener并处理listview OnClicklistener在单个覆盖方法OnClickListener中。 任何人都可以提供帮助。

3 个答案:

答案 0 :(得分:4)

class ListViewContainer implements AdapterView.OnItemClickListener{
ListView listView1;
ListView listView2;
private void someMethod(){
    listView1.setOnItemClickListener(this);
    listView2.setOnItemClickListener(this);
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    if(parent == listView1){
        //Do Listview 1 things
    }else if(parent == listView2){
        //Do Listview 2 things
    }
}
}

答案 1 :(得分:0)

private AdapterView.OnItemClickListener getItemClickListener(){

    return new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

        }
    };
}

答案 2 :(得分:0)

private AdapterView.OnItemClickListener listener = new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

    }
};
...
listView1.setOnItemClickListener(listener);
listView2.setOnItemClickListener(listener);