OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 package org.chromium.ui; | |
6 | |
7 import android.content.Context; | |
8 import android.graphics.drawable.GradientDrawable; | |
9 import android.graphics.drawable.LayerDrawable; | |
10 import android.text.TextUtils; | |
11 import android.view.LayoutInflater; | |
12 import android.view.View; | |
13 import android.view.ViewGroup; | |
14 import android.widget.AbsListView.LayoutParams; | |
15 import android.widget.BaseAdapter; | |
16 import android.widget.GridView; | |
17 import android.widget.LinearLayout; | |
18 | |
19 import java.util.ArrayList; | |
20 | |
21 public class ColorSuggestionListAdapter extends BaseAdapter implements View.OnCl ickListener { | |
newt (away)
2013/10/15 18:00:15
still need javadoc for the class
keishi
2013/10/16 04:47:23
Done.
| |
22 private Context mContext; | |
23 private ColorSuggestion[] mSuggestions; | |
24 private OnColorSuggestionClickListener mListener; | |
25 | |
26 /** | |
27 * The callback used to indicate the user has clicked on a suggestion. | |
28 */ | |
29 public interface OnColorSuggestionClickListener { | |
30 | |
31 /** | |
32 * Called upon a click on a suggestion. | |
33 * | |
34 * @param suggestion The suggestion that was clicked. | |
35 */ | |
36 void onColorSuggestionClick(ColorSuggestion suggestion); | |
37 } | |
38 | |
39 private final static int COLORS_PER_ROW = 4; | |
40 | |
41 ColorSuggestionListAdapter(Context context, ColorSuggestion[] suggestions) { | |
42 mContext = context; | |
43 mSuggestions = suggestions; | |
44 } | |
45 | |
46 /** | |
47 * Sets the listener that will be notified upon a click on a suggestion. | |
48 */ | |
49 public void setOnColorSuggestionClickListener(OnColorSuggestionClickListener listener) { | |
50 mListener = listener; | |
51 } | |
52 | |
53 /** | |
54 * Sets up the color button to represent a color suggestion. | |
55 * | |
56 * @param button The button view to set up. | |
57 * @param index The index of the suggestion in mSuggestions. | |
58 */ | |
59 private void setUpColorButton(View button, int index) { | |
60 if (index >= mSuggestions.length) { | |
61 button.setTag(null); | |
62 button.setContentDescription(null); | |
63 button.setVisibility(View.INVISIBLE); | |
64 return; | |
65 } | |
66 button.setTag(mSuggestions[index]); | |
67 button.setVisibility(View.VISIBLE); | |
68 ColorSuggestion suggestion = mSuggestions[index]; | |
69 LayerDrawable layers = (LayerDrawable) button.getBackground(); | |
70 GradientDrawable swatch = | |
71 (GradientDrawable) layers.findDrawableByLayerId(R.id.color_butto n_swatch); | |
72 swatch.setColor(suggestion.mColor); | |
73 String description = suggestion.mLabel; | |
74 if (TextUtils.isEmpty(description)) { | |
75 description = String.format("#%06X", (0xFFFFFF & suggestion.mColor)) ; | |
76 } | |
77 button.setContentDescription(description); | |
78 button.setOnClickListener(this); | |
79 } | |
80 | |
81 @Override | |
82 public void onClick(View v) { | |
83 if (mListener == null) { | |
84 return; | |
85 } | |
86 ColorSuggestion suggestion = (ColorSuggestion) v.getTag(); | |
87 if (suggestion == null) { | |
88 return; | |
89 } | |
90 mListener.onColorSuggestionClick(suggestion); | |
91 } | |
92 | |
93 @Override | |
94 public View getView(int position, View convertView, ViewGroup parent) { | |
95 LinearLayout layout = (LinearLayout) convertView; | |
96 if (convertView == null) { | |
newt (away)
2013/10/15 18:00:15
nit: convertView -> layout
keishi
2013/10/16 04:47:23
Done.
| |
97 layout = new LinearLayout(mContext); | |
98 layout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, | |
99 LayoutParams.WRAP_CONTENT)); | |
100 layout.setOrientation(LinearLayout.HORIZONTAL); | |
101 LayoutInflater inflater = LayoutInflater.from(mContext); | |
newt (away)
2013/10/15 18:00:15
inflater is unused
keishi
2013/10/16 04:47:23
Done.
| |
102 for (int i = 0; i < COLORS_PER_ROW; ++i) { | |
103 View button = new View(mContext); | |
104 button.setLayoutParams(new android.widget.LinearLayout.LayoutPar ams( | |
newt (away)
2013/10/15 18:00:15
to make the different types of layout params clear
keishi
2013/10/16 04:47:23
Done.
| |
105 0, | |
106 (int) mContext.getResources().getDimension(R.dimen.c olor_button_height), | |
newt (away)
2013/10/15 18:00:15
nit: get this value once outside of the loop
keishi
2013/10/16 04:47:23
Done.
| |
107 1f)); | |
108 button.setBackgroundResource(R.drawable.color_button_background) ; | |
109 layout.addView(button); | |
110 } | |
111 } | |
112 for (int i = 0; i < COLORS_PER_ROW; ++i) { | |
113 setUpColorButton(layout.getChildAt(i), position * COLORS_PER_ROW + i); | |
114 } | |
115 return layout; | |
116 } | |
117 | |
118 @Override | |
119 public long getItemId(int position) { | |
120 return position; | |
121 } | |
122 | |
123 @Override | |
124 public Object getItem(int position) { | |
125 return null; | |
126 } | |
127 | |
128 @Override | |
129 public int getCount() { | |
130 return (mSuggestions.length + COLORS_PER_ROW - 1) / COLORS_PER_ROW; | |
131 } | |
132 } | |
OLD | NEW |