admin
2020-09-10 44e43897c0f490ea275b386bf9399640aa3200d7
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
package com.weikou.wpc.superad.banner.adapter;
 
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
 
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.transition.Transition;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.weikou.wpc.superad.BrowserActivity;
import com.weikou.wpc.superad.api.BannerAPI;
import com.weikou.wpc.superad.api.BannerConstant;
import com.weikou.wpc.superad.banner.Superbanner;
import com.weikou.wpc.superad.banner.entity.BannerContent;
import com.weikou.wpc.superad.service.DownLoadFileService;
import com.weikou.wpc.superad.util.PxUtils;
import com.weikou.wpc.superad.widget.BannerLayout;
 
import java.util.HashMap;
import java.util.List;
 
public class BannerAdapter extends PagerAdapter {
 
    HashMap<String, String> map = new HashMap<>();
    private Context context;
    private List<BannerContent> list;
    private boolean delete;
 
    public BannerAdapter(List<BannerContent> list, Context context,
                         boolean delete, int height) {
        super();
        this.delete = delete;
        this.context = context;
        this.list = list;
        bannerHeight = height;
    }
 
    @Override
    public int getCount() {
        return list == null ? 0 : list.size();
    }
 
    public void getPosition() {
 
    }
 
    public void setBannerHeight(int height) {
        bannerHeight = height;
    }
 
    private int bannerHeight = 0;
 
    @Override
    public Object instantiateItem(final ViewGroup container, final int position) {
        final BannerLayout bannerView = new BannerLayout(container.getContext());
        BannerContent info = list.get(position);
        final BannerContent mInfo = info;
        if (info.getType() == 1) {
            if (bannerHeight > 0) {
                RelativeLayout.LayoutParams params2 = new RelativeLayout.LayoutParams(
                        ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
                bannerView.iv_banner_full_img.setLayoutParams(params2);
            } else {
                RelativeLayout.LayoutParams params2 = new RelativeLayout.LayoutParams(
                        ViewGroup.LayoutParams.MATCH_PARENT, PxUtils.dpToPx(60,
                        context));
                bannerView.iv_banner_full_img.setLayoutParams(params2);
            }
            bannerView.ll_img_and_text.setVisibility(View.GONE);
            bannerView.rl_only_img.setVisibility(View.VISIBLE);
            if (delete) {
                bannerView.iv_banner_close.setVisibility(View.VISIBLE);
            } else {
                bannerView.iv_banner_close.setVisibility(View.GONE);
            }
//            SimpleTarget<Bitmap> target = new SimpleTarget<Bitmap>() {
//                @Override
//                public void onResourceReady(
//                        Bitmap bitmap,
//                        @SuppressWarnings("rawtypes") GlideAnimation glideAnimation) {
//                    bannerView.iv_banner_full_img.setImageBitmap(bitmap);
//                }
//            };
            try {
                if (info.getImg().contains("http:")) {
//                    Glide.with(context).load(info.getImg() + "").asBitmap().fitCenter()
//                            .into(target);
                    ImageLoader.getInstance().displayImage(info.getImg() + "", bannerView.iv_banner_full_img);
                } else {
//                    Glide.with(context)
//                            .load(BannerConstant.HOST + info.getImg() + "")
//                            .asBitmap().fitCenter().into(target);
                    ImageLoader.getInstance().displayImage(BannerConstant.HOST + info.getImg() + "", bannerView.iv_banner_full_img);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else if (info.getType() == 2) {
            bannerView.ll_img_and_text.setVisibility(View.VISIBLE);
            bannerView.rl_only_img.setVisibility(View.GONE);
            if (delete) {
                bannerView.iv_banner_close.setVisibility(View.VISIBLE);
            } else {
                bannerView.iv_banner_close.setVisibility(View.GONE);
            }
            // Glide.with(context).load(info.getIcon() + "").crossFade()
            // .into(bannerView.iv_img);
            bannerView.tv_title.setText(info.getTitle() + "");
            bannerView.tv_description.setText(info.getDesc());
            if (!(info.getDownimg() != null || info.getDownimg().length() == 0)) {
                bannerView.iv_banner_download.setVisibility(View.VISIBLE);
                Glide.with(context).load(info.getDownimg())
                        .into(bannerView.iv_banner_download);
            } else {
                bannerView.iv_banner_download.setVisibility(View.GONE);
            }
 
 
            SimpleTarget<Drawable> target = new SimpleTarget<Drawable>() {
 
                @Override
                public void onResourceReady(@NonNull Drawable resource, @Nullable Transition<? super Drawable> transition) {
                    bannerView.iv_img.setImageDrawable(resource);
                }
            };
            Glide.with(context).load(info.getImg() + "").into(target);
        }
 
        bannerView.setOnClickListener(new OnClickListener() {
 
            @Override
            public void onClick(View arg0) {
                if (mInfo.isNotify()) {
                    DialogInterface.OnClickListener dialogOnclicListener = new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
 
                            switch (which) {
                                case Dialog.BUTTON_POSITIVE:
                                    if (map.get(mInfo.getId()) == null
                                            || map.get(mInfo.getId()) == "") {
                                        updateInfo("3", mInfo);
                                        map.put(mInfo.getId(), "asda");
                                    }
                                    if (mInfo.getLinkType() == 1) {// ����
                                        try {
                                            Intent intent = new Intent(context,
                                                    DownLoadFileService.class);
                                            context.stopService(intent);
                                        } catch (Exception e) {
                                        }
                                        try {
                                            Bundle bundle = new Bundle();
                                            bundle.putString("downloadurl",
                                                    mInfo.getLinkUrl());
                                            Intent intent = new Intent(context,
                                                    DownLoadFileService.class);
                                            intent.putExtras(bundle);
                                            context.startService(intent);
                                        } catch (Exception e) {
                                        }
                                    } else if (mInfo.getLinkType() == 2) {// ��ҳ
                                        Intent intent = new Intent(context,
                                                BrowserActivity.class);
                                        intent.putExtra("url", mInfo.getLinkUrl());
                                        context.startActivity(intent);
                                    }
                                    break;
                                case Dialog.BUTTON_NEGATIVE:
                                    break;
                            }
                        }
                    };
                    // dialog参数设置
                    AlertDialog.Builder builder = new AlertDialog.Builder(
                            context); // 先得到构造器
                    builder.setTitle("插件下载提示"); // 设置标题
                    builder.setMessage("是否确认下载插件?"); // 设置内容
                    builder.setPositiveButton("确认", dialogOnclicListener);
                    builder.setNegativeButton("取消", dialogOnclicListener);
                    builder.create().show();
                } else {
 
                    if (map.get(mInfo.getId()) == null
                            || map.get(mInfo.getId()) == "") {
                        updateInfo("3", mInfo);
                        map.put(mInfo.getId(), "asda");
                    }
                    if (mInfo.getLinkType() == 1) {// ����
                        try {
                            Intent intent = new Intent(context,
                                    DownLoadFileService.class);
                            context.stopService(intent);
                        } catch (Exception e) {
                        }
                        try {
                            Bundle bundle = new Bundle();
                            bundle.putString("downloadurl", mInfo.getLinkUrl());
                            Intent intent = new Intent(context,
                                    DownLoadFileService.class);
                            intent.putExtras(bundle);
                            context.startService(intent);
                        } catch (Exception e) {
                        }
                    } else if (mInfo.getLinkType() == 2) {// ��ҳ
                        Intent intent = new Intent(context,
                                BrowserActivity.class);
                        intent.putExtra("url", mInfo.getLinkUrl());
                        context.startActivity(intent);
                    }
                }
            }
        });
        bannerView.iv_banner_close.setOnClickListener(new OnClickListener() {
 
            @Override
            public void onClick(View arg0) {
                container.removeAllViews();
            }
        });
 
        container.addView(bannerView);
        return bannerView;
    }
 
    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
    }
 
    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }
 
    @Override
    public CharSequence getPageTitle(int position) {
        return "";
        // return mHomeAds.get(position).getTitle();
    }
 
    /**
     * 更新网络请求
     */
    private void updateInfo(String type, BannerContent info) {
        BannerAPI.updateInfo(context, type, info.getId(), Superbanner.bannerid,
                new JsonHttpResponseHandler());
    }
}