Alexander Lucas | 97842ff | 2014-03-07 14:56:55 -0800 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2014 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 | |
| 17 | package com.example.android.adaptertransition; |
| 18 | |
| 19 | import android.view.LayoutInflater; |
| 20 | import android.view.View; |
| 21 | import android.view.ViewGroup; |
| 22 | import android.widget.BaseAdapter; |
| 23 | import android.widget.ImageView; |
| 24 | import android.widget.TextView; |
| 25 | |
| 26 | /** |
| 27 | * This class provides data as Views. It is designed to support both ListView and GridView by |
| 28 | * changing a layout resource file to inflate. |
| 29 | */ |
| 30 | public class MeatAdapter extends BaseAdapter { |
| 31 | |
| 32 | private final LayoutInflater mLayoutInflater; |
| 33 | private final int mResourceId; |
| 34 | |
| 35 | /** |
| 36 | * Create a new instance of {@link MeatAdapter}. |
| 37 | * |
| 38 | * @param inflater The layout inflater. |
| 39 | * @param resourceId The resource ID for the layout to be used. The layout should contain an |
| 40 | * ImageView with ID of "meat_image" and a TextView with ID of "meat_title". |
| 41 | */ |
| 42 | public MeatAdapter(LayoutInflater inflater, int resourceId) { |
| 43 | mLayoutInflater = inflater; |
| 44 | mResourceId = resourceId; |
| 45 | } |
| 46 | |
| 47 | @Override |
| 48 | public int getCount() { |
| 49 | return Meat.MEATS.length; |
| 50 | } |
| 51 | |
| 52 | @Override |
| 53 | public Meat getItem(int position) { |
| 54 | return Meat.MEATS[position]; |
| 55 | } |
| 56 | |
| 57 | @Override |
| 58 | public long getItemId(int position) { |
| 59 | return Meat.MEATS[position].resourceId; |
| 60 | } |
| 61 | |
| 62 | @Override |
| 63 | public View getView(int position, View convertView, ViewGroup parent) { |
| 64 | final View view; |
| 65 | final ViewHolder holder; |
| 66 | if (null == convertView) { |
| 67 | view = mLayoutInflater.inflate(mResourceId, parent, false); |
| 68 | holder = new ViewHolder(); |
| 69 | assert view != null; |
| 70 | holder.image = (ImageView) view.findViewById(R.id.meat_image); |
| 71 | holder.title = (TextView) view.findViewById(R.id.meat_title); |
| 72 | view.setTag(holder); |
| 73 | } else { |
| 74 | view = convertView; |
| 75 | holder = (ViewHolder) view.getTag(); |
| 76 | } |
| 77 | Meat meat = getItem(position); |
| 78 | holder.image.setImageResource(meat.resourceId); |
| 79 | holder.title.setText(meat.title); |
| 80 | return view; |
| 81 | } |
| 82 | |
| 83 | private static class ViewHolder { |
| 84 | public ImageView image; |
| 85 | public TextView title; |
| 86 | } |
| 87 | |
| 88 | } |