我尝试使用RecyclerVIewAdapter和Picasso将图像添加到RecyclerView

时间:2016-07-28 17:20:17

标签: android android-recyclerview

我试图这样做,但我没有运气,图片不会显示在屏幕上 我对此非常陌生,所以这里是我的代码

我的RecyclerViewAdapter:

public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> {

    public Context context;
    public ArrayList<MoviesData> array;
    public LayoutInflater inflater;


    RecyclerViewAdapter(Context context,ArrayList<MoviesData> array)
    {
        this.context = context;
        this.array = array;
        this.inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }



    public class ViewHolder extends RecyclerView.ViewHolder {

        ImageView image;


        public ViewHolder(View itemView) {
            super(itemView);
             image = (ImageView) itemView.findViewById(R.id.oneMovieBlockImageView);
        }


    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View rootview = inflater.inflate(R.layout.movie_block,parent,false);

        ViewHolder viewHolder = new ViewHolder(rootview);

        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {


        //holder.image.setImageResource(Picasso.with(context).load(array.get(position).getPoster_path()).into(image));
        MoviesData current = array.get(position);
        Picasso.with(context).load(current.getPoster_path()).into(holder.image);


    }

    @Override
    public int getItemCount() {
        return 0;
    }
}

我的LinearLayout有RecyclerView

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.android.themovieapp.MainActivityFragment"
    >


    <android.support.v7.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/recyclerview"
        >


    </android.support.v7.widget.RecyclerView>

</LinearLayout>

我的ImageView

<ImageView

    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:scaleType="centerCrop"
    android:id="@+id/oneMovieBlockImageView"
    android:adjustViewBounds="true"

    >
</ImageView>

onCreateView方法

 @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    updateMovies();


    View rootView = inflater.inflate(R.layout.fragment_main,container,false);


    RecyclerView recyclerView = (RecyclerView) rootView.findViewById(R.id.recyclerview);
    adapter = new RecyclerViewAdapter(getActivity(),moviesDatas);
    recyclerView.setAdapter(adapter);
    recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));


    return rootView;

}

1 个答案:

答案 0 :(得分:1)

您的适配器为空

@Override
public int getItemCount() {
    return 0;
}}

请更新至return array.size();