- convertViwe的复用
我们借助给view打tag的方式来复用放在内存中的view
@Override
public View getView(final int i, View view, ViewGroup viewGroup) {
item = getItem(i);
ViewHolder viewHolder;
if (view == null || view.getTag() == null) {
view = LayoutInflater.from(getContext()).inflate(R.layout.course_online_cell, null);
viewHolder = new ViewHolder();
viewHolder.tv_online_course_title = view.findViewById(tv_online_course_title);
viewHolder.tv_online_course_hot = view.findViewById(R.id.tv_online_course_hot);
viewHolder.iv_course_online_thumbnail = view.findViewById(R.id.iv_course_online_thumbnail);
ImageLoader.getInstance().displayImage(item.optString("thumbnail"), viewHolder.iv_course_online_thumbnail, displayImageOptions);
viewHolder.iv_course_online_thumbnail.setTag(item.optInt("courseId") + "");
view.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) view.getTag();
if (!viewHolder.iv_course_online_thumbnail.getTag().equals(item.optInt("courseId") + "")) {
ImageLoader.getInstance().displayImage(item.optString("thumbnail"), viewHolder.iv_course_online_thumbnail, displayImageOptions);
viewHolder.iv_course_online_thumbnail.setTag(item.optInt("courseId") + "");
}
}
viewHolder.tv_online_course_title.setText(item.optString("title"));
String enrollNumber = getContext().getResources().getString(R.string.enrollNumber);
viewHolder.tv_online_course_hot.setText(String.format(enrollNumber, item.optString("enrollNum")));
viewHolder.iv_course_online_thumbnail.setScaleType(ImageView.ScaleType.CENTER_CROP);
return view;
}
static class ViewHolder {
TextView tv_online_course_title;
TextViewNumber tv_online_course_hot;
ImageView iv_course_online_thumbnail;
}
- 滑动操作图片闪烁
如果是参考上面的代码重载的getView方法便不会出现滑动图片闪烁的问题,闪烁的原因就是我们的ImageView被重新加载了一遍(因为当ListView区域被滑动的时候,VisibleView发生了变化,会重新调用我们的getView方法,viewHolder会辅助我们对inflate出来的view打tag,那再重新调用getView的时候,如果这个view已经被打了tag就不必重新inflate,也不必findViewById去寻找布局UI控件,这是其中一个优化点,但是出现闪烁的原因了,每次getView我们都会重新渲染我们的图片,尽管调用的是缓存,但是只能保证这个闪烁是短暂的,当然如果没调用缓存图片,那就是较长时间的白屏了),那如何才能避免这一问题呢?那就是对ImageView也打一个tag,我们把核心代码抠出来,大家可以看一下。
ViewHolder viewHolder;
if (view == null || view.getTag() == null) {
view = LayoutInflater.from(getContext()).inflate(R.layout.course_online_cell, null);
viewHolder = new ViewHolder();
viewHolder.iv_course_online_thumbnail = view.findViewById(R.id.iv_course_online_thumbnail);
ImageLoader.getInstance().displayImage(item.optString("thumbnail"), viewHolder.iv_course_online_thumbnail, displayImageOptions);
viewHolder.iv_course_online_thumbnail.setTag(item.optInt("courseId") + "");
view.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) view.getTag();
if (!viewHolder.iv_course_online_thumbnail.getTag().equals(item.optInt("courseId") + "")) {
ImageLoader.getInstance().displayImage(item.optString("thumbnail"), viewHolder.iv_course_online_thumbnail, displayImageOptions);
viewHolder.iv_course_online_thumbnail.setTag(item.optInt("courseId") + "");
}
}
- 数据更新
由于从服务端拿到的数据是json形式,所以我放在BaseAdapter内的数据是JSONArray格式,上面其实已经暴露了一部分代码了,我们在这里只讲BaseAdapter内的数据(JSONArray list)如何更新?
因为我是从js转过来的,所以对js 中array丰富的api印象很深刻,在开发过程中是非常灵活的,但是当我看完JSONArray的api后感觉很不适应,基本满足不了开发需要,所以我写了一个JSONArray的工具类,基本涵盖了所有设计到的操作。
- add(JSONArray array, JSONObject object)
- add(JSONArray array1, JSONArray array2)
- add(int index, JSONArray array, JSONObject object)
- add(int index, JSONArray array1, JSONArray array2)
- replice(int index, int length, JSONArray list)
- replice(int index, int length, JSONArray list, JSONObject object)
- replice(int index, int length, JSONArray array1, JSONArray array2)
- reset(JSONArray list)
import android.util.Log;
import org.json.JSONArray;
import org.json.JSONObject;
public class JSONArrayHelper {
public static <T> JSONArray add(JSONArray jsonArray, T t) {
String type = t.getClass().getSimpleName();
switch (type) {
case "JSONObject":
jsonArray.put(t);
return jsonArray;
case "JSONArray":
return joinJSONArray((JSONArray) t, jsonArray);
default:
return jsonArray;
}
}
public static <T> JSONArray add(int index, JSONArray jsonArray, T t) {
String type = t.getClass().getSimpleName();
switch (type) {
case "JSONObject":
return joinJSONArray(index, jsonArray, (JSONObject) t);
case "JSONArray":
return joinJSONArray(index, jsonArray, (JSONArray) t);
default:
return jsonArray;
}
}
public static <T> JSONArray replice(int index, int length, JSONArray jsonArray) {
return repliceJSONArray(index, length, jsonArray);
}
public static <T> JSONArray replice(int index, int length, JSONArray jsonArray, T t) {
if (length == 0) return add(index, jsonArray, t);
String type = t.getClass().getSimpleName();
switch (type) {
case "JSONObject":
return repliceJSONArray(index, length, jsonArray, (JSONObject) t);
case "JSONArray":
return repliceJSONArray(index, length, jsonArray, (JSONArray) t);
default:
return jsonArray;
}
}
public static JSONArray reset (JSONArray jsonArray) {
return jsonArray;
}
public static JSONArray joinJSONArray (JSONArray array1, JSONArray array2) {
if (array1.length() == 0) return array2;
if (array2.length() == 0) return array1;
StringBuffer buffer = new StringBuffer();
try {
int len = array1.length();
for (int i = 0; i < len; i++) {
JSONObject obj1 = (JSONObject) array1.get(i);
if (i == len - 1)
buffer.append(obj1.toString());
else
buffer.append(obj1.toString()).append(",");
}
len = array2.length();
if (len > 0)
buffer.append(",");
for (int i = 0; i < len; i++) {
JSONObject obj1 = (JSONObject) array2.get(i);
if (i == len - 1)
buffer.append(obj1.toString());
else
buffer.append(obj1.toString()).append(",");
}
buffer.insert(0, "[").append("]");
return new JSONArray(buffer.toString());
} catch (Exception e) {
return new JSONArray();
}
}
public static JSONArray joinJSONArray (int index, JSONArray array1, JSONArray array2) {
if (array1.length() == 0) return array2;
if (array2.length() == 0) return array1;
StringBuffer buffer = new StringBuffer();
try {
int len1 = array1.length();
int len2 = array2.length();
if (index < len1) {
for (int i = 0; i < len1; i++) {
JSONObject obj1 = (JSONObject) array1.get(i);
if (i == index) {
for (int j = 0; j < len2; j++) {
JSONObject obj2 = (JSONObject) array2.get(j);
buffer.append(obj2.toString()).append(",");
}
if (i == len1 - 1)
buffer.append(obj1.toString());
else
buffer.append(obj1.toString()).append(",");
} else {
if (i == len1 - 1)
buffer.append(obj1.toString());
else
buffer.append(obj1.toString()).append(",");
}
}
} else {
for (int i = 0; i < len1; i++) {
JSONObject obj1 = (JSONObject) array1.get(i);
if (i == len1 - 1)
buffer.append(obj1.toString());
else
buffer.append(obj1.toString()).append(",");
}
if (len2 > 0)
buffer.append(",");
for (int j = 0; j < len2; j++) {
JSONObject obj2 = (JSONObject) array2.get(j);
if (j == len2 - 1)
buffer.append(obj2.toString());
else
buffer.append(obj2.toString()).append(",");
}
}
buffer.insert(0, "[").append("]");
return new JSONArray(buffer.toString());
} catch (Exception e) {
return new JSONArray();
}
}
public static JSONArray joinJSONArray (int index, JSONArray jsonArray, JSONObject jsonObject) {
int len = jsonArray.length();
if (len == 0 || index > len - 1) return jsonArray.put(jsonObject);
StringBuffer buffer = new StringBuffer();
try {
for (int i = 0; i < len; i++) {
JSONObject obj = (JSONObject) jsonArray.get(i);
if (i == index) {
buffer.append(jsonObject.toString()).append(",");
if (i == len - 1)
buffer.append(obj.toString());
else
buffer.append(obj.toString()).append(",");
} else {
if (i == len - 1)
buffer.append(obj.toString());
else
buffer.append(obj.toString()).append(",");
}
}
buffer.insert(0, "[").append("]");
return new JSONArray(buffer.toString());
} catch (Exception e) {
return new JSONArray();
}
}
public static JSONArray repliceJSONArray(int index, int length, JSONArray jsonArray) {
int len = jsonArray.length();
if (len == 0 || index >= length) return jsonArray;
StringBuffer buffer = new StringBuffer();
try {
for (int i = 0; i < len; i++) {
JSONObject obj1 = (JSONObject) jsonArray.get(i);
if (i < index || i > index + length -1) {
if (i == len - 1)
buffer.append(obj1.toString());
else
buffer.append(obj1.toString()).append(",");
}
}
buffer.insert(0, "[").append("]");
Log.e("JSONArrayHelper", buffer.toString());
return new JSONArray(buffer.toString());
} catch (Exception e) {
return new JSONArray();
}
}
public static JSONArray repliceJSONArray(int index, int length, JSONArray jsonArray, JSONObject jsonObject) {
int len = jsonArray.length();
if (len == 0 || index > len - 1) return jsonArray.put(jsonObject);
StringBuffer buffer = new StringBuffer();
try {
for (int i = 0; i < len; i++) {
JSONObject obj = (JSONObject) jsonArray.get(i);
if (i >= index && i <= index + length - 1) {
if (i == index) {
buffer.append(jsonObject.toString());
if (i < len -1 && index + length - 1 < len - 1) buffer.append(",");
}
} else {
if (i == len - 1)
buffer.append(obj.toString());
else
buffer.append(obj.toString()).append(",");
}
}
buffer.insert(0, "[").append("]");
return new JSONArray(buffer.toString());
} catch (Exception e) {
return new JSONArray();
}
}
public static JSONArray repliceJSONArray(int index, int length, JSONArray array1, JSONArray array2) {
int len1 = array1.length();
int len2 = array2.length();
if (len2 == 0) return array1;
if (len1 == 0 || index > len1 - 1) return add(array1, array2);
StringBuffer buffer = new StringBuffer();
try {
for (int i = 0; i < len1; i++) {
JSONObject obj1 = (JSONObject) array1.get(i);
if (i >= index && i <= index + length - 1) {
if (i == index)
for (int j = 0; j < len2; j++) {
JSONObject obj2 = (JSONObject) array2.get(j);
if (j == len2 - 1 && index + length - 1 >= len1 - 1)
buffer.append(obj2.toString());
else
buffer.append(obj2.toString()).append(",");
}
} else {
if (i == len1 - 1)
buffer.append(obj1.toString());
else
buffer.append(obj1.toString()).append(",");
}
}
buffer.insert(0, "[").append("]");
Log.e("JSONArrayHelper", buffer.toString());
return new JSONArray(buffer.toString());
} catch (Exception e) {
return new JSONArray();
}
}
}
- 下拉刷新
为保证用户体验,正在加动画处理,待续
- 上拉加载
为保证用户体验,正在加动画处理,待续