20
20
/**
21
21
* Created by wuyr on 18-5-20 上午4:09.
22
22
*/
23
- public class PathAdapter extends BaseAdapter <String , PathAdapter .ViewHolder > {
23
+ public class PathAdapter extends BaseAdapter <Object , PathAdapter .ViewHolder > {
24
24
25
25
public static final int TYPE_CARD = 0 , TYPE_J20 = 1 , TYPE_DRAGON = 2 ;
26
26
private Toast mToast ;
27
27
private int mCurrentType ;
28
28
private Random mRandom = new Random ();
29
29
private List <SoftReference <Bitmap >> mBitmapList ;
30
30
31
- public PathAdapter (Context context , List <String > data ) {
31
+ public PathAdapter (Context context , List <Object > data ) {
32
32
super (context , data , R .layout .adapter_item_view , ViewHolder .class );
33
33
mToast = Toast .makeText (context , "" , Toast .LENGTH_SHORT );
34
34
initBitmaps ();
@@ -94,7 +94,6 @@ private SoftReference<Bitmap> getBitmapById(int id) {
94
94
return new SoftReference <>(decodeSampledBitmapFromResource (mContext .getResources (), id ));
95
95
}
96
96
97
-
98
97
@ Override
99
98
public void onDetachedFromRecyclerView (@ NonNull RecyclerView recyclerView ) {
100
99
super .onDetachedFromRecyclerView (recyclerView );
@@ -162,11 +161,13 @@ private void initDragonHolder(ViewHolder holder, int position) {
162
161
163
162
holder .imageView2 .getLayoutParams ().width = 135 ;
164
163
holder .imageView2 .requestLayout ();
165
- if (position == 0 ) {
164
+ if (position == 10 ) {
166
165
holder .imageView2 .setImageBitmap (getBitmap (7 ));
166
+ } else if (position < 10 ) {
167
+ holder .imageView2 .setImageBitmap (null );
167
168
} else if (position == mData .size () - 1 ) {
168
169
holder .imageView2 .setImageBitmap (getBitmap (4 ));
169
- } else {
170
+ } else if ( position < mData . size () - 1 ) {
170
171
holder .imageView2 .setImageBitmap (getBitmap (mRandom .nextBoolean () ? 5 : 6 ));
171
172
}
172
173
}
@@ -181,9 +182,36 @@ private Bitmap getBitmap(int index) {
181
182
}
182
183
183
184
public void setType (int type ) {
184
- mCurrentType = type ;
185
- notifyItemRangeChanged (0 , getItemCount ());
185
+ if (mCurrentType != type ) {
186
+ if (type == TYPE_DRAGON ) {
187
+ List <Object > tempList = new ArrayList <>();
188
+ for (int i = 0 ; i < 10 ; i ++) {
189
+ tempList .add (null );
190
+ }
191
+ mData .addAll (tempList );
192
+ } else {
193
+ if (mCurrentType == TYPE_DRAGON ) {
194
+ for (int i = 0 ; i < 10 ; i ++) {
195
+ int index = mData .size () - 1 ;
196
+ if (index >= 0 ) {
197
+ mData .remove (index );
198
+ }
199
+ }
200
+ }
201
+ }
202
+ mCurrentType = type ;
203
+ notifyItemRangeChanged (0 , getItemCount ());
204
+ }
186
205
}
206
+
207
+ @ Override
208
+ public void addData (@ NonNull List <Object > data ) {
209
+ super .addData (data );
210
+ if (mCurrentType == TYPE_DRAGON && mData .size () < 20 ) {
211
+ super .addData (data );
212
+ }
213
+ }
214
+
187
215
private int calculateInSampleSize (BitmapFactory .Options options ) {
188
216
int reqWidth = 0 ;
189
217
int reqHeight = 0 ;
@@ -203,29 +231,22 @@ private int calculateInSampleSize(BitmapFactory.Options options) {
203
231
default :
204
232
break ;
205
233
}
206
- // 源图片的高度和宽度
207
234
final int height = options .outHeight ;
208
235
final int width = options .outWidth ;
209
236
int inSampleSize = 1 ;
210
237
if (height > reqHeight || width > reqWidth ) {
211
- // 计算出实际宽高和目标宽高的比率
212
238
final int heightRatio = Math .round ((float ) height / (float ) reqHeight );
213
239
final int widthRatio = Math .round ((float ) width / (float ) reqWidth );
214
- // 选择宽和高中最小的比率作为inSampleSize的值,这样可以保证最终图片的宽和高
215
- // 一定都会大于等于目标的宽和高。
216
240
inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio ;
217
241
}
218
242
return inSampleSize ;
219
243
}
220
244
221
245
private Bitmap decodeSampledBitmapFromResource (Resources res , int resId ) {
222
- // 第一次解析将inJustDecodeBounds设置为true,来获取图片大小
223
246
final BitmapFactory .Options options = new BitmapFactory .Options ();
224
247
options .inJustDecodeBounds = true ;
225
248
BitmapFactory .decodeResource (res , resId , options );
226
- // 调用上面定义的方法计算inSampleSize值
227
249
options .inSampleSize = calculateInSampleSize (options );
228
- // 使用获取到的inSampleSize值再次解析图片
229
250
options .inJustDecodeBounds = false ;
230
251
try {
231
252
return BitmapFactory .decodeResource (res , resId , options );
0 commit comments