ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

android – 如何用Picasso设置@BindingAdapter?

2019-06-11 11:11:16  阅读:239  来源: 互联网

标签:android picasso android-databinding


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

我的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);

    }

}

布局:

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <data class="PopularMoviesBinding">
    <variable
        name="pmvm"
        type="com.hartyandi.oviesm.modelviews.PopularMoviesViewModel"></variable>

    </data>

    <LinearLayout


        android:id="@+id/row"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#FFFFFF"
        android:paddingBottom="0dp"
        android:paddingTop="5dp"
        android:paddingRight="2.5dp"
        android:paddingLeft="5dp"
        android:orientation="vertical">

        <ImageView
            app:imageUrl="@{pmvm.imageUrl}"
            android:id="@+id/popular_movies_grid_image"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="0dp"
            android:adjustViewBounds="true"
            android:elevation="20dp">

        </ImageView>

        <TextView
            android:id="@+id/popular_movies_grid_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@{pmvm.title}"
            android:textColor="#000000"
            android:textSize="12sp"
            android:background="#FFFFFF"
            >
        </TextView>

    </LinearLayout>
</layout>

适配器:

public class PopularMoviesAdapter extends RecyclerView.Adapter<PopularMoviesAdapter.BindingHolder> {

    private List<Movie> movies;
    private Context context;

    public PopularMoviesAdapter(List<Movie> 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. 

I tried to change my implementation just like this stackoverflow post suggest,我得到了相同的错误消息.

I also used the following code as example.

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

解决方法:

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

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

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

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <data class="PopularMoviesBinding">
        <variable name="pmvm"
            type="com.hartyandi.oviesm.modelviews.PopularMoviesViewModel"/>
        <variable name="picasso" type="com.whatever.Picasso"/>
    </data>
    <!-- ... -->
    <ImageView
        app:imageUrl="@{pmvm.imageUrl}"
        app:picasso="@{picasso}"
        ... />
    </ImageView>
</layout>

然后在你的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现在是静态的.

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

<ImageView
    app:imageUrl="@{pmvm.imageUrl}"
    app:picasso="@{pmvm.picasso}"
    ... />

和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());

标签:android,picasso,android-databinding
来源: https://codeday.me/bug/20190611/1218136.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有