我正在为Android创建一个应用程序,此时我只想使用 RecyclerView 创建一个可选列表,为此,当用户点击某个项目时,我把它放入数组中的id,我更改其图标。 问题是,当我向下滚动然后我再次向上滚动或当我更改片段时,数组仍然包含所有项目的ID,但图标像以前一样回来,他们不会保留新图像我设定的。
这是onBindViewHolder方法,这个方法是整个项目中唯一改变imageview的方法:
List<Integer> selected = new LinkedList<>();
@Override
public void onBindViewHolder(final ViewHolder holder, int position) {
holder.mBoundString = mValues.get(position);
holder.mTextView.setText(mValues.get(position));
final int pos = position;
holder.mView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final Context context = v.getContext();
Animation to_middle = AnimationUtils.loadAnimation(v.getContext(), R.anim.to_middle);
final Animation from_middle = AnimationUtils.loadAnimation(v.getContext(), R.anim.from_middle);
to_middle.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
holder.mImageView.setImageResource(R.drawable.ic_action_navigation_check);
holder.mImageView.clearAnimation();
}
@Override
public void onAnimationRepeat(Animation animation) {
}
});
holder.mImageView.setAnimation(to_middle);
holder.mImageView.startAnimation(to_middle);
selected.add(pos);
}
});
if(!selected.contains(position)) {
/* I checked and this method it's not called when
an item is selected, so the following instruction
isn't the cause of my problem, anyway the image
comes back as before */
Glide.with(holder.mImageView.getContext())
.load(R.drawable.ic_default)
.fitCenter()
.into(holder.mImageView);
}
}
我确定R.drawable.ic_default没有在其他地方使用,那么哪种方法正在改变图像? 谢谢
答案 0 :(得分:0)
if(selected.contains(position)) { // Check if already selected
Glide.with(holder.mImageView.getContext())
.load(R.drawable.ic_action_navigation_check) // set selected drawable here.
.fitCenter()
.into(holder.mImageView);
}
您正在检查“如果没有选择将图像设置为默认值”,但您需要选中“如果选择的图像设置为所选图像”。