Fix issue with blinking in recycler view

This commit is contained in:
Victor Shcherb 2018-05-16 22:45:47 +02:00
parent 3828d2ec51
commit 98a56ceb73
4 changed files with 95 additions and 70 deletions

View file

@ -137,13 +137,13 @@ public class ExploreRvAdapter extends RecyclerView.Adapter<RecyclerView.ViewHold
return false;
}
public void setNeededMapsCard(TravelNeededMapsCard card) {
public void addNeededMapsCard(TravelNeededMapsCard card) {
if (addItem(getNeededMapsCardPosition(), card)) {
notifyDataSetChanged();
}
}
public void updateNeededMapsCard() {
public void updateNeededMapsCard(boolean onlyProgress) {
int pos = getNeededMapsCardPosition();
if (neededMapsCardExists(pos)) {
notifyItemChanged(pos);
@ -169,13 +169,13 @@ public class ExploreRvAdapter extends RecyclerView.Adapter<RecyclerView.ViewHold
return items.size() > position && items.get(position).getCardType() == TravelNeededMapsCard.TYPE;
}
public void setDownloadUpdateCard(TravelDownloadUpdateCard card) {
public void addDownloadUpdateCard(TravelDownloadUpdateCard card) {
if (addItem(getDownloadUpdateCardPosition(), card)) {
notifyDataSetChanged();
}
}
public void updateDownloadUpdateCard() {
public void updateDownloadUpdateCard(boolean onlyProgress) {
int pos = getDownloadUpdateCardPosition();
if (downloadUpdateCardExists(pos)) {
notifyItemChanged(pos);

View file

@ -90,8 +90,8 @@ public class ExploreTabFragment extends BaseOsmAndFragment implements DownloadIn
currentDownloadingIndexItem = current;
removeRedundantCards();
}
adapter.updateDownloadUpdateCard();
adapter.updateNeededMapsCard();
adapter.updateDownloadUpdateCard(true);
adapter.updateNeededMapsCard(true);
}
@Override
@ -194,7 +194,7 @@ public class ExploreTabFragment extends BaseOsmAndFragment implements DownloadIn
public void onPrimaryButtonClick() {
if (mainIndexItem != null) {
downloadManager.startDownload(getMyActivity(), mainIndexItem);
adapter.updateDownloadUpdateCard();
adapter.updateDownloadUpdateCard(false);
}
}
@ -202,7 +202,7 @@ public class ExploreTabFragment extends BaseOsmAndFragment implements DownloadIn
public void onSecondaryButtonClick() {
if (downloadUpdateCard.isLoading()) {
downloadThread.cancelDownload(mainIndexItem);
adapter.updateDownloadUpdateCard();
adapter.updateDownloadUpdateCard(false);
} else if (!downloadUpdateCard.isDownload()) {
removeDownloadUpdateCard();
} else if (downloadUpdateCard.isShowOtherMapsBtn()) {
@ -217,7 +217,7 @@ public class ExploreTabFragment extends BaseOsmAndFragment implements DownloadIn
}
});
downloadUpdateCard.setIndexItem(mainIndexItem);
adapter.setDownloadUpdateCard(downloadUpdateCard);
adapter.addDownloadUpdateCard(downloadUpdateCard);
}
}
@ -230,14 +230,14 @@ public class ExploreTabFragment extends BaseOsmAndFragment implements DownloadIn
@Override
public void onPrimaryButtonClick() {
downloadManager.startDownload(getMyActivity(), getAllItemsForDownload());
adapter.updateNeededMapsCard();
adapter.updateNeededMapsCard(false);
}
@Override
public void onSecondaryButtonClick() {
if (neededMapsCard.isDownloading()) {
app.getDownloadThread().cancelDownload(neededIndexItems);
adapter.updateNeededMapsCard();
adapter.updateNeededMapsCard(false);
} else {
removeNeededMapsCard();
}
@ -257,11 +257,11 @@ public class ExploreTabFragment extends BaseOsmAndFragment implements DownloadIn
} else if (!item.isDownloaded()) {
downloadManager.startDownload(getMyActivity(), item);
}
adapter.updateNeededMapsCard();
adapter.updateNeededMapsCard(false);
}
}
});
adapter.setNeededMapsCard(neededMapsCard);
adapter.addNeededMapsCard(neededMapsCard);
}
}

View file

@ -10,11 +10,11 @@ import android.view.View;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;
import net.osmand.plus.OsmandApplication;
import net.osmand.plus.R;
import net.osmand.plus.download.IndexItem;
import java.lang.ref.WeakReference;
import java.text.DateFormat;
public class TravelDownloadUpdateCard extends BaseTravelCard {
@ -23,6 +23,7 @@ public class TravelDownloadUpdateCard extends BaseTravelCard {
private boolean download;
private boolean showOtherMapsBtn;
private WeakReference<DownloadUpdateVH> ref;
private ClickListener listener;
@ -62,6 +63,7 @@ public class TravelDownloadUpdateCard extends BaseTravelCard {
if (viewHolder instanceof DownloadUpdateVH) {
boolean loading = isLoading();
DownloadUpdateVH holder = (DownloadUpdateVH) viewHolder;
this.ref = new WeakReference<TravelDownloadUpdateCard.DownloadUpdateVH>(holder);
holder.title.setText(getTitle(loading));
holder.icon.setImageDrawable(getIcon());
holder.description.setText(getDescription());
@ -73,18 +75,29 @@ public class TravelDownloadUpdateCard extends BaseTravelCard {
holder.fileTitle.setText(getFileTitle());
holder.fileDescription.setText(getFileDescription());
holder.progressBar.setVisibility(loading ? View.VISIBLE : View.GONE);
if (isLoadingInProgress()) {
int progress = app.getDownloadThread().getCurrentDownloadingItemProgress();
holder.progressBar.setProgress(progress < 0 ? 0 : progress);
} else {
holder.progressBar.setProgress(0);
}
updateProgressBar(holder);
}
boolean primaryBtnVisible = updatePrimaryButton(holder, loading);
boolean secondaryBtnVisible = updateSecondaryButton(holder, loading);
holder.buttonsDivider.setVisibility(primaryBtnVisible && secondaryBtnVisible ? View.VISIBLE : View.GONE);
}
}
public void updateProgresBar() {
DownloadUpdateVH holder = ref.get();
if (holder != null && holder.itemView.isShown()) {
updateProgressBar(holder);
}
}
private void updateProgressBar(DownloadUpdateVH holder) {
if (isLoadingInProgress()) {
int progress = app.getDownloadThread().getCurrentDownloadingItemProgress();
holder.progressBar.setProgress(progress < 0 ? 0 : progress);
} else {
holder.progressBar.setProgress(0);
}
}
@Override
public int getCardType() {

View file

@ -10,14 +10,15 @@ import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;
import net.osmand.plus.OsmandApplication;
import net.osmand.plus.R;
import net.osmand.plus.Version;
import net.osmand.plus.download.DownloadActivityType;
import net.osmand.plus.download.DownloadIndexesThread;
import net.osmand.plus.download.IndexItem;
import net.osmand.plus.wikivoyage.explore.travelcards.TravelDownloadUpdateCard.DownloadUpdateVH;
import java.lang.ref.WeakReference;
import java.util.List;
public class TravelNeededMapsCard extends BaseTravelCard {
@ -29,7 +30,7 @@ public class TravelNeededMapsCard extends BaseTravelCard {
private Drawable downloadIcon;
private Drawable cancelIcon;
private WeakReference<NeededMapsVH> ref;
private CardListener listener;
private View.OnClickListener onItemClickListener;
@ -57,64 +58,75 @@ public class TravelNeededMapsCard extends BaseTravelCard {
public void bindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder) {
if (viewHolder instanceof NeededMapsVH) {
NeededMapsVH holder = (NeededMapsVH) viewHolder;
ref = new WeakReference<NeededMapsVH>(holder);
holder.description.setText(isInternetAvailable()
? R.string.maps_you_need_descr : R.string.no_index_file_to_download);
adjustChildCount(holder.itemsContainer);
boolean paidVersion = Version.isPaidVersion(app);
for (int i = 0; i < items.size(); i++) {
IndexItem item = items.get(i);
boolean downloading = downloadThread.isDownloading(item);
boolean currentDownloading = downloading && downloadThread.getCurrentDownloadingItem() == item;
boolean lastItem = i == items.size() - 1;
View view = holder.itemsContainer.getChildAt(i);
if (item.isDownloaded()) {
view.setOnClickListener(null);
} else {
view.setTag(item);
view.setOnClickListener(onItemClickListener);
}
((ImageView) view.findViewById(R.id.icon))
.setImageDrawable(getActiveIcon(item.getType().getIconResource()));
((TextView) view.findViewById(R.id.title))
.setText(item.getVisibleName(app, app.getRegions(), false));
((TextView) view.findViewById(R.id.description)).setText(getItemDescription(item));
ImageView iconAction = (ImageView) view.findViewById(R.id.icon_action);
Button buttonAction = (Button) view.findViewById(R.id.button_action);
if (item.isDownloaded()) {
iconAction.setVisibility(View.GONE);
buttonAction.setVisibility(View.GONE);
} else {
boolean showBtn = !paidVersion && item.getType() == DownloadActivityType.WIKIPEDIA_FILE;
iconAction.setVisibility(showBtn ? View.GONE : View.VISIBLE);
buttonAction.setVisibility(showBtn ? View.VISIBLE : View.GONE);
if (!showBtn) {
iconAction.setImageDrawable(downloading ? cancelIcon : downloadIcon);
}
}
ProgressBar progressBar = (ProgressBar) view.findViewById(R.id.progress_bar);
progressBar.setVisibility(downloading ? View.VISIBLE : View.GONE);
if (currentDownloading) {
int progress = downloadThread.getCurrentDownloadingItemProgress();
progressBar.setProgress(progress < 0 ? 0 : progress);
} else {
progressBar.setProgress(0);
}
view.findViewById(R.id.divider).setVisibility(lastItem ? View.GONE : View.VISIBLE);
}
updateView(holder);
boolean primaryBtnVisible = updatePrimaryButton(holder);
boolean secondaryBtnVisible = updateSecondaryButton(holder);
holder.buttonsDivider.setVisibility(primaryBtnVisible && secondaryBtnVisible ? View.VISIBLE : View.GONE);
}
}
public void updateView() {
NeededMapsVH holder = ref.get();
if (holder != null && holder.itemView.isShown()) {
updateView(holder);
}
}
private void updateView(NeededMapsVH holder) {
boolean paidVersion = Version.isPaidVersion(app);
for (int i = 0; i < items.size(); i++) {
IndexItem item = items.get(i);
boolean downloading = downloadThread.isDownloading(item);
boolean currentDownloading = downloading && downloadThread.getCurrentDownloadingItem() == item;
boolean lastItem = i == items.size() - 1;
View view = holder.itemsContainer.getChildAt(i);
if (item.isDownloaded()) {
view.setOnClickListener(null);
} else {
view.setTag(item);
view.setOnClickListener(onItemClickListener);
}
((ImageView) view.findViewById(R.id.icon))
.setImageDrawable(getActiveIcon(item.getType().getIconResource()));
((TextView) view.findViewById(R.id.title))
.setText(item.getVisibleName(app, app.getRegions(), false));
((TextView) view.findViewById(R.id.description)).setText(getItemDescription(item));
ImageView iconAction = (ImageView) view.findViewById(R.id.icon_action);
Button buttonAction = (Button) view.findViewById(R.id.button_action);
if (item.isDownloaded()) {
iconAction.setVisibility(View.GONE);
buttonAction.setVisibility(View.GONE);
} else {
boolean showBtn = !paidVersion && item.getType() == DownloadActivityType.WIKIPEDIA_FILE;
iconAction.setVisibility(showBtn ? View.GONE : View.VISIBLE);
buttonAction.setVisibility(showBtn ? View.VISIBLE : View.GONE);
if (!showBtn) {
iconAction.setImageDrawable(downloading ? cancelIcon : downloadIcon);
}
}
ProgressBar progressBar = (ProgressBar) view.findViewById(R.id.progress_bar);
progressBar.setVisibility(downloading ? View.VISIBLE : View.GONE);
if (currentDownloading) {
int progress = downloadThread.getCurrentDownloadingItemProgress();
progressBar.setProgress(progress < 0 ? 0 : progress);
} else {
progressBar.setProgress(0);
}
view.findViewById(R.id.divider).setVisibility(lastItem ? View.GONE : View.VISIBLE);
}
}
@Override
public int getCardType() {