目前我正在试验 viewmodels 并且想知道将 viewmodel 传递给 recyclerview 适配器是否会导致内存泄漏?适配器中视图模型的唯一目的是提供要在活动中显示的图像的新 url
我不知道接口是更好的方法还是有更好的方法从 recyclerview 接收 onclick 事件?
这是我的代码:
视图模型:
public class WallDetailViewModel extends AndroidViewModel {
private final String apiAddress = "*";
private BlindWall detailWall;
private MutableLiveData<String> mainImageUrl;
public WallDetailViewModel(@NonNull Application application) {
super(application);
}
public LiveData<String> getMainImageUrl() {
if(mainImageUrl == null) {
mainImageUrl = new MutableLiveData<>();
mainImageUrl.postValue(getImageUrl(0));
}
return mainImageUrl;
}
public void setWall(BlindWall wall) {
detailWall = wall;
}
public String getImageUrl(int position) {
String returnValue = null;
if(position >= 0 && position …Run Code Online (Sandbox Code Playgroud)