代码之家  ›  专栏  ›  技术社区  ›  Andritchi Alexei

不知道如何为RecyclerView注入适配器

  •  1
  • Andritchi Alexei  · 技术社区  · 6 年前

    我做了一个学习匕首的简单项目。该应用程序正在从互联网上获取房产(度假租赁)列表,并将其显示在 RecyclerView 列表我用Dagger 2注入了所有依赖项,但列表的适配器除外。适配器非常标准,它接受属性列表并填充视图:

    public class PropertyListAdapter extends RecyclerView.Adapter<PropertyListAdapter.ViewHolder> {
    
        private List<Property> mPropertyList;
    
        @Inject
        public PropertyListAdapter(List<Property> propertyList) {
            mPropertyList = propertyList;
        }
    
        // onCreateViewHolder()
        // onBindViewHolder()
        // getItemCount()
        // class ViewHolder{}
    

    }

    该应用程序由一个活动组成,其中包含一个包含 recyclerview 列表在片段的 onActivityCreated() 我有:

    mPropertyViewModel.getPropertyList().observe(this, properties -> setPropertyAdapter(properties));
    

    以及 setPropertyAdapter() 是:

    private void setPropertyAdapter(List<Property> properties) {
        rvPropertyList.setAdapter(new PropertyListAdapter(properties));
    }
    

    所以,注入 PropertyListAdapter 我创建了一个模块:

    @Module
        public class AdapterModule {
        @Provides
        @Singleton
        PropertyListAdapter providePropertyListAdapter(List<Property> propertyList) {
            return new PropertyListAdapter(propertyList);
    }
    

    }

    但后来我意识到我必须注射 List<Property> 我不知道如何做到这一点,我陷入了困境。在本例中,如何注入适配器?

    P、 我将MVVM与架构组件一起使用。

    日志类别: error: [dagger.android.AndroidInjector.inject(T)] java.util.List<com.aandritchi.android.propertyrentals.data.domain.Property> cannot be provided without an @Provides-annotated method. java.util.List<com.aandritchi.android.propertyrentals.data.domain.Property> is injected at com.aandritchi.android.propertyrentals.di.module.data.AdapterModule.providePropertyListAdapter(propertyList) com.aandritchi.android.propertyrentals.ui.search_result.PropertyListAdapter is injected at com.aandritchi.android.propertyrentals.ui.search_result.PropertySearchResultFragment.mPropertyListAdapter com.aandritchi.android.propertyrentals.ui.search_result.PropertySearchResultFragment is injected at com.aandritchi.android.propertyrentals.ui.home.HomeActivity.mPropertySearchResultFragment com.aandritchi.android.propertyrentals.ui.home.HomeActivity is injected at dagger.android.AndroidInjector.inject(arg0)

    2 回复  |  直到 6 年前
        1
  •  2
  •   Josef Adamcik    6 年前

    你应该问问自己:我真的需要注入适配器吗?

    您想要这样做的两个原因:

    1) 适配器具有要注入的依赖项。这不是您的情况,因为除了属性列表之外,没有其他依赖项。

    2) 您希望从活动/片段中删除创建适配器实例的责任。

    这两个问题的正确解决方案都是注入一个工厂,该工厂已经注入了所有可注入的依赖项,并提供了创建实例的方法。

        2
  •  1
  •   Andritchi Alexei    6 年前

    之后 David Medenjak 我意识到我没有正确地使用构造函数注入,而且这种方法是错误的。像 David Medenjak 建议我为 List<Property> 然后我在片段中初始化了它。因此,解决方案是:

    public class PropertyListAdapter extends RecyclerView.Adapter<PropertyListAdapter.ViewHolder> {
    
        private List<Property> mPropertyList;
    
        public void setPropertyList(List<Property> propertyList) {
            mPropertyList = propertyList;
        }
    
        @Inject
        public PropertyListAdapter() {
        }
    
        // onCreateViewHolder()
        // onBindViewHolder()
        // getItemCount()
        // class ViewHolder{}
    }
    

    在片段中,我需要设置属性列表:

    private void setPropertyAdapter(List<Property> properties) {
        mPropertyListAdapter.setPropertyList(properties); // added this line
        rvPropertyList.setAdapter(mPropertyListAdapter);
    }