/*
* Copyright 2014 Google Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.example.leanback;
import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.android.example.leanback.data.Video;
import com.squareup.picasso.Picasso;
import java.util.List;
public class VideoAdapter extends RecyclerView.Adapter<VideoAdapter.ViewHolder> {
private List<Video> mDataset;
private Context mContext;
// Provide a reference to the views for each data item
// Complex data items may need more than one view per item, and
// you provide access to all the views for a data item in a view holder
public static class ViewHolder extends RecyclerView.ViewHolder {
// each data item is just a string in this case
public CardView mCardView;
public ViewHolder(CardView v) {
super(v);
mCardView = v;
}
}
// Provide a suitable constructor (depends on the kind of dataset)
public VideoAdapter(List<Video> myDataset, Context context) {
mDataset = myDataset;
mContext = context;
}
// Create new views (invoked by the layout manager)
@Override
public VideoAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
int viewType) {
// create a new view
CardView v = (CardView) LayoutInflater.from(parent.getContext())
.inflate(R.layout.video_card, parent, false);
// set the view's size, margins, paddings and layout parameters
ViewHolder vh = new ViewHolder(v);
return vh;
}
// Replace the contents of a view (invoked by the layout manager)
@Override
public void onBindViewHolder(final ViewHolder holder, int position) {
// - get element from your dataset at this position
// - replace the contents of the view with that element
Video video = mDataset.get(position);
Log.d("VideoAdapter", video.getThumbUrl());
((TextView)holder.mCardView.findViewById(R.id.info_text)).setText(video.getTitle());
ImageView imageView = (ImageView) holder.mCardView.findViewById(R.id.info_image);
Picasso.with(holder.mCardView.getContext()).load(video.getThumbUrl()).into(imageView);
holder.mCardView.findViewById(R.id.play_button).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(holder.mCardView.getContext(), PlayerActivity.class);
holder.mCardView.getContext().startActivity(intent);
}});
}
// Return the size of your dataset (invoked by the layout manager)
@Override
public int getItemCount() {
return mDataset.size();
}
}