Created
October 3, 2017 19:23
-
-
Save wwe-johndpope/c75434b1a36fd2283701324ecf863570 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.wwe.universe.ui.adapter; | |
import android.content.Context; | |
import android.support.annotation.NonNull; | |
import android.view.LayoutInflater; | |
import android.view.View; | |
import android.view.ViewGroup; | |
import android.widget.ArrayAdapter; | |
import android.widget.ImageView; | |
import android.widget.TextView; | |
import com.bumptech.glide.Glide; | |
import com.wwe.universe.R; | |
import com.wwe.universe.data.model.Video; | |
import java.util.List; | |
import java.util.Locale; | |
public class PlaylistStackAdapter extends ArrayAdapter<Video> { | |
private static final List<Video> mVideos; | |
private final LayoutInflater mInflater; | |
public PlaylistStackAdapter(List<Video> videos, Context context) { | |
super(context, R.layout.item_playlist_stack, videos); | |
PlaylistStackAdapter.mVideos = videos; | |
mInflater = LayoutInflater.from(context); | |
} | |
@Override | |
public int getCount() { | |
return PlaylistStackAdapter.mVideos.size(); | |
} | |
@Override | |
public Video getItem(int position) { | |
return PlaylistStackAdapter.mVideos.get(position); | |
} | |
@Override | |
public long getItemId(int position) { | |
return position; | |
} | |
@NonNull | |
@Override | |
public View getView(int position, View convertView, @NonNull ViewGroup parent) { | |
Video video = mVideos.get(position); | |
ViewHolder holder; | |
if (convertView == null) { | |
convertView = mInflater.inflate(R.layout.item_playlist_stack, parent, false); | |
holder = new ViewHolder(convertView); | |
convertView.setTag(holder); | |
} else { | |
holder = (ViewHolder) convertView.getTag(); | |
} | |
Glide.with(convertView.getContext()).load(video.getThumbnailUrl()).into(holder.videoImage); | |
holder.videoDuration.setText(formatDuration(video.getDuration())); | |
return convertView; | |
} | |
private String formatDuration(long duration) { | |
int seconds = (int) duration % 60; | |
int minutes = (int) duration / 60; | |
return String.format(Locale.US, "%d:%02d", minutes, seconds); | |
} | |
public static class ViewHolder { | |
final ImageView videoImage; | |
final TextView videoDuration; | |
ViewHolder(View view) { | |
videoImage = (ImageView) view.findViewById(R.id.video_image); | |
videoDuration = (TextView) view.findViewById(R.id.video_duration); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment