質問するログイン新規登録

質問編集履歴

1

Adapterクラスのコードを追記しました。

2018/07/07 14:23

投稿

dfrd
dfrd

スコア14

title CHANGED
File without changes
body CHANGED
@@ -21,6 +21,163 @@
21
21
 
22
22
  まだまだ、初心者で申し訳ありませんが、ご教授をお願い致します。
23
23
 
24
+ 以下に、Adapterのソースコードを記載させて頂きます。
24
25
 
26
+ ```ここに言語を入力
27
+ namespace SampleArticleMemberApp.Droid
28
+ {
29
+ public class ArticleListItemAdapter : BaseAdapter<ArticleListItem>
30
+ {
31
+ public List<ArticleListItem> items;
32
+ public Activity context;
33
+
34
+ /**
35
+ * コンストラクタ
36
+ */
37
+ public ArticleListItemAdapter(Activity context, List<ArticleListItem> items)
38
+ {
39
+ Debug.Log(Debug.DEBUG_LEVEL_VERBOSE, "ArticleListItemAdapter", "-----START-----");
40
+ this.context = context;
41
+ this.items = items;
42
+ Debug.Log(Debug.DEBUG_LEVEL_VERBOSE, "ArticleListItemAdapter", "-----END-----");
43
+ }
44
+
45
+ /**
46
+ * クリック処理
47
+ */
48
+ public override long GetItemId(int position)
49
+ {
50
+ Debug.Log(Debug.DEBUG_LEVEL_VERBOSE, "GetItemId", "-----START-----");
51
+ Debug.Log(Debug.DEBUG_LEVEL_VERBOSE, "GetItemId", "-----END-----");
52
+ return position;
53
+ }
54
+
55
+ public override ArticleListItem this[int position]
56
+ {
57
+ get { return items[position]; }
58
+ }
59
+
60
+ public override int Count
61
+ {
62
+ get { return items.Count; }
63
+ }
64
+
65
+ public override View GetView(int position, View convertView, ViewGroup parent)
66
+ {
67
+ Debug.Log(Debug.DEBUG_LEVEL_VERBOSE, "GetView", "-----START-----");
68
+ var item = items[position];
69
+
70
+ View view = convertView;
71
+ if (view == null){
72
+ view = context.LayoutInflater.Inflate(Resource.Layout.ArticleListViewItem, null);
73
+ }
74
+
75
+ // BaseAdapter<T>の対応するプロパティを割り当て
76
+ view.FindViewById<TextView>(Resource.Id.articleItemDate).Text = item.date;
77
+ view.FindViewById<TextView>(Resource.Id.articleItemTitle).Text = item.title;
78
+
79
+ Debug.Log(Debug.DEBUG_LEVEL_INFO, "GetView", item.title);
80
+ Debug.Log(Debug.DEBUG_LEVEL_INFO, "GetView", item.url);
81
+ Debug.Log(Debug.DEBUG_LEVEL_INFO, "GetView", item.thumbUrl);
82
+ Debug.Log(Debug.DEBUG_LEVEL_INFO, "GetView", "readable: " +item.readable.ToString());
83
+
84
+ if(item.thumbUrl != ""){
85
+ Debug.Log(Debug.DEBUG_LEVEL_INFO, "GetView", "画像を読み込みます");
86
+ view.FindViewById<ImageView>(Resource.Id.articleItemThumb).SetImageResource(Resource.Mipmap.def_thumb);
87
+ SetThumb(view, position);
88
+ //var image = GetImageBitmapFromUrl(item.thumbUrl);
89
+ //view.FindViewById<ImageView>(Resource.Id.articleItemThumb).SetImageBitmap(image);
90
+ }
91
+ else{
92
+ view.FindViewById<ImageView>(Resource.Id.articleItemThumb).SetImageResource(Resource.Mipmap.def_thumb);
93
+ }
94
+
95
+ //読み込み不可能ならば、背景色を変える
96
+ if(!item.readable){
97
+ view.SetBackgroundColor(Color.DarkGray);
98
+ view.FindViewById<TextView>(Resource.Id.articleItemDate).SetTextColor(Color.GhostWhite);
99
+ view.FindViewById<TextView>(Resource.Id.articleItemDate).Alpha = 0.4f;
100
+
101
+ view.FindViewById<TextView>(Resource.Id.articleItemTitle).SetTextColor(Color.GhostWhite);
102
+ view.FindViewById<TextView>(Resource.Id.articleItemTitle).Alpha = 0.4f;
103
+
104
+ view.FindViewById<ImageView>(Resource.Id.articleItemThumb).Alpha = 0.4f;
105
+ }
106
+ else{
107
+ view.SetBackgroundColor(Color.GhostWhite);
108
+ view.FindViewById<TextView>(Resource.Id.articleItemDate).SetTextColor(Color.Black);
109
+ view.FindViewById<TextView>(Resource.Id.articleItemDate).Alpha = 1.0f;
110
+
111
+ view.FindViewById<TextView>(Resource.Id.articleItemTitle).SetTextColor(Color.Black);
112
+ view.FindViewById<TextView>(Resource.Id.articleItemTitle).Alpha = 1.0f;
113
+
114
+ view.FindViewById<ImageView>(Resource.Id.articleItemThumb).Alpha = 1.0f;
115
+ }
116
+
117
+
118
+ Debug.Log(Debug.DEBUG_LEVEL_VERBOSE, "GetView", "-----END-----");
119
+ return view;
120
+ }
121
+
122
+ /**
123
+ * URLから、Bitmap生成
124
+ */
125
+ private async Task<Bitmap> GetImageBitmapFromUrl(string url)
126
+ {
127
+ Bitmap imageBitmap = null;
128
+
129
+ try{
130
+ using (var webClient = new WebClient())
131
+ {
132
+ var imageBytes = webClient.DownloadData(url);
133
+ if (imageBytes != null && imageBytes.Length > 0)
134
+ {
135
+ imageBitmap = BitmapFactory.DecodeByteArray(imageBytes, 0, imageBytes.Length);
136
+ }
137
+ }
138
+
139
+ }
140
+ catch(Exception e)
141
+ {
142
+ Debug.Log(Debug.DEBUG_LEVEL_ERROR, "GetImageBitmapFromUrl", "e: " +e);
143
+ }
144
+ return imageBitmap;
145
+ }
146
+
147
+ private async void SetThumb(View view, int position)
148
+ {
149
+ var item = items[position];
150
+ if(items[position].bitmap == null)
151
+ {
152
+ ImageView imageView = view.FindViewById<ImageView>(Resource.Id.articleItemThumb);
153
+
154
+ Debug.Log(Debug.DEBUG_LEVEL_ERROR, "GetImageBitmapFromUrl", "imgvW: " + imageView.Width);
155
+ Debug.Log(Debug.DEBUG_LEVEL_ERROR, "GetImageBitmapFromUrl", "imgvH: " + imageView.Height);
156
+ if(0 < imageView.Width && 0 < imageView.Height){
157
+ Bitmap bmp = await GetImageBitmapFromUrl(item.thumbUrl);
158
+ int bmpH = (int)((float)imageView.Width / (float)bmp.Width *(float)bmp.Height);
159
+
160
+ int scale = Math.Max(imageView.Width, bmpH);
161
+
162
+ bmp = Bitmap.CreateScaledBitmap(bmp, imageView.Width, bmpH, false);
163
+ items[position].bitmap = await GetImageBitmapFromUrl(item.thumbUrl);
164
+ view.FindViewById<ImageView>(Resource.Id.articleItemThumb).SetImageBitmap(items[position].bitmap);
165
+ Debug.Log(Debug.DEBUG_LEVEL_ERROR, "GetImageBitmapFromUrl", "bmpW: " + bmp.Width);
166
+ Debug.Log(Debug.DEBUG_LEVEL_ERROR, "GetImageBitmapFromUrl", "bmpH: " + bmp.Height);
167
+
168
+ bmp.Recycle();
169
+ bmp = null;
170
+ }
171
+ }
172
+ else
173
+ {
174
+ view.FindViewById<ImageView>(Resource.Id.articleItemThumb).SetImageBitmap(items[position].bitmap);
175
+ }
176
+ }
177
+ }
178
+ }
179
+ ```
180
+
181
+
25
182
  以上です。
26
183
  よろしくお願い致します。