Blame view

src/com/ectrip/cyt/spinnerwheel/adapters/AbstractWheelTextAdapter.java 8.37 KB
3c2353cd   杜方   1、畅游通核销app源码提交;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
  /*
   * android-spinnerwheel
   * https://github.com/ai212983/android-spinnerwheel
   *
   * based on
   *
   * Android Wheel Control.
   * https://code.google.com/p/android-wheel/
   *
   * Copyright 2011 Yuri Kanivets
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   * http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  package com.ectrip.cyt.spinnerwheel.adapters;
  
  import android.content.Context;
  import android.graphics.Typeface;
  import android.util.Log;
  import android.view.Gravity;
  import android.view.LayoutInflater;
  import android.view.View;
  import android.view.ViewGroup;
  import android.widget.TextView;
  
  /**
   * Abstract spinnerwheel adapter provides common functionality for adapters.
   */
  public abstract class AbstractWheelTextAdapter extends AbstractWheelAdapter {
      
      /** Text view resource. Used as a default view for adapter. */
      public static final int TEXT_VIEW_ITEM_RESOURCE = -1;
      
      /** No resource constant. */
      protected static final int NO_RESOURCE = 0;
      
      /** Default text color */
      public static final int DEFAULT_TEXT_COLOR = 0xFF101010;
      
      /** Default text color */
      public static final int LABEL_COLOR = 0xFF700070;
      
      /** Default text size */
      public static final int DEFAULT_TEXT_SIZE = 30;
  
      /// Custom text typeface
      private Typeface textTypeface;
      
      // Text settings
      private int textColor = DEFAULT_TEXT_COLOR;
      private int textSize = DEFAULT_TEXT_SIZE;
      
      // Current context
      protected Context context;
      // Layout inflater
      protected LayoutInflater inflater;
      
      // Items resources
      protected int itemResourceId;
      protected int itemTextResourceId;
      
      // Empty items resources
      protected int emptyItemResourceId;
  
  
      /**
       * Constructor
       * @param context the current context
       */
      protected AbstractWheelTextAdapter(Context context) {
          this(context, TEXT_VIEW_ITEM_RESOURCE);
      }
  
      /**
       * Constructor
       * @param context the current context
       * @param itemResource the resource ID for a layout file containing a TextView to use when instantiating items views
       */
      protected AbstractWheelTextAdapter(Context context, int itemResource) {
          this(context, itemResource, NO_RESOURCE);
      }
      
      /**
       * Constructor
       * @param context the current context
       * @param itemResource the resource ID for a layout file containing a TextView to use when instantiating items views
       * @param itemTextResource the resource ID for a text view in the item layout
       */
      protected AbstractWheelTextAdapter(Context context, int itemResource, int itemTextResource) {
          this.context = context;
          itemResourceId = itemResource;
          itemTextResourceId = itemTextResource;
          
          inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
      }
      
      /**
       * Gets text color
       * @return the text color
       */
      public int getTextColor() {
          return textColor;
      }
      
      /**
       * Sets text color
       * @param textColor the text color to set
       */
      public void setTextColor(int textColor) {
          this.textColor = textColor;
      }
  
      /**
       * Sets text typeface
       * @param typeface typeface to set
       */
      public void setTextTypeface(Typeface typeface) {
          this.textTypeface = typeface;
      }
  
      /**
       * Gets text size
       * @return the text size
       */
      public int getTextSize() {
          return textSize;
      }
      
      /**
       * Sets text size
       * @param textSize the text size to set
       */
      public void setTextSize(int textSize) {
          this.textSize = textSize;
      }
      
      /**
       * Gets resource Id for items views
       * @return the item resource Id
       */
      public int getItemResource() {
          return itemResourceId;
      }
      
      /**
       * Sets resource Id for items views
       * @param itemResourceId the resource Id to set
       */
      public void setItemResource(int itemResourceId) {
          this.itemResourceId = itemResourceId;
      }
      
      /**
       * Gets resource Id for text view in item layout 
       * @return the item text resource Id
       */
      public int getItemTextResource() {
          return itemTextResourceId;
      }
      
      /**
       * Sets resource Id for text view in item layout 
       * @param itemTextResourceId the item text resource Id to set
       */
      public void setItemTextResource(int itemTextResourceId) {
          this.itemTextResourceId = itemTextResourceId;
      }
  
      /**
       * Gets resource Id for empty items views
       * @return the empty item resource Id
       */
      public int getEmptyItemResource() {
          return emptyItemResourceId;
      }
  
      /**
       * Sets resource Id for empty items views
       * @param emptyItemResourceId the empty item resource Id to set
       */
      public void setEmptyItemResource(int emptyItemResourceId) {
          this.emptyItemResourceId = emptyItemResourceId;
      }
  
      /**
       * Returns text for specified item
       * @param index the item index
       * @return the text of specified items
       */
      protected abstract CharSequence getItemText(int index);
  
      @Override
      public View getItem(int index, View convertView, ViewGroup parent) {
          if (index >= 0 && index < getItemsCount()) {
              if (convertView == null) {
                  convertView = getView(itemResourceId, parent);
              }
              TextView textView = getTextView(convertView, itemTextResourceId);
              if (textView != null) {
                  CharSequence text = getItemText(index);
                  if (text == null) {
                      text = "";
                  }
                  textView.setText(text);
                  configureTextView(textView);
              }
              return convertView;
          }
          return null;
      }
  
      @Override
      public View getEmptyItem(View convertView, ViewGroup parent) {
          if (convertView == null) {
              convertView = getView(emptyItemResourceId, parent);
          }
          if (convertView instanceof TextView) {
              configureTextView((TextView)convertView);
          }
              
          return convertView;
      }
  
      /**
       * Configures text view. Is called for the TEXT_VIEW_ITEM_RESOURCE views.
       * @param view the text view to be configured
       */
      protected void configureTextView(TextView view) {
          if (itemResourceId == TEXT_VIEW_ITEM_RESOURCE) {
              view.setTextColor(textColor);
              view.setGravity(Gravity.CENTER);
              view.setTextSize(textSize);
              view.setLines(1);
          }
          if (textTypeface != null) {
              view.setTypeface(textTypeface);
          } else {
              view.setTypeface(Typeface.SANS_SERIF, Typeface.BOLD);
          }
      }
      
      /**
       * Loads a text view from view
       * @param view the text view or layout containing it
       * @param textResource the text resource Id in layout
       * @return the loaded text view
       */
      private TextView getTextView(View view, int textResource) {
          TextView text = null;
          try {
              if (textResource == NO_RESOURCE && view instanceof TextView) {
                  text = (TextView) view;
              } else if (textResource != NO_RESOURCE) {
                  text = (TextView) view.findViewById(textResource);
              }
          } catch (ClassCastException e) {
              Log.e("AbstractWheelAdapter", "You must supply a resource ID for a TextView");
              throw new IllegalStateException(
                      "AbstractWheelAdapter requires the resource ID to be a TextView", e);
          }
          
          return text;
      }
      
      /**
       * Loads view from resources
       * @param resource the resource Id
       * @return the loaded view or null if resource is not set
       */
      private View getView(int resource, ViewGroup parent) {
          switch (resource) {
          case NO_RESOURCE:
              return null;
          case TEXT_VIEW_ITEM_RESOURCE:
              return new TextView(context);
          default:
              return inflater.inflate(resource, parent, false);
          }
      }
  }