|
1 | 1 | package com.wuyr.pathlayoutmanagertest;
|
2 | 2 |
|
3 | 3 | import android.content.Context;
|
| 4 | +import android.graphics.Bitmap; |
| 5 | +import android.graphics.BitmapFactory; |
| 6 | +import android.support.annotation.NonNull; |
4 | 7 | import android.support.v7.widget.RecyclerView;
|
5 | 8 | import android.view.View;
|
6 | 9 | import android.widget.ImageView;
|
7 |
| -import android.widget.TextView; |
8 | 10 | import android.widget.Toast;
|
9 | 11 |
|
| 12 | +import java.lang.ref.SoftReference; |
| 13 | +import java.util.ArrayList; |
10 | 14 | import java.util.List;
|
| 15 | +import java.util.Locale; |
| 16 | +import java.util.Random; |
11 | 17 |
|
12 | 18 | /**
|
13 | 19 | * Created by wuyr on 18-5-20 上午4:09.
|
14 | 20 | */
|
15 | 21 | public class PathAdapter extends BaseAdapter<String, PathAdapter.ViewHolder> {
|
16 | 22 |
|
17 |
| - private Toast toast; |
| 23 | + public static final int TYPE_CARD = 0, TYPE_J20 = 1, TYPE_DRAGON = 2; |
| 24 | + private Toast mToast; |
| 25 | + private int mCurrentType; |
| 26 | + private Random mRandom = new Random(); |
| 27 | + private List<SoftReference<Bitmap>> mBitmapList; |
18 | 28 |
|
19 | 29 | public PathAdapter(Context context, List<String> data) {
|
20 | 30 | super(context, data, R.layout.adapter_item_view, ViewHolder.class);
|
21 |
| - toast = Toast.makeText(context, "", Toast.LENGTH_SHORT); |
| 31 | + mToast = Toast.makeText(context, "", Toast.LENGTH_SHORT); |
| 32 | + } |
| 33 | + |
| 34 | + private void initBitmaps() { |
| 35 | + mBitmapList = new ArrayList<>(); |
| 36 | + mBitmapList.add(getBitmapById(R.drawable.ic_1)); |
| 37 | + mBitmapList.add(getBitmapById(R.drawable.ic_2)); |
| 38 | + mBitmapList.add(getBitmapById(R.drawable.ic_3)); |
| 39 | + mBitmapList.add(getBitmapById(R.drawable.ic_j20)); |
| 40 | + mBitmapList.add(getBitmapById(R.drawable.ic_dragon_head)); |
| 41 | + mBitmapList.add(getBitmapById(R.drawable.ic_dragon_body_1)); |
| 42 | + mBitmapList.add(getBitmapById(R.drawable.ic_dragon_body_2)); |
| 43 | + mBitmapList.add(getBitmapById(R.drawable.ic_dragon_tail)); |
| 44 | + } |
| 45 | + |
| 46 | + @NonNull |
| 47 | + private SoftReference<Bitmap> getBitmapById(int id) { |
| 48 | + return new SoftReference<>(BitmapFactory.decodeResource(mContext.getResources(), id)); |
| 49 | + } |
| 50 | + |
| 51 | + @Override |
| 52 | + public void onDetachedFromRecyclerView(@NonNull RecyclerView recyclerView) { |
| 53 | + super.onDetachedFromRecyclerView(recyclerView); |
| 54 | + recyclerBitmaps(); |
| 55 | + } |
| 56 | + |
| 57 | + private void recyclerBitmaps() { |
| 58 | + for (SoftReference<Bitmap> softReference : mBitmapList) { |
| 59 | + Bitmap bitmap = softReference.get(); |
| 60 | + if (bitmap != null) { |
| 61 | + bitmap.recycle(); |
| 62 | + } |
| 63 | + softReference.clear(); |
| 64 | + } |
| 65 | + mBitmapList.clear(); |
| 66 | + mBitmapList = null; |
22 | 67 | }
|
23 | 68 |
|
24 | 69 | @Override
|
25 |
| - public void onBindViewHolder(ViewHolder holder, int position) { |
26 |
| -// |
27 |
| -// if (position == 0) { |
28 |
| -// holder.imageView.setImageResource(R.drawable.ic_dragon_tail); |
29 |
| -// } else if (position == mData.size() - 1) { |
30 |
| -// holder.imageView.setImageResource(R.drawable.ic_dragon_head); |
31 |
| -// } else { |
32 |
| -// holder.imageView.setImageResource(new Random().nextBoolean() ? R.drawable.ic_dragon_body_1 : R.drawable.ic_dragon_body_2); |
33 |
| -// } |
34 |
| - holder.imageView.setImageResource(R.drawable.ic_j20); |
35 |
| - holder.rootView.setOnClickListener(v -> { |
36 |
| - toast.setText("点击了: " + holder.textView.getText().toString()); |
37 |
| - toast.show(); |
| 70 | + public void onBindViewHolder(@NonNull ViewHolder holder, int position) { |
| 71 | + switch (mCurrentType) { |
| 72 | + case TYPE_CARD: |
| 73 | + initCardHolder(holder); |
| 74 | + break; |
| 75 | + case TYPE_J20: |
| 76 | + initJ20Holder(holder); |
| 77 | + break; |
| 78 | + case TYPE_DRAGON: |
| 79 | + initDragonHolder(holder, position); |
| 80 | + break; |
| 81 | + default: |
| 82 | + break; |
| 83 | + } |
| 84 | + holder.itemView.setOnClickListener(v -> { |
| 85 | + mToast.setText(String.format(Locale.getDefault(), "item %s clicked", holder.getAdapterPosition())); |
| 86 | + mToast.show(); |
38 | 87 | });
|
39 |
| - holder.textView.setText(position + ""); |
40 |
| - holder.textView.setBackgroundColor(mContext.getResources().getColor(position % 5 == 0 ? R.color.colorPrimary : R.color.colorAccent)); |
| 88 | + } |
| 89 | + |
| 90 | + private void initCardHolder(ViewHolder holder) { |
| 91 | + holder.imageView.getLayoutParams().width = 360; |
| 92 | + holder.itemView.requestLayout(); |
| 93 | + holder.imageView.setImageBitmap(getBitmap(mRandom.nextInt(3))); |
| 94 | + } |
| 95 | + |
| 96 | + private void initJ20Holder(ViewHolder holder) { |
| 97 | + holder.imageView.getLayoutParams().width = 270; |
| 98 | + holder.itemView.requestLayout(); |
| 99 | + holder.imageView.setImageBitmap(getBitmap(3)); |
| 100 | + } |
| 101 | + |
| 102 | + private void initDragonHolder(ViewHolder holder, int position) { |
| 103 | + holder.imageView.getLayoutParams().width = 135; |
| 104 | + holder.itemView.requestLayout(); |
| 105 | + if (position == 0) { |
| 106 | + holder.imageView.setImageBitmap(getBitmap(7)); |
| 107 | + } else if (position == mData.size() - 1) { |
| 108 | + holder.imageView.setImageBitmap(getBitmap(4)); |
| 109 | + } else { |
| 110 | + holder.imageView.setImageBitmap(getBitmap(mRandom.nextBoolean() ? 5 : 6)); |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + private Bitmap getBitmap(int index) { |
| 115 | + if (mBitmapList == null) { |
| 116 | + initBitmaps(); |
| 117 | + } |
| 118 | + Bitmap bitmap = mBitmapList.get(index).get(); |
| 119 | + return bitmap; |
| 120 | + } |
| 121 | + |
| 122 | + public void setType(int type) { |
| 123 | + mCurrentType = type; |
| 124 | + notifyDataSetChanged(); |
41 | 125 | }
|
42 | 126 |
|
43 | 127 | static class ViewHolder extends RecyclerView.ViewHolder {
|
44 | 128 |
|
45 |
| - View rootView; |
46 |
| - TextView textView; |
47 | 129 | ImageView imageView;
|
48 | 130 |
|
49 | 131 | public ViewHolder(View itemView) {
|
50 | 132 | super(itemView);
|
51 |
| - textView = itemView.findViewById(R.id.text_view); |
52 |
| - rootView = itemView.findViewById(R.id.root_view); |
53 | 133 | imageView = itemView.findViewById(R.id.image);
|
54 | 134 | }
|
55 | 135 | }
|
|
0 commit comments