我的列表视图包含TextView
和ImageView
,在第一个图片视图为INVISIBLE
时,点击每个项目VISIBLE
这里是我的适配器:< / p>
public class ContentViewrAdaptor extends BaseAdapter implements Filterable {
private List<String> originalData = null;
private List<String> filteredData = null;
private LayoutInflater mInflater;
private Typeface regular;
private Context context;
private ItemFilter mFilter = new ItemFilter();
public ContentViewrAdaptor(Context context, List<String> data, Typeface regular) {
this.filteredData = data;
this.originalData = data;
this.regular = regular;
this.context = context;
mInflater = LayoutInflater.from(context);
}
public int getCount() {
return filteredData.size();
}
public Object getItem(int position) {
return filteredData.get(position);
}
public long getItemId(int position) {
return position;
}
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.text_view, null);
holder = new ViewHolder();
holder.image = (ImageView) convertView.findViewById(R.id.iAccept);
holder.text = (TextView) convertView.findViewById(R.id.contentText);
holder.text.setTypeface(regular);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.image.setTag(position);
convertView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(context, (Integer) holder.image.getTag() + "", Toast.LENGTH_SHORT).show();
ViewHolder vh = (ViewHolder) v.getTag();
ImageView iv = vh.image;
if (iv.getVisibility() == View.VISIBLE) {
iv.setVisibility(View.INVISIBLE);
} else {
iv.setVisibility(View.VISIBLE);
}
}
});
if (filteredData.get(position).contains("font"))
holder.text.setText(Html.fromHtml(filteredData.get(position)));
else
holder.text.setText(filteredData.get(position));
return convertView;
}
static class ViewHolder {
ImageView image;
TextView text;
}
public Filter getFilter() {
return mFilter;
}
private class ItemFilter extends Filter {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
String filterString = constraint.toString().toLowerCase();
FilterResults results = new FilterResults();
final List<String> list = originalData;
int count = list.size();
final ArrayList<String> nlist = new ArrayList<String>(count);
String filterableString;
for (int i = 0; i < count; i++) {
filterableString = list.get(i);
if (filterableString.toLowerCase().contains(filterString)) {
filterableString = filterableString.replaceAll(filterString, "<font color='red'>" + filterString + "</font>");
nlist.add(filterableString);
}
}
results.values = nlist;
results.count = nlist.size();
return results;
}
@SuppressWarnings("unchecked")
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
filteredData = (ArrayList<String>) results.values;
notifyDataSetChanged();
}
}
}
敌人的例子当我点击第一个项目时,第四个项目也将可见,当列表滚动时会出现此问题
有什么问题?我怎么解决这个问题?
答案 0 :(得分:1)
问题是您的适配器在滚动时重复使用视图,但您只在调用onClicked()时设置了视图的状态。
如果你想避免这种情况,一个解决方案就是拥有一个包含View可见性状态的辅助List。
e.g。
ArrayList<Integer> visiblePositions = new ArrayList<Integer>();
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.text_view, null);
holder = new ViewHolder();
holder.image = (ImageView) convertView.findViewById(R.id.iAccept);
holder.text = (TextView) convertView.findViewById(R.id.contentText);
holder.text.setTypeface(regular);
convertView.setTag(holder);
}
else holder = (ViewHolder) convertView.getTag();
holder.image.setTag(position);
adjustImageVisibility(viewHolder, visiblePositions.contains(position));
convertView.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
onViewClicked(holder, position);
}
});
if (filteredData.get(position).contains("font")) holder.text.setText(Html.fromHtml(filteredData.get(position)));
else holder.text.setText(filteredData.get(position));
return convertView;
}
public void onViewClicked(ViewHolder viewHolder, Integer position){
if (visiblePositions.contains(position)) {
adjustImageVisibility(viewHolder.image, false);
visibilePositions.remove(position);
}
else {
adjustImageVisibility(viewHolder.image, true);
visiblePositions.add(position);
}
}
public void adjustImageVisibility(ImageView imageView, boolean visible){
if(visible) imageView.setVisibility(View.INVISIBLE);
else imageView.setVisibility(View.VISIBLE);
}
通过支持单击项目的位置并使适配器的getView()方法在构建要显示的视图时使用该位置,这应该对您有用。
注意:如果由于某种原因列表的顺序发生变化,此解决方案可能会失败。
更好的方法是创建一个Object,它既包含要显示的String,也包含其可见性的布尔值,然后让Object自己处理它。
但这应该指出你正确的方向。
答案 1 :(得分:0)
您需要一个单独的数组来保存每个组的图像状态。以下是我对此的解释。
private boolean[] mImageStates;
public ContentViewrAdaptor(Context context, List<String> data, Typeface regular) {
...
mImageStates = new boolean[getCount()];
}
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.text_view, null);
holder = new ViewHolder();
holder.image = (ImageView) convertView.findViewById(R.id.iAccept);
holder.text = (TextView) convertView.findViewById(R.id.contentText);
holder.text.setTypeface(regular);
convertView.setTag(holder);
convertView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(context, (Integer) holder.image.getTag() + "", Toast.LENGTH_SHORT).show();
ImageView imageView = holder.image;
int position = Integer.parseInt(imageView.getTag().toString());
if (mImageStates[position]) {
imageView.setVisibility(View.INVISIBLE);
mImageStates[position] = false;
} else {
imageView.setVisibility(View.VISIBLE);
mImageStates[position] = true;
}
}
});
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.image.setTag(position);
if (mImageStates[position]) {
holder.image.setVisibility(View.INVISIBLE);
} else {
holder.image.setVisibility(View.VISIBLE);
}
if (filteredData.get(position).contains("font")) {
holder.text.setText(Html.fromHtml(filteredData.get(position)));
} else {
holder.text.setText(filteredData.get(position));
}
return convertView;
}