我尝试在我的应用中实现RecyclerView
但我滚动时RecyclerView
中的某些UI组件会不断重复。这可能是缓存的问题,但我还没有为UI元素的缓存实现任何逻辑。我也使用毕加索图书馆。
这是我的代码:
public class Adapter extends RecyclerView.Adapter<Adapter.ViewHolder> {
private List<CardViewBean> mListItemsCard;
Activity mActivity;
CardViewBean itemCardView;
ChatMessageDTO chatMessageDTO;
List<EventGalleryDTO> eventGalleryDTOs;
private static final String PICASSO_CACHE = "picasso-cache";
public Adapter(Activity activity, List<CardViewBean> listItemsCard) {
mListItemsCard = listItemsCard;
this.mActivity = activity;
}
@Override
public Adapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return new ViewHolder(mActivity, LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_adapter, parent, false));
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(mActivity);
itemCardView = mListItemsCard.get(position);
holder.txtEventName.setText(itemCardView.getEventName());
if (itemCardView.getMessageCount() > 0) {
holder.txtChat.setText("(" + itemCardView.getMessageCount() + ")");
}
if (itemCardView.getImageCount() > 0) {
holder.txtGallery.setText(String.valueOf(itemCardView.getImageCount()));
}
chatMessageDTO = getChatMsgDTO();
String imagePath = mListItemsCard.get(position).getImagePath();
if (StringUtils.isNotBlank(imagePath)) {
File file = new File(imagePath);
if (file.exists()) {
setCreatorImage(file, holder);
} else {
holder.ivEventImage.setImageResource(R.drawable.placeholder);
}
}
if (chatMessageDTO != null) {
if (StringUtils.isNotBlank(chatMessageDTO.getMessage())) {
holder.txtLastMessage.setText(chatMessageDTO.getMessage());
}
String time = "";
if (StringUtils.isNotBlank(chatMessageDTO.getDateTime())) {
String dateArr[] = chatMessageDTO.getDateTime().split(" ");
if (dateArr.length > 1) {
if (dateArr[1].contains(":")) {
time = dateArr[1].substring(0, dateArr[1].lastIndexOf(":"));
} else {
time = dateArr[1];
}
holder.txtLastMessageTime.setText(time);
}
}
}
}
@Override
public int getItemCount() {
return mListItemsCard.size();
}
}
答案 0 :(得分:0)
试试这个(添加一个else语句):
if (itemCardView.getMessageCount() > 0) {
holder.txtChat.setText("(" + itemCardView.getMessageCount() + ")");
}
else {
holder.txtChat.setText("")
}
if (itemCardView.getImageCount() > 0) {
holder.txtGallery.setText(String.valueOf(itemCardView.getImageCount()));
}
else {
holder.txtGallery.setText("")
}