在Android自定义listview中,如果我们有Checkbox那些按钮,会经常出现上下滑动的选中项没有被选中了(或者字体颜色上下滑动也会变化)。出现这种情况主要原因是上下滑动时候没有把视图保存下来。下面给出一个Adapter,教你解决这种问题。
import java.util.ArrayList;
import cn.ingenic.indroidsync.R;
import android.annotation.SuppressLint;
import android.app.ActionBar.LayoutParams;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.graphics.Color;
import android.widget.TextSwitcher;
import java.util.Map;
import java.util.HashMap;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
@SuppressLint("NewApi")
public class NotificationAdapter extends ArrayAdapter<AppItem>{ private final Context mContext;
private final ArrayList<AppItem> mNotificationList;
private Map<Integer, View> map = new HashMap<Integer, View>();
private int mUnReadListLength = 0;
public static class ListViewHolder {
public ImageView notificationIcon;
public TextView notificationTitle; public TextView notifierContent; }
public NotificationAdapter(Context context, ArrayList<AppItem> mAppList, int mUnReadListLength) {
super(context, R.layout.notification_item, mAppList);
this.mContext = context;
this.mNotificationList = mAppList;
this.mUnReadListLength = mUnReadListLength;
} @Override
public View getView(int position, View rowView, ViewGroup parent) {
ListViewHolder viewHolder = null;
if (map.size() < mNotificationList.size()) {
if (!map.containsKey(position)) {
你具体做的事情
map.put(position, rowView);
} else{
rowView = map.get(position);
}
} else {
rowView = map.get(position);
}
return rowView;
}