|
| 1 | +package jp.fluct.sample.samplefluctsdkapp; |
| 2 | + |
| 3 | +import android.content.Context; |
| 4 | +import android.os.Bundle; |
| 5 | +import android.support.annotation.NonNull; |
| 6 | +import android.support.annotation.Nullable; |
| 7 | +import android.support.v4.app.Fragment; |
| 8 | +import android.view.LayoutInflater; |
| 9 | +import android.view.View; |
| 10 | +import android.view.ViewGroup; |
| 11 | +import android.widget.ArrayAdapter; |
| 12 | +import android.widget.ImageView; |
| 13 | +import android.widget.ListView; |
| 14 | +import android.widget.TextView; |
| 15 | + |
| 16 | +import java.util.ArrayList; |
| 17 | +import java.util.List; |
| 18 | + |
| 19 | +import jp.fluct.fluctsdk.FluctAdBanner; |
| 20 | + |
| 21 | +public class ContentsListFragment extends Fragment { |
| 22 | + |
| 23 | + private ListView mContentListView; |
| 24 | + |
| 25 | + public ContentsListFragment() { |
| 26 | + } |
| 27 | + |
| 28 | + @Override |
| 29 | + public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { |
| 30 | + View layout = inflater.inflate(R.layout.fragment_contents_list, container, false); |
| 31 | + mContentListView = (ListView) layout.findViewById(R.id.list); |
| 32 | + |
| 33 | + return layout; |
| 34 | + } |
| 35 | + |
| 36 | + @Override |
| 37 | + public void onActivityCreated(@Nullable Bundle savedInstanceState) { |
| 38 | + super.onActivityCreated(savedInstanceState); |
| 39 | + |
| 40 | + List<Object> listContents = generateContents(); |
| 41 | + ContentsListAdapter adapter = new ContentsListAdapter(getContext(), listContents); |
| 42 | + |
| 43 | + // Fluct広告を入れる |
| 44 | + FluctAdBanner.SizeType sizeType = FluctAdBanner.SizeType.BANNER; |
| 45 | + String mediaId = "0000005617"; |
| 46 | + adapter.insert(new Ad(sizeType, mediaId), 3); |
| 47 | + adapter.insert(new Ad(sizeType, mediaId), 12); |
| 48 | + adapter.insert(new Ad(sizeType, mediaId), 30); |
| 49 | + adapter.insert(new Ad(sizeType, mediaId), 55); |
| 50 | + adapter.insert(new Ad(sizeType, mediaId), 80); |
| 51 | + |
| 52 | + // リストへ反映 |
| 53 | + mContentListView.setAdapter(adapter); |
| 54 | + } |
| 55 | + |
| 56 | + // リストコンテンツの作成 |
| 57 | + private List<Object> generateContents(){ |
| 58 | + List<Object> contents = new ArrayList<>(); |
| 59 | + for (int i = 0; i < 100; i++) { |
| 60 | + Content content = new Content(i); |
| 61 | + content.name = "item " + (i + 1); |
| 62 | + contents.add(content); |
| 63 | + } |
| 64 | + return contents; |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * コンテンツのモデル |
| 69 | + */ |
| 70 | + private static class Content { |
| 71 | + final int identity; |
| 72 | + String name; |
| 73 | + |
| 74 | + Content(int id) { |
| 75 | + this.identity = id; |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * 広告モデル |
| 81 | + */ |
| 82 | + private static class Ad { |
| 83 | + final FluctAdBanner.SizeType sizeType; |
| 84 | + final String mediaId; |
| 85 | + |
| 86 | + Ad(FluctAdBanner.SizeType sizeType, String mediaId) { |
| 87 | + this.sizeType = sizeType; |
| 88 | + this.mediaId = mediaId; |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + private static class ContentsListAdapter extends ArrayAdapter<Object> { |
| 93 | + |
| 94 | + private final LayoutInflater mInflater; |
| 95 | + |
| 96 | + public ContentsListAdapter(Context context, List<Object> contents) { |
| 97 | + super(context, R.layout.item_list_content, contents); |
| 98 | + mInflater = LayoutInflater.from(context); |
| 99 | + } |
| 100 | + |
| 101 | + @NonNull |
| 102 | + @Override |
| 103 | + public View getView(int position, View convertView, ViewGroup parent) { |
| 104 | + Object content = getItem(position); |
| 105 | + |
| 106 | + int itemViewType = getItemViewType(position); |
| 107 | + ViewType viewType = ViewType.find(itemViewType); |
| 108 | + switch (viewType) { |
| 109 | + case CONTENT: |
| 110 | + convertView = getViewForContent((Content) content, position, convertView, parent); |
| 111 | + break; |
| 112 | + case AD: |
| 113 | + convertView = getViewForFluctAd((Ad) content, position, convertView, parent); |
| 114 | + break; |
| 115 | + } |
| 116 | + |
| 117 | + return convertView; |
| 118 | + } |
| 119 | + |
| 120 | + private View getViewForContent(Content content, int position, View convertView, ViewGroup parent) { |
| 121 | + ContentViewHolder holder; |
| 122 | + if (convertView == null) { |
| 123 | + convertView = mInflater.inflate(R.layout.item_list_content, null); |
| 124 | + holder = new ContentViewHolder(); |
| 125 | + holder.index = (TextView) convertView.findViewById(R.id.list_index); |
| 126 | + holder.name = (TextView) convertView.findViewById(R.id.list_name); |
| 127 | + holder.thumbnail = (ImageView) convertView.findViewById(R.id.list_thumbnail); |
| 128 | + convertView.setTag(holder); |
| 129 | + } else { |
| 130 | + holder = (ContentViewHolder) convertView.getTag(); |
| 131 | + } |
| 132 | + |
| 133 | + // リスト番号 |
| 134 | + holder.index.setText(String.valueOf(position + 1)); |
| 135 | + |
| 136 | + // 項目名 |
| 137 | + holder.name.setText(content.name); |
| 138 | + |
| 139 | + return convertView; |
| 140 | + } |
| 141 | + |
| 142 | + private View getViewForFluctAd(Ad ad, int position, View convertView, ViewGroup parent) { |
| 143 | + FluctAdBanner adView; |
| 144 | + if (convertView == null) { |
| 145 | + // 初回作成 |
| 146 | + adView = new FluctAdBanner(getContext()); |
| 147 | + adView.getViewSettings().setSizeType(ad.sizeType); |
| 148 | + adView.getViewSettings().setMediaId(ad.mediaId); |
| 149 | + adView.setTag(ad); |
| 150 | + |
| 151 | + } else { |
| 152 | + // 広告の再利用 |
| 153 | + adView = (FluctAdBanner) convertView; |
| 154 | + Ad preListAd = (Ad) adView.getTag(); |
| 155 | + |
| 156 | + if (preListAd != ad) { |
| 157 | + // リサイクルされた広告と違う広告を表示させたい場合は再生成する |
| 158 | + adView = new FluctAdBanner(getContext()); |
| 159 | + adView.getViewSettings().setSizeType(ad.sizeType); |
| 160 | + adView.getViewSettings().setMediaId(ad.mediaId); |
| 161 | + adView.setTag(ad); |
| 162 | + } |
| 163 | + } |
| 164 | + |
| 165 | + return adView; |
| 166 | + } |
| 167 | + |
| 168 | + |
| 169 | + @Override |
| 170 | + public int getViewTypeCount() { |
| 171 | + return ViewType.values().length; |
| 172 | + } |
| 173 | + |
| 174 | + @Override |
| 175 | + public int getItemViewType(int position) { |
| 176 | + Object content = getItem(position); |
| 177 | + if (content instanceof Ad) { |
| 178 | + // 広告 |
| 179 | + return ViewType.AD.id; |
| 180 | + |
| 181 | + } else { |
| 182 | + // デフォルト |
| 183 | + return ViewType.CONTENT.id; |
| 184 | + |
| 185 | + } |
| 186 | + } |
| 187 | + |
| 188 | + private enum ViewType { |
| 189 | + CONTENT(0), AD(1); |
| 190 | + |
| 191 | + final int id; |
| 192 | + |
| 193 | + ViewType(int id) { |
| 194 | + this.id = id; |
| 195 | + } |
| 196 | + |
| 197 | + static ViewType find(int id) { |
| 198 | + for(ViewType vt: values()){ |
| 199 | + if(vt.id == id){ |
| 200 | + return vt; |
| 201 | + } |
| 202 | + } |
| 203 | + return CONTENT; |
| 204 | + } |
| 205 | + } |
| 206 | + |
| 207 | + private static class ContentViewHolder { |
| 208 | + TextView index; |
| 209 | + TextView name; |
| 210 | + ImageView thumbnail; |
| 211 | + } |
| 212 | + } |
| 213 | +} |
0 commit comments