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.fragmenttransition; 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 class MeatAdapter extends BaseAdapter { 27 28 private final LayoutInflater mLayoutInflater; 29 private final int mResourceId; 30 31 public MeatAdapter(LayoutInflater inflater, int resourceId) { 32 mLayoutInflater = inflater; 33 mResourceId = resourceId; 34 } 35 36 @Override 37 public int getCount() { 38 return Meat.MEATS.length; 39 } 40 41 @Override 42 public Meat getItem(int position) { 43 return Meat.MEATS[position]; 44 } 45 46 @Override 47 public long getItemId(int position) { 48 return Meat.MEATS[position].resourceId; 49 } 50 51 @Override 52 public View getView(int position, View convertView, ViewGroup parent) { 53 final View view; 54 final ViewHolder holder; 55 if (null == convertView) { 56 view = mLayoutInflater.inflate(mResourceId, parent, false); 57 holder = new ViewHolder(); 58 assert view != null; 59 holder.image = (ImageView) view.findViewById(R.id.image); 60 holder.title = (TextView) view.findViewById(R.id.title); 61 view.setTag(holder); 62 } else { 63 view = convertView; 64 holder = (ViewHolder) view.getTag(); 65 } 66 bindView(holder, position); 67 return view; 68 } 69 70 public void bindView(ViewHolder holder, int position) { 71 Meat meat = getItem(position); 72 holder.image.setImageResource(meat.resourceId); 73 holder.title.setText(meat.title); 74 } 75 76 public static class ViewHolder { 77 public ImageView image; 78 public TextView title; 79 } 80 81 }