Bug 1395792 - Add show/hide to onBindVH. r?mcomella
MozReview-Commit-ID: H4bAlcaVCld
--- a/mobile/android/app/src/main/res/layout/activity_stream_main_highlightstitle.xml
+++ b/mobile/android/app/src/main/res/layout/activity_stream_main_highlightstitle.xml
@@ -1,16 +1,24 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- This Source Code Form is subject to the terms of the Mozilla Public
- License, v. 2.0. If a copy of the MPL was not distributed with this
- file, You can obtain one at http://mozilla.org/MPL/2.0/. -->
+
+<!-- The width and height of this view are always overridden in StreamTitleRow because
+ this view is dynamically shown/hidden in onBindViewHolder.
+
+ NB: This is a hack because the title views for each section are always included
+ in the RecyclerView adapter.
+ A more correct implementation would dynamically add/remove these title views rather than
+ showing and hiding them. -->
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
+ android:layout_width="0dp"
+ android:layout_height="0dp"
android:orientation="horizontal">
<TextView
android:id="@+id/title_highlights"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_margin="@dimen/activity_stream_base_margin"
--- a/mobile/android/base/java/org/mozilla/gecko/activitystream/homepanel/ActivityStreamPanel.java
+++ b/mobile/android/base/java/org/mozilla/gecko/activitystream/homepanel/ActivityStreamPanel.java
@@ -109,17 +109,17 @@ public class ActivityStreamPanel extends
public void unload() {
adapter.swapHighlights(Collections.<Highlight>emptyList());
adapter.swapTopSitesCursor(null);
}
public void reload(LoaderManager lm) {
- adapter.reset();
+ adapter.clearAndInit();
load(lm);
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
// This code does two things:
--- a/mobile/android/base/java/org/mozilla/gecko/activitystream/homepanel/StreamRecyclerAdapter.java
+++ b/mobile/android/base/java/org/mozilla/gecko/activitystream/homepanel/StreamRecyclerAdapter.java
@@ -89,26 +89,28 @@ public class StreamRecyclerAdapter exten
return type;
}
};
}
public StreamRecyclerAdapter() {
setHasStableIds(true);
recyclerViewModel = new LinkedList<>();
+
+ clearAndInit();
+ }
+
+ public void clearAndInit() {
+ recyclerViewModel.clear();
for (RowItemType type : ACTIVITY_STREAM_SECTIONS) {
recyclerViewModel.add(makeRowModelFromType(type));
}
topStoriesQueue = Collections.emptyList();
}
- public void reset() {
- // TODO: Reset adapter
- }
-
void setOnUrlOpenListeners(HomePager.OnUrlOpenListener onUrlOpenListener, HomePager.OnUrlOpenInBackgroundListener onUrlOpenInBackgroundListener) {
this.onUrlOpenListener = onUrlOpenListener;
this.onUrlOpenInBackgroundListener = onUrlOpenInBackgroundListener;
}
public void setTileSize(int tilesSize) {
this.tilesSize = tilesSize;
notifyDataSetChanged();
@@ -124,27 +126,23 @@ public class StreamRecyclerAdapter exten
@Override
public StreamViewHolder onCreateViewHolder(ViewGroup parent, final int type) {
final LayoutInflater inflater = LayoutInflater.from(parent.getContext());
if (type == RowItemType.TOP_PANEL.getViewType()) {
return new TopPanelRow(inflater.inflate(TopPanelRow.LAYOUT_ID, parent, false), onUrlOpenListener, onUrlOpenInBackgroundListener);
} else if (type == RowItemType.TOP_STORIES_TITLE.getViewType()) {
- final boolean pocketEnabled = GeckoSharedPrefs.forProfile(parent.getContext()).getBoolean(ActivityStreamPanel.PREF_POCKET_ENABLED, true);
- return new StreamTitleRow(inflater.inflate(StreamTitleRow.LAYOUT_ID, parent, false), R.string.activity_stream_topstories, pocketEnabled, R.string.activity_stream_link_more, LINK_MORE_POCKET, onUrlOpenListener);
+ return new StreamTitleRow(inflater.inflate(StreamTitleRow.LAYOUT_ID, parent, false), R.string.activity_stream_topstories, R.string.activity_stream_link_more, LINK_MORE_POCKET, onUrlOpenListener);
} else if (type == RowItemType.TOP_STORIES_ITEM.getViewType()) {
return new WebpageItemRow(inflater.inflate(WebpageItemRow.LAYOUT_ID, parent, false), this);
} else if (type == RowItemType.HIGHLIGHT_ITEM.getViewType()) {
return new WebpageItemRow(inflater.inflate(WebpageItemRow.LAYOUT_ID, parent, false), this);
} else if (type == RowItemType.HIGHLIGHTS_TITLE.getViewType()) {
- final SharedPreferences sharedPreferences = GeckoSharedPrefs.forProfile(parent.getContext());
- final boolean bookmarksEnabled = sharedPreferences.getBoolean(ActivityStreamPanel.PREF_BOOKMARKS_ENABLED, true);
- final boolean visitedEnabled = sharedPreferences.getBoolean(ActivityStreamPanel.PREF_VISITED_ENABLED, true);
- return new StreamTitleRow(inflater.inflate(StreamTitleRow.LAYOUT_ID, parent, false), R.string.activity_stream_highlights, bookmarksEnabled || visitedEnabled);
+ return new StreamTitleRow(inflater.inflate(StreamTitleRow.LAYOUT_ID, parent, false), R.string.activity_stream_highlights);
} else if (type == RowItemType.HIGHLIGHTS_EMPTY_STATE.getViewType()) {
return new HighlightsEmptyStateRow(inflater.inflate(HighlightsEmptyStateRow.LAYOUT_ID, parent, false));
} else {
throw new IllegalStateException("Missing inflation for ViewType " + type);
}
}
/**
@@ -177,16 +175,24 @@ public class StreamRecyclerAdapter exten
if (type == RowItemType.HIGHLIGHT_ITEM.getViewType()) {
final Highlight highlight = (Highlight) recyclerViewModel.get(position);
((WebpageItemRow) holder).bind(highlight, position, tilesSize);
} else if (type == RowItemType.TOP_PANEL.getViewType()) {
((TopPanelRow) holder).bind(topSitesCursor, tilesSize);
} else if (type == RowItemType.TOP_STORIES_ITEM.getViewType()) {
final TopStory story = (TopStory) recyclerViewModel.get(position);
((WebpageItemRow) holder).bind(story, position, tilesSize);
+ } else if (type == RowItemType.HIGHLIGHTS_TITLE.getViewType()) {
+ final SharedPreferences sharedPreferences = GeckoSharedPrefs.forProfile(holder.itemView.getContext());
+ final boolean bookmarksEnabled = sharedPreferences.getBoolean(ActivityStreamPanel.PREF_BOOKMARKS_ENABLED, true);
+ final boolean visitedEnabled = sharedPreferences.getBoolean(ActivityStreamPanel.PREF_VISITED_ENABLED, true);
+ ((StreamTitleRow) holder).setVisible(bookmarksEnabled || visitedEnabled);
+ } else if (type == RowItemType.TOP_STORIES_TITLE.getViewType()) {
+ final boolean pocketEnabled = GeckoSharedPrefs.forProfile(holder.itemView.getContext()).getBoolean(ActivityStreamPanel.PREF_POCKET_ENABLED, true);
+ ((StreamTitleRow) holder).setVisible(pocketEnabled);
}
}
@Override
public void onItemClicked(RecyclerView recyclerView, int position, View v) {
if (!onItemClickIsValidRowItem(position)) {
return;
}
--- a/mobile/android/base/java/org/mozilla/gecko/activitystream/homepanel/stream/StreamTitleRow.java
+++ b/mobile/android/base/java/org/mozilla/gecko/activitystream/homepanel/stream/StreamTitleRow.java
@@ -16,28 +16,25 @@ import org.mozilla.gecko.R;
import org.mozilla.gecko.home.HomePager;
import org.mozilla.gecko.util.DrawableUtil;
import java.util.EnumSet;
public class StreamTitleRow extends StreamViewHolder {
public static final int LAYOUT_ID = R.layout.activity_stream_main_highlightstitle;
- public StreamTitleRow(final View itemView, final @StringRes @NonNull int titleResId, boolean isEnabled) {
+ public StreamTitleRow(final View itemView, final @StringRes @NonNull int titleResId) {
super(itemView);
final TextView titleView = (TextView) itemView.findViewById(R.id.title_highlights);
titleView.setText(titleResId);
- if (!isEnabled) {
- hideView(itemView);
- }
}
- public StreamTitleRow(final View itemView, final @StringRes @NonNull int titleResId, boolean isEnabled,
+ public StreamTitleRow(final View itemView, final @StringRes @NonNull int titleResId,
final @StringRes int linkTitleResId, final String url, final HomePager.OnUrlOpenListener onUrlOpenListener) {
- this(itemView, titleResId, isEnabled);
+ this(itemView, titleResId);
// Android 21+ is needed to set RTL-aware compound drawables, so we use a tinted ImageView here.
final TextView titleLink = (TextView) itemView.findViewById(R.id.title_link);
titleLink.setVisibility(View.VISIBLE);
titleLink.setText(linkTitleResId);
final ImageView titleArrow = (ImageView) itemView.findViewById(R.id.arrow_link);
titleArrow.setImageDrawable(DrawableUtil.tintDrawableWithColorRes(itemView.getContext(), R.drawable.menu_item_more, R.color.ob_click));
titleArrow.setVisibility(View.VISIBLE);
@@ -48,19 +45,23 @@ public class StreamTitleRow extends Stre
onUrlOpenListener.onUrlOpen(url, EnumSet.of(HomePager.OnUrlOpenListener.Flags.ALLOW_SWITCH_TO_TAB));
}
};
titleLink.setOnClickListener(clickListener);
titleArrow.setOnClickListener(clickListener);
}
- private static void hideView(final View itemView) {
- itemView.setVisibility(View.GONE);
- // We also need to set the layout height, width, and margins to 0 for the RecyclerView child.
+ public void setVisible(boolean toShow) {
+ itemView.setVisibility(toShow ? View.VISIBLE : View.GONE);
+ // We also need to set the layout height and width to 0 for the RecyclerView child.
final RecyclerView.LayoutParams layoutParams = (RecyclerView.LayoutParams) itemView.getLayoutParams();
- layoutParams.setMargins(0, 0, 0, 0);
- layoutParams.height = 0;
- layoutParams.width = 0;
+ if (toShow) {
+ layoutParams.height = RecyclerView.LayoutParams.WRAP_CONTENT;
+ layoutParams.width = RecyclerView.LayoutParams.MATCH_PARENT;
+ } else {
+ layoutParams.height = 0;
+ layoutParams.width = 0;
+ }
itemView.setLayoutParams(layoutParams);
}
}