19

我正在使用 Android 支持库 26.1.0。这些是 app 模块中的依赖项 -

implementation "android.arch.lifecycle:runtime:1.0.0"
implementation "android.arch.lifecycle:extensions:1.0.0-beta1"
implementation "android.arch.persistence.room:rxjava2:1.0.0-beta1"
implementation "android.arch.lifecycle:common-java8:1.0.0-beta1"
annotationProcessor "android.arch.lifecycle:compiler:1.0.0-beta1"

但是当我尝试像下面这样使用 ViewModel 时 -

mUserViewModel.getUsers().observe(this,
            (Observer<Resource<List<UserView>>>) listResource -> {
                if(listResource != null){
                    this.handleDataState(listResource.status, listResource.data, listResource.message);
                }
            });

它显示错误,this错误消息是Wrong first argument type Found: packagename.BrowseActivity, required: android.arch.lifecycle.LifecycleOwner。(虽然支持库版本是 26.1+ 已经实现了 LifecycleOwner)

我也尝试实现LifecycleRegistryOwner我在这个示例中找到的,这也不起作用。请提出解决方案,如果我做错了什么,请告诉我。

编辑 1 - 这是 Activity 类

import android.arch.lifecycle.Observer;
import android.arch.lifecycle.ViewModelProviders;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ProgressBar;

import com.github.amitkma.boilerplate.app.R;
import com.github.amitkma.boilerplate.app.mapper.UserMapper;
import com.github.amitkma.boilerplate.app.model.UserModel;
import com.github.amitkma.boilerplate.app.ui.widget.EmptyView;
import com.github.amitkma.boilerplate.app.ui.widget.ErrorView;
import com.github.amitkma.boilerplate.presentation.data.Resource;
import com.github.amitkma.boilerplate.presentation.data.ResourceState;
import com.github.amitkma.boilerplate.presentation.factory.ViewModelFactory;
import com.github.amitkma.boilerplate.presentation.viewmodel.UserViewModel;
import com.github.amitkma.boilerplate.presentation.vo.UserView;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import dagger.android.AndroidInjection;

public class BrowseActivity extends AppCompatActivity {

    @Inject
    UserAdapter mUserAdapter;
    @Inject
    UserMapper mUserMapper;
    @Inject
    ViewModelFactory mViewModelFactory;
    private UserViewModel mUserViewModel;

    private RecyclerView mRecyclerView;
    private ProgressBar mProgressBar;
    private ErrorView mErrorView;
    private EmptyView mEmptyView;


   @Override
   protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       AndroidInjection.inject(this);
       setContentView(R.layout.activity_browse);
       mUserViewModel = ViewModelProviders.of(this, mViewModelFactory)
            .get(UserViewModel.class);

       mRecyclerView = findViewById(R.id.recycler_browse);
       mProgressBar = findViewById(R.id.progress);
       mEmptyView = findViewById(R.id.view_empty);
       mErrorView = findViewById(R.id.view_error);

       setupBrowseRecycler();
       setupViewListeners();
   }

    @Override
    protected void onStart() {
        super.onStart();
        mUserViewModel.getUsers().observe(this,
            (Observer<Resource<List<UserView>>>) listResource -> {
                if(listResource != null){
                    this.handleDataState(listResource.status, listResource.data, listResource.message);
                }
            });
    }
    ....
}

这是 ViewModel 类

import android.arch.lifecycle.LiveData;
import android.arch.lifecycle.MutableLiveData;
import android.arch.lifecycle.ViewModel;

import com.github.amitkma.boilerplate.domain.interactor.GetUserList;
import com.github.amitkma.boilerplate.domain.model.User;
import com.github.amitkma.boilerplate.presentation.data.Resource;
import com.github.amitkma.boilerplate.presentation.data.ResourceState;
import com.github.amitkma.boilerplate.presentation.mapper.UserMapper;
import com.github.amitkma.boilerplate.presentation.vo.UserView;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import io.reactivex.subscribers.DisposableSubscriber;

public class UserViewModel extends ViewModel {

    private final GetUserList mGetUserList;
    private final UserMapper mUserMapper;

    @Inject
    public UserViewModel(GetUserList getUserList,
            UserMapper userMapper) {
        mGetUserList = getUserList;
        mUserMapper = userMapper;
        fetchUsers();
    }

    public void fetchUsers(){
        mUserMutableLiveData.postValue(new Resource(ResourceState.LOADING, null, null));
        mGetUserList.execute(new UserSubscriber(), null);
    }

    public LiveData<Resource<List<UserView>>> getUsers(){
        return mUserMutableLiveData;
    }

    @Override
    protected void onCleared() {
        mGetUserList.dispose();
        super.onCleared();
    }

    private final MutableLiveData<Resource<List<UserView>>> mUserMutableLiveData =
        new MutableLiveData<>();

}

4

3 回答 3

21

同样的问题,所以我不得不更新我的 androidx.appcompat 依赖项,如下所示:

implementation 'androidx.appcompat:appcompat:1.1.0-alpha04'
于 2019-04-14T19:41:26.763 回答
11

您不再需要实现这一点。

只需通过扩展 LifecycleOwner 的 AppCompatActivity 扩展您的活动。

此接口在 API 级别 1.0.0 中已弃用。使用扩展 LifecycleOwner 的 android.support.v7.app.AppCompatActivity,所以这个类没有用例。

你忘了覆盖

private final LifecycleRegistry lifecycleRegistry = new LifecycleRegistry(this);

@Override
public LifecycleRegistry getLifecycle() {
    return lifecycleRegistry;
}

您至少需要 Support-Lib 26.1

片段也一样。只需扩展您的 FragmentLifecycleFragment即可android.support.v4.app.Fragment

编辑:只需使用 Kotlin 重建一个快速示例项目,它就可以工作。

class MainActivity : AppCompatActivity(), HasSupportFragmentInjector, AnkoLogger {
    private val lifecycleRegistry by lazy { android.arch.lifecycle.LifecycleRegistry(this) }
    private val viewModel by lazy { ViewModelProviders.of(this, viewModelFactory).get(BridgesVm::class.java) }
    override fun getLifecycle() = lifecycleRegistry

    public override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        viewModel.data.observe(this, Observer<Bridge?> { info { "Received" } })
   }
}

class BridgesVm @Inject constructor() : ViewModel() { 
        val data: LiveData<Bridge> = MutableLiveData<Bridge>()
}

编辑:

使用 arch 版本 beta1 时出现问题。在解决此问题之前,请考虑使用 1.0.0-alpha9-1。在我的情况下,如果我使用 beta1 但使用 alpha 9-1,它就不起作用

于 2017-09-28T09:10:52.277 回答
7

使用android jetpack,它会解决你的问题

implementation 'androidx.appcompat:appcompat:1.1.0-rc01'
于 2019-07-16T11:44:35.083 回答