获取引发侦听器的对象

时间:2014-08-22 15:47:00

标签: java android listener

我把这个监听器放在几个swipelistview

BaseSwipeListViewListener myBaseSwipeListViewListener = new BaseSwipeListViewListener() {
            @Override
            public void onOpened(int position, boolean toRight) {
            //Here I need the calling swipelistview
            }
}

swipelistview0.setSwipeListViewListener(myBaseSwipeListViewListener);
swipelistview1.setSwipeListViewListener(myBaseSwipeListViewListener);
swipelistview2.setSwipeListViewListener(myBaseSwipeListViewListener); //and so on

我怎样才能获得调用swipelistview?

1 个答案:

答案 0 :(得分:1)

嗯,我不确定我是否得到了你,但是只是在你的监听器回调方法中添加另一个参数呢?像这样:

BaseSwipeListViewListener myBaseSwipeListViewListener = new BaseSwipeListViewListener(){
    @Override
    public void onOpened(int position, boolean toRight, SwipeListView swipeListView) {
        //Here you now have your calling swipelistview
    } 
}

在你的BaseSwipeListViewListener中,现在只需通过返回自己(this)来添加第三个参数。

更新:我不知道它是一个库。这不是我要做的,但如果您不想编辑源代码,请执行以下操作:

BaseSwipeListViewListener myBaseSwipeListViewListener0 = new BaseSwipeListViewListener() {
    @Override
    public void onOpened(int position, boolean toRight) {
        onOpenedAdvanced(position, toRight, swipelistview0);
    } 
}

BaseSwipeListViewListener myBaseSwipeListViewListener1 = new BaseSwipeListViewListener() {
    @Override
    public void onOpened(int position, boolean toRight) {
        onOpenedAdvanced(position, toRight, swipelistview1);
    } 
}

BaseSwipeListViewListener myBaseSwipeListViewListener2 = new BaseSwipeListViewListener() {
    @Override
    public void onOpened(int position, boolean toRight) {
        onOpenedAdvanced(position, toRight, swipelistview0);
    } 
}

public void onOpenedAdvanced(int position, boolean toRight, SwipeListView swipeListView){
    //Here you now have your calling swipelistview
}

swipelistview0.setSwipeListViewListener(myBaseSwipeListViewListener0);
swipelistview1.setSwipeListViewListener(myBaseSwipeListViewListener1);
swipelistview2.setSwipeListViewListener(myBaseSwipeListViewListener2);