From 98a56ceb732836b5bfb523df6b8a13f2c0070269 Mon Sep 17 00:00:00 2001 From: Victor Shcherb Date: Wed, 16 May 2018 22:45:47 +0200 Subject: [PATCH] Fix issue with blinking in recycler view --- .../wikivoyage/explore/ExploreRvAdapter.java | 8 +- .../explore/ExploreTabFragment.java | 18 +-- .../travelcards/TravelDownloadUpdateCard.java | 27 +++-- .../travelcards/TravelNeededMapsCard.java | 112 ++++++++++-------- 4 files changed, 95 insertions(+), 70 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/wikivoyage/explore/ExploreRvAdapter.java b/OsmAnd/src/net/osmand/plus/wikivoyage/explore/ExploreRvAdapter.java index 9af1da3336..8052ca49fa 100644 --- a/OsmAnd/src/net/osmand/plus/wikivoyage/explore/ExploreRvAdapter.java +++ b/OsmAnd/src/net/osmand/plus/wikivoyage/explore/ExploreRvAdapter.java @@ -137,13 +137,13 @@ public class ExploreRvAdapter extends RecyclerView.Adapter 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); diff --git a/OsmAnd/src/net/osmand/plus/wikivoyage/explore/ExploreTabFragment.java b/OsmAnd/src/net/osmand/plus/wikivoyage/explore/ExploreTabFragment.java index 46492fd3ee..96ea159263 100644 --- a/OsmAnd/src/net/osmand/plus/wikivoyage/explore/ExploreTabFragment.java +++ b/OsmAnd/src/net/osmand/plus/wikivoyage/explore/ExploreTabFragment.java @@ -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); } } diff --git a/OsmAnd/src/net/osmand/plus/wikivoyage/explore/travelcards/TravelDownloadUpdateCard.java b/OsmAnd/src/net/osmand/plus/wikivoyage/explore/travelcards/TravelDownloadUpdateCard.java index 58555ce002..78afb435c6 100644 --- a/OsmAnd/src/net/osmand/plus/wikivoyage/explore/travelcards/TravelDownloadUpdateCard.java +++ b/OsmAnd/src/net/osmand/plus/wikivoyage/explore/travelcards/TravelDownloadUpdateCard.java @@ -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 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(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() { diff --git a/OsmAnd/src/net/osmand/plus/wikivoyage/explore/travelcards/TravelNeededMapsCard.java b/OsmAnd/src/net/osmand/plus/wikivoyage/explore/travelcards/TravelNeededMapsCard.java index 0b722b7e5d..28636655bf 100644 --- a/OsmAnd/src/net/osmand/plus/wikivoyage/explore/travelcards/TravelNeededMapsCard.java +++ b/OsmAnd/src/net/osmand/plus/wikivoyage/explore/travelcards/TravelNeededMapsCard.java @@ -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 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(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() {