如何从 Android 中的匿名 class 中引用接口?

How to reffer to an interface from within an anonymous class in Android?

我有以下 class:

public class MyFragment extends Fragment implements Observer<List<User>>

searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
    @Override
    public boolean onQueryTextSubmit(String s) {return false;}

    @Override
    public boolean onQueryTextChange(String searchText) {
        if (searchText.length() > 0) {
            myLiveData.removeObservers(getViewLifecycleOwner(), this);
        }

        return false;
    }
});

我需要将 LifecycleOwner 对象作为第一个参数(有效)和 Oberver 对象的第二个参数传递给 removeObservers() 函数。当使用上面的代码行时,它失败了,因为第二个 this 而不是 引用在片段中实现的接口。如何让this指向正确的接口并得到对应的Oberver对象?

MyFragment.this 就是您要找的