RecyclerView-更改选定的项目图标需要两次单击才能工作 [英] RecyclerView - changing selected item icon needs two clicks to work
问题描述
我正在开发一个邮件应用程序,用户可以在其中将同一封邮件发送给多个联系人.当我打开联系人列表时,我希望能够单击该用户,并且他的个人资料图片应替换为选中的图标.
I am working on a mailing app, in which user can send the same mail to multiple contacts. When I open the contacts list I want to be able to click on the user and his profile picture should be replaced with checked icon.
当我单击以选择用户时,图标 闪烁,并且第一次单击时它不会更改 . 第二次单击它时,图像仍然闪烁,但是变为选中状态 .每当我下次单击该用户时,它都会闪烁,但会执行我想要的操作-变成已选中/未选中.
When I click to select a user the icon flickers and it does not change the first time I click on it. Second time I click on it the image still flickers, but then changes to checked, and every next time I click on that user it will flicker but do what I want - become checked/unchecked.
我正在使用此教程作为指南,但没有得到应有的详细记录.有些方法用一个词解释,其他方法甚至都没有提及,但出现在代码中.我寻找了其他教程,确实发现了很多相同的内容(
I am using this tutorial as a guide, but it is not documented as good as it should be. Some methods are explained in a single word, other not even mentioned, but appear in the code. I looked for other tutorials, and really found a lot of the same (identical) examples, without going even a bit deeper than the original.
Adapter.java :
@Override
public void onBindViewHolder(final ChooseContactsAdapter.ChooseContactsViewHolder holder, final int position) {
final Contact contact = contactList.get(position);
holder.userName.setText(contact.getUserName());
TextDrawable.IBuilder builder = TextDrawable.builder()
.beginConfig()
.withBorder(0)
.toUpperCase()
.endConfig()
.round();
ColorGenerator generator = ColorGenerator.MATERIAL;
// generate color based on a key (same key returns the same color), useful for list/grid views
int color = generator.getColor(contact.getUserId());
textDrawable = builder.build(contactList.get(position).getUserName().substring(0, 1), color);
holder.thumbNail.setImageDrawable(textDrawable);
holder.contactId = contact.getUserId();
// display profile image
applyProfilePicture(holder, contact);
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// toggle selection
toggleSelection(position);
// Change background color of the selected items in list view
holder.itemView.setBackgroundColor(selectedItems.get(position) ? 0x9934B5E4 : Color.TRANSPARENT);
// check if item still exists
if (position != RecyclerView.NO_POSITION) {
Contact contact = contactList.get(position);
Toast.makeText(v.getContext(), "You clicked " + contact.getUserName(), Toast.LENGTH_SHORT).show();
}
// handle icon animation
applyIconAnimation(holder, position);
}
});
}
private void applyProfilePicture(ChooseContactsViewHolder holder, Contact contact) {
Picasso.with(context)
.load(AppConfig.URL_PROFILE_PHOTO + contact.getThumbnailUrl())
.placeholder(textDrawable)
.error(textDrawable)
.transform(new CircleTransform())
.into(holder.thumbNail);
}
private void applyIconAnimation(ChooseContactsViewHolder holder, int position) {
if (selectedItems.get(position, false)) {
holder.iconFront.setVisibility(View.GONE);
resetIconYAxis(holder.iconBack);
holder.iconBack.setVisibility(View.VISIBLE);
holder.iconBack.setAlpha(1);
if (currentSelectedIndex == position) {
FlipAnimator.flipView(context, holder.iconBack, holder.iconFront, true);
resetCurrentIndex();
}
} else {
holder.iconBack.setVisibility(View.GONE);
resetIconYAxis(holder.iconFront);
holder.iconFront.setVisibility(View.VISIBLE);
holder.iconFront.setAlpha(1);
if ((reverseAllAnimations && animationItemsIndex.get(position, false)) || currentSelectedIndex == position) {
FlipAnimator.flipView(context, holder.iconBack, holder.iconFront, false);
resetCurrentIndex();
}
}
}
private void toggleSelection(int pos) {
currentSelectedIndex = pos;
if (selectedItems.get(pos, false)) {
selectedItems.delete(pos);
animationItemsIndex.delete(pos);
} else {
selectedItems.put(pos, true);
animationItemsIndex.put(pos, true);
}
notifyItemChanged(pos);
}
推荐答案
无需在 toggleSelection
方法上调用 notifyItemChanged
.您真想通过动画手动更改项目.
There's no need to call notifyItemChanged
on the toggleSelection
method. You are alredy changing the item manually with animations.
调用 notifyItemChanged
是导致闪烁的原因,因为它会干扰动画.
Calling notifyItemChanged
is what causes the flickering, because it interferes with the animations.
这篇关于RecyclerView-更改选定的项目图标需要两次单击才能工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!