带有收藏夹按钮的列表视图 [英] listview with favorite button
问题描述
在我的项目中,我有一个带有星图的listView.如果用户触摸星标,则将其添加到收藏夹.所有代码都是正确的,但是当listview滚动星形图像变为不受欢迎时.这是我的代码:
In my project I have a listView with star image. If a user touches a star item add to favorite. All code is correct but when listview scroll star image change to unfavorite. This is my code:
private class MyAdapter extends ArrayAdapter<String> {
public MyAdapter(Context context, int resource, int textViewResourceId,
String[] strings) {
super(context, resource, textViewResourceId, strings);
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View row = inflater.inflate(R.layout.custom_list_item, parent, false);
String my_string = title[position].toString();
final TextView content_title = (TextView) row.findViewById(R.id.contentitle);
final ImageView favicon = (ImageView) row.findViewById(R.id.favicon);
content_title.setText(my_string);
content_title.setTypeface(koodakfont);
if (favicon.getTag().equals("fav")) {
favicon.setImageResource(R.drawable.favicon);
favicon.setTag("fav");
}
favicon.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (favicon.getTag().equals("unfav")) {
favicon.setImageResource(R.drawable.favicon);
favicon.setTag("fav");
} else {
favicon.setImageResource(R.drawable.unfavicon);
favicon.setTag("unfav");
}
}
});
return row;
}
编辑:下面的编辑对我来说也不起作用.
EDIT : the below edit doesn't really work for me either.
static class ViewHolder {
ImageView favicon;
}
private class MyAdapter extends ArrayAdapter<String> {
public MyAdapter(Context context, int resource, int textViewResourceId,
String[] strings) {
super(context, resource, textViewResourceId, strings);
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View row = inflater.inflate(R.layout.custom_list_item, parent, false);
String my_string = title[position].toString();
final TextView content_title = (TextView) row.findViewById(R.id.contentitle);
final ViewHolder holder = new ViewHolder();
holder.favicon = (ImageView) row.findViewById(R.id.favicon);
content_title.setText(my_string);
content_title.setTypeface(koodakfont);
if (holder.favicon.getTag().equals("fav")) {
holder.favicon.setImageResource(R.drawable.favicon);
holder.favicon.setTag("fav");
}
holder.favicon.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (holder.favicon.getTag().equals("unfav")) {
holder.favicon.setImageResource(R.drawable.favicon);
holder.favicon.setTag("fav");
} else {
holder.favicon.setImageResource(R.drawable.unfavicon);
holder.favicon.setTag("unfav");
}
}
});
return row;
}
推荐答案
这是 View
回收的问题,基本上,当您滚动时,将再次创建View,因此星形图标的状态为丢失.您需要实现一种逻辑来检查您需要为每次调用 getView
设置图标的状态.一个很好的起点可能是在创建 View
时使用 ListView
中项目的位置来检查是否需要将喜欢的图标设置为加星标或未加星标.我建议您阅读此答案,其中详细说明了该机制.这是您可以做什么的简化示例:
It's a problem of View
recycling, basically when you scroll, the View is created again, so the state of your star icon is lost. You would need to implement a logic to check in which state you need to set the icon for every call to getView
. A good starting point could be to use the position of the item in the ListView
to check, when the View
is created, if you need your favorite icon to be set to starred or unstarred.
I recommend you read this answer, which explains the mechanism in detail.
Here's a simplified example of what you could do :
private boolean[] favorites; // initialize this array on creation of your adapter with the same size as your listView
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
//the rest of your code
if(favorites[position]){
holder.favicon.setImageResource(R.drawable.favicon);
else{
holder.favicon.setImageResource(R.drawable.unfavicon);
}
holder.favicon.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (favorites[position]) {
holder.favicon.setImageResource(R.drawable.unfavicon);
favorites[position] = false;
} else {
holder.favicon.setImageResource(R.drawable.favicon);
favorites[position] = true;
}
}
});
我还建议您开始使用 ViewHolder 模式是出于效率的考虑,尽管您的问题与此无关.
I also recommend you start using the ViewHolder pattern for efficiency reasons, although you problem is not related to that.
这篇关于带有收藏夹按钮的列表视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!