我正在开发一个邮件应用程序,用户可以在其中将同一封邮件发送给多个联系人。当我打开联系人列表时,我希望能够单击该用户,并且他的个人资料图片应替换为选中的图标。
当我单击选择用户时,图标闪烁,并且在我第一次单击时它不会改变。 我第二次单击它时,图像仍然闪烁,但随后更改为已选中,并且每次我下次单击该用户时,它都会闪烁,但会执行我想要的操作 - 变为选中/取消选中。
我正在使用this教程作为指南,但它没有得到应有的良好记录。有些方法用一个词解释,有些方法甚至没有提到,但出现在代码中。我查找了其他教程,确实发现了很多相同的 ( identical ) 示例,但没有比原始教程更深入。
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
。您已经用动画手动更改了该项目。
调用 notifyItemChanged
是导致闪烁的原因,因为它会干扰动画。
关于java - RecyclerView - 更改所选项目图标需要单击两次才能起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47378085/