【发布时间】:2015-03-23 05:33:17
【问题描述】:
我有一个最喜欢的 ImageButton,它位于另一个 Imageview 上。这是正确的,但是当我在里面相应地设置新图像时,我得到了两个相同的图像。
不知道哪里错了?
这是我的 XML(有 ImageView):
<ImageView
android:id="@+id/item_image"
android:layout_width="match_parent"
android:layout_height="150dp"
android:layout_below="@+id/grid_regular_image"
android:layout_centerHorizontal="true"
android:src="@drawable/none"
tools:ignore="ContentDescription" />
<ImageButton
android:id="@+id/favouriteIcon"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_alignParentRight="true"
android:layout_alignTop="@+id/item_image"
android:background="@android:color/transparent"
android:paddingBottom="20dp"
android:paddingRight="5dp"
android:visibility="invisible" />
这是我的自定义 Apadter 文件:这里我也在点击部分调用图像。
@Override
public View getView(int position, View convertView, final ViewGroup parent)
{
ViewHolder holder = null;
if (convertView == null)
{
convertView = vi.inflate(R.layout.emb_gridlist_items, null);
holder = new ViewHolder();
holder.favourite = (ImageButton) convertView.findViewById(R.id.favouriteIcon);
holder.favourite.setVisibility(View.VISIBLE);
convertView.setTag(holder);
}
else
{
holder = (ViewHolder) convertView.getTag();
}
favouriteclicked = pref.getString("favourite", getContext().getApplicationContext().getResources().getString(R.string.favouritenotclicked));
if (favouriteclicked.equalsIgnoreCase(getContext().getApplicationContext().getResources().getString(R.string.favouritenotclicked)))
{
holder.favourite.startAnimation(AnimationUtils.loadAnimation(getContext(), R.anim.anim_alpha));
holder.favourite.setImageResource(R.drawable.ic_starfill);
SharedPreferences.Editor editor6 = pref.edit();
editor6.putString("favouriteclicked", getContext().getApplicationContext().getResources().getString(R.string.favouriteclicked));
editor6.commit();
}
else
{
holder.favourite.startAnimation(AnimationUtils.loadAnimation(getContext(), R.anim.anim_alpha));
holder.favourite.setImageResource(R.drawable.ic_stillnofill);
SharedPreferences.Editor editor6 = pref.edit();
editor6.putString("favouriteclicked", getContext().getApplicationContext().getResources().getString(R.string.favouritenotclicked));
editor6.commit();
}
holder.favourite.setOnClickListener( new View.OnClickListener()
{
public void onClick(View v)
{
favouriteclicked = pref.getString("favourite", getContext().getApplicationContext().getResources().getString(R.string.favouriteclicked));
ImageButton favi = (ImageButton) v;
if (favouriteclicked.equalsIgnoreCase(getContext().getApplicationContext().getResources().getString(R.string.favouritenotclicked)))
{
favi.startAnimation(AnimationUtils.loadAnimation(getContext(), R.anim.anim_alpha));
favi.setBackgroundResource(R.drawable.ic_starfill);
SharedPreferences.Editor editor6 = pref.edit();
editor6.putString("favouriteclicked", getContext().getApplicationContext().getResources().getString(R.string.favouriteclicked));
editor6.commit();
}
else
{
favi.startAnimation(AnimationUtils.loadAnimation(getContext(), R.anim.anim_alpha));
favi.setBackgroundResource(R.drawable.ic_stillnofill);
SharedPreferences.Editor editor6 = pref.edit();
editor6.putString("favouriteclicked", getContext().getApplicationContext().getResources().getString(R.string.favouritenotclicked));
editor6.commit();
}
}
});
}
使用上面的代码。当我单击收藏夹图标时,我可以同时看到两个图标。一个旧图像和另一个新图像。
如何清除旧图像?
谢谢!
【问题讨论】:
-
这是因为您的视图被回收了,您没有在 Imagebutton 的 View 上设置任何标签,并且您没有在 ImageButton 的 onClick() 中获取任何标签在这里查看我的答案:stackoverflow.com/questions/29181500/…。我认为问题类似。
-
解决了。非常感谢方德萨。虽然链接没有给我太多用处,但你的逻辑确实有用。