/** * Updates the list adding or modifying a tag, updating its position according the the sort * criterion. * * @param tag the tag to add or modify */ public void update(Tag tag) { // It is necessary to compare the previous position in the list of the tag with the new // position, updating the list if it has changed. int oldPosition = 0; int newPosition = TagSettings.getTagPosition( mContext, tag.getId(), mProfileId, mTagOrder, TagSettings.LIMIT_UNBOUNDED); while (oldPosition < mTags.size() && mTags.get(oldPosition).getId() != tag.getId()) { oldPosition++; } if (oldPosition >= mTags.size()) { // The tag is not in the list because it's new and its custom settings were saved during its // creation. mTags.add(newPosition, tag); notifyItemInserted(newPosition); } if (oldPosition != newPosition) { // Animate the change of position of this tag. // The tag is removed from its current position and added to the new one. mTags.remove(oldPosition); mTags.add(newPosition, tag); notifyItemRemoved(oldPosition); notifyItemInserted(newPosition); } else { // Update the tag because its name (and therefore its favicon) may have changed. mTags.set(oldPosition, tag); notifyItemChanged(oldPosition); } }
@Override public void onBindViewHolder(TagListViewHolder tagListViewHolder, int i) { final Tag tag = mTags.get(i); // Set tag favicon FaviconLoader.setAsBackground(mContext, tagListViewHolder.getFaviconTextView(), tag); // Set tag name tagListViewHolder.getTagNameTextView().setText(tag.getName()); // Set tag click listener tagListViewHolder.itemView.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View v) { // Increase hash counter. Note that this may affect the order of the tags in the // case that they are ordered by usage. tag.setHashCounter(tag.getHashCounter() + 1); TagSettings.updateTag(mContext, tag); if (mTagOrder == TagSettings.ORDER_BY_HASH_COUNTER) { update(tag); } // Notify to the tag click listener mTagClickedListener.onTagClicked(new Tag(tag)); } }); // Add listener for tag long click event, which notifies to the tag click listener. tagListViewHolder.itemView.setOnLongClickListener( new View.OnLongClickListener() { @Override public boolean onLongClick(View v) { mTagClickedListener.onTagLongClicked(tag); return false; } }); }