QueueAdapter.java revision e2104f4b5c8e3ad63570306a25e61502dfe4c418
1/* 2 * Copyright (C) 2015 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package com.example.android.supportv4.media; 18 19import android.app.Activity; 20import android.support.v4.content.ContextCompat; 21import android.support.v4.media.session.MediaSessionCompat; 22import android.view.LayoutInflater; 23import android.view.View; 24import android.view.ViewGroup; 25import android.widget.ArrayAdapter; 26import android.widget.ImageView; 27import android.widget.TextView; 28 29import com.example.android.supportv4.R; 30 31import java.util.ArrayList; 32 33/** 34 * A list adapter for items in a queue 35 */ 36public class QueueAdapter extends ArrayAdapter<MediaSessionCompat.QueueItem> { 37 38 // The currently selected/active queue item Id. 39 private long mActiveQueueItemId = MediaSessionCompat.QueueItem.UNKNOWN_ID; 40 41 public QueueAdapter(Activity context) { 42 super(context, R.layout.media_list_item, new ArrayList<MediaSessionCompat.QueueItem>()); 43 } 44 45 public void setActiveQueueItemId(long id) { 46 this.mActiveQueueItemId = id; 47 } 48 49 private static class ViewHolder { 50 ImageView mImageView; 51 TextView mTitleView; 52 TextView mDescriptionView; 53 } 54 55 @Override 56 public View getView(int position, View convertView, ViewGroup parent) { 57 ViewHolder holder; 58 59 if (convertView == null) { 60 convertView = LayoutInflater.from(getContext()) 61 .inflate(R.layout.media_list_item, parent, false); 62 holder = new ViewHolder(); 63 holder.mImageView = (ImageView) convertView.findViewById(R.id.play_eq); 64 holder.mTitleView = (TextView) convertView.findViewById(R.id.title); 65 holder.mDescriptionView = (TextView) convertView.findViewById(R.id.description); 66 convertView.setTag(holder); 67 } else { 68 holder = (ViewHolder) convertView.getTag(); 69 } 70 71 MediaSessionCompat.QueueItem item = getItem(position); 72 holder.mTitleView.setText(item.getDescription().getTitle()); 73 if (item.getDescription().getDescription() != null) { 74 holder.mDescriptionView.setText(item.getDescription().getDescription()); 75 } 76 77 // If the itemId matches the active Id then use a different icon 78 if (mActiveQueueItemId == item.getQueueId()) { 79 holder.mImageView.setImageDrawable( 80 ContextCompat.getDrawable(getContext(), R.drawable.ic_equalizer_white_24dp)); 81 } else { 82 holder.mImageView.setImageDrawable( 83 ContextCompat.getDrawable(getContext(), R.drawable.ic_play_arrow_white_24dp)); 84 } 85 return convertView; 86 } 87} 88