当前位置:  开发笔记 > Android > 正文

如何用Picasso设置@BindingAdapter?

如何解决《如何用Picasso设置@BindingAdapter?》经验,为你挑选了1个好方法。

我想用绑定创建一个电影海报图像的网格视图.

我的viewmodel看起来像这样:

public class PopularMoviesViewModel extends BaseObservable {

    Movie movie;
    Context context;

    MovieServiceComponent movieServiceComponent = DaggerMovieServiceComponent.builder()
            .contextModule(new ContextModule(context))
            .build();

    Picasso getPicasso = movieServiceComponent.getPicasso();

    public PopularMoviesViewModel(Movie movie, Context context) {
        this.movie = movie;
        this.context = context;
    }

    @Bindable
    public String getImageUrl(){
        return movie.posterPath();
    }

    @Bindable
    public String getTitle(){
        return movie.originalTitle();
    }

    @BindingAdapter({"imageUrl"})
    public void setImageUrl(ImageView view, String poserPath){
        getPicasso.with(view.getContext()).load("http://image.tmdb.org/t/p/w185"+ poserPath).into(view);

    }

}

布局:




    
    

    

    

        

        

        
        

    

适配器:

public class PopularMoviesAdapter extends RecyclerView.Adapter {

    private List movies;
    private Context context;

    public PopularMoviesAdapter(List movies, Context context) {
        this.movies = movies;
        this.context = context;
    }

    public void add(Movie movie){
        movies.add(movie);
    }

    @Override
    public BindingHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        PopularMoviesBinding popularMoviesBinding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()),
                R.layout.popular_movies_gridview_row, parent,false);
        return new BindingHolder(popularMoviesBinding);
    }

    @Override
    public void onBindViewHolder(PopularMoviesAdapter.BindingHolder holder, int position) {
        PopularMoviesBinding popularMoviesBinding = holder.popularMoviesBinding;
        popularMoviesBinding.setPmvm(new PopularMoviesViewModel(movies.get(position), context));
    }

    @Override
    public int getItemCount() {
        return movies.size();
    }

    public class BindingHolder extends RecyclerView.ViewHolder{

        private PopularMoviesBinding popularMoviesBinding;

        public BindingHolder(PopularMoviesBinding popularMoviesBinding) {
            super(popularMoviesBinding.getRoot());
            this.popularMoviesBinding = popularMoviesBinding;
        }
 }
}

我收到以下错误:

java.lang.IllegalStateException: Required DataBindingComponent is null in class PopularMoviesBinding.A BindingAdapter in modelviews.PopularMoviesViewModel is not static and requires an object to use, retrieved from the DataBindingComponent. 

我尝试改变我的实现就像这个stackoverflow帖子建议,我得到了相同的错误消息.

我还使用以下代码作为示例.

有人可以解释代码的问题,以及如何解决它?



1> George Mount..:

您可能不打算使用实例方法BindingAdapter.

如果这样做,则必须提供DataBindingComponent,以便生成的Binding类知道要使用的实例.

您有两个选项 - 提供DataBindingComponent或只是将所需的上下文作为属性传递给静态绑定适配器方法.第二个更容易理解,所以我将从那开始:




    
        
        
    
    
    
    

然后在你的BindingAdapter中:

@BindingAdapter({"imageUrl", "picasso"})
public static void setImageUrl(ImageView view, String poserPath, Picasso picasso){
    picasso.with(view.getContext()).load("http://image.tmdb.org/t/p/w185"+ poserPath).into(view);
}

请注意,setImageUrl现在是static.

或者,由于您的Picasso实例也在ViewModel上,您可以通过为毕加索添加一个getter来传递实例:


和ViewModel中的方法:

public Picasso getPicasso() { return this.getPicasso; }

另一种方式意味着你实现了DataBindingComponent.当您创建实例BindingAdapter方法时,生成的接口将为您的类提供一个getter.您需要创建一个类来实现该接口:

public class MyDataBindingComponent implements DataBindingComponent {
    public PopularMoviesViewModel getPopularMoviesViewModel() {
         return whateverIDoToCreateOrGetThisBindingAdapterInstance();
    }
}

然后在膨胀或绑定时传递实例:

PopularMoviesBinding popularMoviesBinding = 
    DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()),
        R.layout.popular_movies_gridview_row, parent,false,
        new MyDataBindingComponent());

推荐阅读
拾味湖
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有