我有一个ListView,每个项目包含一个Textview
和ImageView
imageView
为invisible
,并点击每个listview项目visible
我的代码是:
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
ImageView acceptImageView= (ImageView) view.findViewById(R.id.iAccept);
if (acceptImageView.getVisibility()==ImageView.INVISIBLE) {
acceptImageView.setVisibility(ImageView.VISIBLE);
} else {
acceptImageView.setVisibility(ImageView.INVISIBLE);
}
}
List View adapter :
public class ContentViewrAdaptor extends BaseAdapter implements Filterable {
private List<String> originalData = null;
private List<String> filteredData = null;
private LayoutInflater mInflater;
private Typeface regular;
private ItemFilter mFilter = new ItemFilter();
public ContentViewrAdaptor(Context context, List<String> data, Typeface regular) {
this.filteredData = data;
this.originalData = data;
this.regular = regular;
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(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.text_view, null);
holder = new ViewHolder();
holder.text = (TextView) convertView.findViewById(R.id.contentText);
holder.text.setTypeface(regular);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
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 {
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();
}
}
}
但问题是,当我接受第一个项目时,第三个项目也将加入,因为它是向下滚动时的第一个项目
我该如何解决这个问题?
更新(getView)
但仍然是同样的问题
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.text_view, null);
holder = new ViewHolder();
holder.image = (ImageView) convertView.findViewById(R.id.iAccept);
convertView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(context,position,Toast.LENGTH_SHORT).show();
if (v.findViewById(R.id.iAccept).getVisibility() == ImageView.VISIBLE)
v.findViewById(R.id.iAccept).setVisibility(ImageView.INVISIBLE);
else
v.findViewById(R.id.iAccept).setVisibility(ImageView.VISIBLE);
}
});
holder.text = (TextView) convertView.findViewById(R.id.contentText);
holder.text.setTypeface(regular);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
if (filteredData.get(position).contains("font"))
holder.text.setText(Html.fromHtml(filteredData.get(position)));
else
holder.text.setText(filteredData.get(position));
return convertView;
}
最终更新
最后用DIVA帮助解决了,
我将适配器更改为:
public class ContentViewrAdaptor extends BaseAdapter implements Filterable {
private List<String> originalData = null;
private List<String> filteredData = null;
private ArrayList<Integer> visiblePositions;
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;
visiblePositions = new ArrayList<Integer>();
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.image.setTag(position);
holder.text = (TextView) convertView.findViewById(R.id.contentText);
holder.text.setTypeface(regular);
convertView.setTag(holder);
} else holder = (ViewHolder) convertView.getTag();
adjustImageVisibility(holder.image, 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);
visiblePositions.remove(position);
} else {
adjustImageVisibility(viewHolder.image, true);
visiblePositions.add(position);
}
}
public void adjustImageVisibility(ImageView imageView, boolean visible) {
if (visible) imageView.setVisibility(View.VISIBLE);
else imageView.setVisibility(View.INVISIBLE);
}
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 :(得分:2)
你需要确定的事情很少:
<强>适配器强>
public class ListAdapter extends BaseAdapter implements Filterable {
private ArrayList<String> originalData = null;
private ArrayList<String> filteredData = null;
private LayoutInflater mInflater;
private Context context;
private ItemFilter mFilter = new ItemFilter();
public ListAdapter(Context context, ArrayList<String> data) {
this.filteredData = new ArrayList<>(data);
this.originalData = new ArrayList<>(data);
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) {
ViewHolder holder = new ViewHolder();
if (convertView == null) {
convertView = mInflater.inflate(R.layout.list_item, null);
holder = new ViewHolder();
holder.image = (ImageView) convertView.findViewById(R.id.image);
// holder.image.setVisibility(View.INVISIBLE);
holder.text = (TextView) convertView.findViewById(R.id.text);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
// holder.image.setTag(position);
convertView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (v.findViewById(R.id.image).getVisibility() == View.VISIBLE) {
v.findViewById(R.id.image).setVisibility(View.INVISIBLE);
} else {
v.findViewById(R.id.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;
}
static class ViewHolder {
ImageView image;
TextView text;
}
@Override
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 ArrayList<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();
}
}
}
<强>活动强>
public class MyActivity extends Activity {
private ListView listView;
private ListAdapter adapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
listView = (ListView) findViewById(R.id.listView);
ArrayList<String> LIST_ITEM = new ArrayList<String>();
LIST_ITEM.add("Item 1");
LIST_ITEM.add("Item 2");
LIST_ITEM.add("Item 3");
LIST_ITEM.add("Item 4");
LIST_ITEM.add("Item 5");
LIST_ITEM.add("Item 6");
LIST_ITEM.add("Item 7");
LIST_ITEM.add("Item 8");
LIST_ITEM.add("Item 9");
LIST_ITEM.add("Item 10");
listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
adapter = new ListAdapter(MyActivity.this, LIST_ITEM);
listView.setAdapter(adapter);
}
}
EDIT ALTERNATIVE CODE.
答案 1 :(得分:0)
对于每个列表项,您需要创建boolean
参数isVisible
。并在单击列表项时将其设置为true / false。在getView()
方法中,检查每个可见项目的isVisible
值:
if(isVisible)
make the image visible;
else
hide the image;