private void setUpGridView(View container) { mAvatarGrid = (GridView) container.findViewById(R.id.avatars); mAvatarGrid.setAdapter(new AvatarAdapter(getActivity())); mAvatarGrid.setOnItemClickListener( new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { mSelectedAvatarView = view; mSelectedAvatar = Avatar.values()[position]; } }); mAvatarGrid.setNumColumns(calculateSpanCount()); mAvatarGrid.setItemChecked(mSelectedAvatar.ordinal(), true); }
public class AvatarAdapter extends BaseAdapter { private static final Avatar[] mAvatars = Avatar.values(); private final LayoutInflater mLayoutInflater; public AvatarAdapter(Context context) { mLayoutInflater = LayoutInflater.from(context); } @Override public View getView(int position, View convertView, ViewGroup parent) { if (null == convertView) { convertView = mLayoutInflater.inflate(R.layout.item_avatar, parent, false); } setAvatar((AvatarView) convertView, mAvatars[position]); return convertView; } private void setAvatar(AvatarView mIcon, Avatar avatar) { mIcon.setImageResource(avatar.getDrawableId()); mIcon.setContentDescription(avatar.getNameForAccessibility()); } @Override public int getCount() { return mAvatars.length; } @Override public Object getItem(int position) { return mAvatars[position]; } @Override public long getItemId(int position) { return position; } }
private void setAvatar(AvatarView mIcon, Avatar avatar) { mIcon.setImageResource(avatar.getDrawableId()); mIcon.setContentDescription(avatar.getNameForAccessibility()); }