admin
2021-05-15 2aead6275fdd1bbbd778abc0e85663a2578fab06
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
package com.tejia.lijin.app.ui.recommend;
 
import android.content.Context;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
 
import com.bumptech.glide.Glide;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer;
import com.tejia.lijin.app.R;
import com.tejia.lijin.app.entity.SpecialOffer2;
 
import java.util.List;
 
/**
 * Created by weikou2015 on 2017/12/5.
 */
 
public class RecommendImgAdapter2 extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private RecyclerView mRecyclerView;
 
    private List<SpecialOffer2> mList;
    private Context mContext;
 
    private View VIEW_FOOTER;
    private View VIEW_HEADER;
 
    //Type
    private int TYPE_HEADER = 1001;
    private int TYPE_FOOTER = 1002;
    private int TYPE_SIMPLE_IMG = 1004;
 
    private DisplayImageOptions options;
 
    public RecommendImgAdapter2(Context context, List<SpecialOffer2> list) {
        this.mList = list;
        this.mContext = context;
        this.options = new DisplayImageOptions.Builder()
                .showImageForEmptyUri(R.drawable.ic_goods_default)
                .showImageOnFail(R.drawable.ic_goods_default)
                .showImageOnLoading(R.drawable.ic_goods_default)
                .resetViewBeforeLoading(true).cacheInMemory(true)
                .cacheOnDisk(true).imageScaleType(ImageScaleType.EXACTLY)
                .considerExifParams(true)
                .displayer(new FadeInBitmapDisplayer(300)).build();
    }
 
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == TYPE_FOOTER) {
            return new MyHolder(VIEW_FOOTER);
        } else if (viewType == TYPE_HEADER) {
            return new MyHolder(VIEW_HEADER);
        } else {
            View view = LayoutInflater.from(mContext).inflate(R.layout.item_recommend_activity, parent, false);
            MyHolder holder = new MyHolder(view);
            return holder;
        }
    }
 
    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder cHolder, int position) {
        MyHolder holder = (MyHolder) cHolder;
        if (!isHeaderView(position) && !isFooterView(position)) {
            SpecialOffer2 info;
            if (haveHeaderView()) {
                info = mList.get(position - 1);
            } else if (haveFooterView() && ((haveHeaderView() && position == mList.size() + 1) ||
                    !haveHeaderView() && position == mList.size())) {
                return;
            } else {
                info = mList.get(position);
            }
            Glide.with(mContext).load(info.getPicture()).into(holder.iv_activity);
        }
    }
 
    @Override
    public int getItemCount() {
        int count = (mList == null ? 0 : mList.size());
        if (VIEW_FOOTER != null) {
            count++;
        }
 
        if (VIEW_HEADER != null) {
            count++;
        }
        return count;
    }
 
    @Override
    public int getItemViewType(int position) {
        if (isHeaderView(position)) {
            return TYPE_HEADER;
        } else if (isFooterView(position)) {
            return TYPE_FOOTER;
        } else {
            return TYPE_SIMPLE_IMG;
        }
    }
 
    @Override
    public void onAttachedToRecyclerView(RecyclerView recyclerView) {
        try {
            if (mRecyclerView == null && mRecyclerView != recyclerView) {
                mRecyclerView = recyclerView;
            }
            ifGridLayoutManager();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
    private View getLayout(int layoutId) {
        return LayoutInflater.from(mContext).inflate(layoutId, null);
    }
 
    public void addHeaderView(View headerView) {
        if (haveHeaderView()) {
            throw new IllegalStateException("hearview has already exists!");
        } else {
            //避免出现宽度自适应
            ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            headerView.setLayoutParams(params);
            VIEW_HEADER = headerView;
            ifGridLayoutManager();
            notifyItemInserted(0);
        }
    }
 
    public void addFooterView(View footerView) {
        if (haveFooterView()) {
            throw new IllegalStateException("footerView has already exists!");
        } else {
            ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            footerView.setLayoutParams(params);
            VIEW_FOOTER = footerView;
            ifGridLayoutManager();
            notifyItemInserted(getItemCount() - 1);
        }
    }
 
    private void ifGridLayoutManager() {
        if (mRecyclerView == null) return;
        final RecyclerView.LayoutManager layoutManager = mRecyclerView.getLayoutManager();
        if (layoutManager instanceof GridLayoutManager) {
            ((GridLayoutManager) layoutManager).setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
                @Override
                public int getSpanSize(int position) {
                    if (isHeaderView(position) || isFooterView(position)) {
                        return ((GridLayoutManager) layoutManager).getSpanCount();
                    } else if (position <= 1) {
                        return ((GridLayoutManager) layoutManager).getSpanCount() / 2;
                    } else {
                        return ((GridLayoutManager) layoutManager).getSpanCount() / 3;
                    }
                }
            });
        }
    }
 
    private boolean haveHeaderView() {
        return VIEW_HEADER != null;
    }
 
    public boolean haveFooterView() {
        return VIEW_FOOTER != null;
    }
 
    private boolean isHeaderView(int position) {
        return haveHeaderView() && position == 0;
    }
 
    private boolean isFooterView(int position) {
        return haveFooterView() && position >= getItemCount() - 1;
    }
 
 
    public class MyHolder extends RecyclerView.ViewHolder {
        ImageView iv_activity;
 
        public MyHolder(View view) {
            super(view);
            iv_activity = view.findViewById(R.id.iv_activity);
        }
    }
}