zhuguifei
2025-12-31 a5a64b0bdc4e90e265cbff767c09351ea82f113a
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
/*
 * FileName: BaseArrayListAdapter.java
 * Copyright (C) 2014 Plusub Tech. Co. Ltd. All Rights Reserved <admin@plusub.com>
 * 
 * Licensed under the Plusub License, Version 1.0 (the "License");
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * author  : quhao <blakequ@gmail.com>
 * date     : 2014-6-7 下午9:50:52
 * last modify author :
 * version : 1.0
 */
package com.shlb.comb.adapter;
 
import android.content.Context;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.BaseAdapter;
 
import java.util.ArrayList;
import java.util.List;
 
/**
 * 数组适配器,只需要实现 {@link #getView(int, View, ViewGroup)}
 * @author blakequ Blakequ@gmail.com
 *
 * @param <T>
 */
public abstract class BaseArrayListAdapter<T> extends BaseAdapter {
 
    protected Context mContext;
    protected LayoutInflater mInflater;
    private List<T> mDatas;
    /**屏幕的宽度*/
    protected int mScreenWidth;
    /**屏幕高度*/
    protected int mScreenHeight;
    /**屏幕密度*/
    protected float mDensity;
    
    public BaseArrayListAdapter(Context context){
        mContext = context;
        mInflater = LayoutInflater.from(context);
        mDatas = new ArrayList<T>();
        DisplayMetrics metric = new DisplayMetrics();
        WindowManager wm = (WindowManager)context.getSystemService(Context.WINDOW_SERVICE);
        wm.getDefaultDisplay().getMetrics(metric);
        mScreenWidth = metric.widthPixels;
        mScreenHeight = metric.heightPixels;
        mDensity = metric.density;
    }
    
    public BaseArrayListAdapter(Context context, List<T> datas){
        mContext = context;
        mInflater = LayoutInflater.from(context);
        if (datas != null && datas.size() > 0) {
            mDatas = datas;
        }else{
            mDatas = new ArrayList<T>();
        }
    }
    
    /**
     * get context
     * <p>Title: getContext
     * <p>Description: 
     * @return
     */
    public Context getContext() {
        return mContext;
    }
 
    public LayoutInflater getInflater() {
        return mInflater;
    }
 
    /**
     * get screen width
     * <p>Title: getScreenWidth
     * <p>Description: 
     * @return
     */
    public int getScreenWidth() {
        return mScreenWidth;
    }
 
    /**
     * get screen height
     * <p>Title: getScreenHeight
     * <p>Description: 
     * @return
     */
    public int getScreenHeight() {
        return mScreenHeight;
    }
 
    /**
     * return all data
     * <p>Title: getAllData
     * <p>Description: 
     * @return
     */
    public List<T> getAllData(){
        return mDatas;
    }
    
    /**
     * add data to head
     * <p>Title: addHead
     * <p>Description: 
     * @param datas
     */
    public void addHead(T datas){
        if (datas != null) {
            mDatas.add(0, datas);
            notifyDataSetChanged();
        }
    }
    
    /**
     * add data list to head
     * <p>Title: addHead
     * <p>Description: 
     * @param datas
     */
    public void addHead(List<T> datas){
        if (datas != null) {
            for (int i = 0; i < datas.size(); i++) {
                mDatas.add(i, datas.get(i));
            }
            notifyDataSetChanged();
        }
    }
    
    
    public void add(T datas){
        if (datas != null) {
            mDatas.add(datas);
            notifyDataSetChanged();
        }
    }
    
    public void add(int position, T datas){
        if (position < 0 || position >= mDatas.size() || datas == null) {
            return;
        }
        mDatas.add(position, datas);
        notifyDataSetChanged();
    }
    
    /**
     * 更新指定位置数据
     * <p>Title: update
     * <p>Description: 
     * @param position
     * @param datas
     */
    public void update(int position, T datas){
        if (position < 0 || position >= mDatas.size() || datas == null) {
            return;
        }
        mDatas.remove(position);
        mDatas.add(position, datas);
        notifyDataSetChanged();
    }
    
    /**
     * add data to adapter
     * <p>Title: addAll
     * <p>Description: 
     * @param datas
     */
    public void addAll(List<T> datas){
        if (datas != null) {
            mDatas.addAll(datas);
            notifyDataSetChanged();
        }
    }
    
    /**
     * refresh data to adapter, history data will be remove
     * <p>Title: refreshData
     * <p>Description: 
     * @param datas
     */
    public void refreshData(List<T> datas){
        if (datas != null) {
            mDatas.clear();
            mDatas.addAll(datas);
            notifyDataSetChanged();
        }
    }
    
    /**
     * clear all data
     * <p>Title: clear
     * <p>Description:
     */
    public void clear(){
        mDatas.clear();
        notifyDataSetChanged();
    }
    
    /**
     * 删除指定位置数据
     * <p>Title: delete
     * <p>Description: 
     * @param position
     */
    public void delete(int position){
        if (position < 0 || position >= mDatas.size()) {
            return;
        }
        mDatas.remove(position);
        notifyDataSetChanged();
    }
    
    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return mDatas.size();
    }
 
    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        if (mDatas == null || mDatas.size() <= 0 || position < 0 || position >= mDatas.size()) {
            return null;
        }
        return mDatas.get(position);
    }
 
    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }
 
}