Skip to content
This repository has been archived by the owner on Jul 22, 2024. It is now read-only.

Commit

Permalink
Fix non HW accelerated Android 8 hover issues (#3173)
Browse files Browse the repository at this point in the history
  • Loading branch information
keianhzo authored Apr 16, 2020
1 parent f41255c commit e32c2f1
Show file tree
Hide file tree
Showing 7 changed files with 110 additions and 64 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ public int getItemViewType(int position) {
return 0;
}

@SuppressLint("ClickableViewAccessibility")
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
if (viewType == BookmarkNodeType.ITEM.ordinal()) {
Expand All @@ -166,15 +166,52 @@ public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int
binding.setCallback(mBookmarkItemCallback);
binding.setIsHovered(false);
binding.setIsNarrow(mIsNarrowLayout);

return new BookmarkViewHolder(binding);

} else if (viewType == BookmarkNodeType.FOLDER.ordinal()) {
BookmarkItemFolderBinding binding = DataBindingUtil
.inflate(LayoutInflater.from(parent.getContext()), R.layout.bookmark_item_folder,
parent, false);
binding.setCallback(mBookmarkItemFolderCallback);

return new BookmarkFolderViewHolder(binding);

} else if (viewType == BookmarkNodeType.SEPARATOR.ordinal()) {
BookmarkSeparatorBinding binding = DataBindingUtil
.inflate(LayoutInflater.from(parent.getContext()), R.layout.bookmark_separator,
parent, false);

return new BookmarkSeparatorViewHolder(binding);
}

throw new IllegalArgumentException("Invalid view Type");
}

@SuppressLint("ClickableViewAccessibility")
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
Bookmark item = mDisplayList.get(position);

if (holder instanceof BookmarkViewHolder) {
BookmarkViewHolder bookmarkHolder = (BookmarkViewHolder) holder;
BookmarkItemBinding binding = bookmarkHolder.binding;
binding.setItem(item);
binding.setIsNarrow(mIsNarrowLayout);
binding.layout.setOnHoverListener((view, motionEvent) -> {
int ev = motionEvent.getActionMasked();
switch (ev) {
case MotionEvent.ACTION_HOVER_ENTER:
binding.setIsHovered(true);
view.getBackground().setState(new int[]{android.R.attr.state_hovered});
view.postInvalidate();
return false;

case MotionEvent.ACTION_CANCEL:
case MotionEvent.ACTION_HOVER_EXIT:
view.getBackground().setState(new int[]{android.R.attr.state_active});
binding.setIsHovered(false);
view.postInvalidate();
return false;
}

Expand Down Expand Up @@ -245,40 +282,27 @@ public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int
return false;
});

return new BookmarkViewHolder(binding);

} else if (viewType == BookmarkNodeType.FOLDER.ordinal()) {
BookmarkItemFolderBinding binding = DataBindingUtil
.inflate(LayoutInflater.from(parent.getContext()), R.layout.bookmark_item_folder,
parent, false);
binding.setCallback(mBookmarkItemFolderCallback);

return new BookmarkFolderViewHolder(binding);

} else if (viewType == BookmarkNodeType.SEPARATOR.ordinal()) {
BookmarkSeparatorBinding binding = DataBindingUtil
.inflate(LayoutInflater.from(parent.getContext()), R.layout.bookmark_separator,
parent, false);

return new BookmarkSeparatorViewHolder(binding);
}

throw new IllegalArgumentException("Invalid view Type");
}

@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
Bookmark item = mDisplayList.get(position);

if (holder instanceof BookmarkViewHolder) {
BookmarkViewHolder bookmarkHolder = (BookmarkViewHolder) holder;
bookmarkHolder.binding.setItem(item);
bookmarkHolder.binding.setIsNarrow(mIsNarrowLayout);

} else if (holder instanceof BookmarkFolderViewHolder) {
BookmarkFolderViewHolder bookmarkHolder = (BookmarkFolderViewHolder) holder;
bookmarkHolder.binding.setItem(item);
bookmarkHolder.binding.executePendingBindings();
bookmarkHolder.binding.layout.setOnHoverListener((view, motionEvent) -> {
int ev = motionEvent.getActionMasked();
switch (ev) {
case MotionEvent.ACTION_HOVER_ENTER:
view.getBackground().setState(new int[]{android.R.attr.state_hovered});
view.postInvalidate();
return false;

case MotionEvent.ACTION_CANCEL:
case MotionEvent.ACTION_HOVER_EXIT:
view.getBackground().setState(new int[]{android.R.attr.state_active});
view.postInvalidate();
return false;
}

return false;
});

} else if (holder instanceof BookmarkSeparatorViewHolder) {
BookmarkSeparatorViewHolder bookmarkHolder = (BookmarkSeparatorViewHolder) holder;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ public int getItemPosition(long id) {
return 0;
}

@SuppressLint("ClickableViewAccessibility")
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
DownloadItemBinding binding = DataBindingUtil
Expand All @@ -129,15 +129,31 @@ public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int
binding.setCallback(mDownloadItemCallback);
binding.setIsHovered(false);
binding.setIsNarrow(mIsNarrowLayout);

return new DownloadItemViewHolder(binding);
}

@SuppressLint("ClickableViewAccessibility")
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
DownloadItemViewHolder item = (DownloadItemViewHolder) holder;
DownloadItemBinding binding = item.binding;
item.binding.setItem(mDownloadsList.get(position));
item.binding.setIsNarrow(mIsNarrowLayout);
binding.layout.setOnHoverListener((view, motionEvent) -> {
int ev = motionEvent.getActionMasked();
switch (ev) {
case MotionEvent.ACTION_HOVER_ENTER:
binding.setIsHovered(true);
view.getBackground().setState(new int[]{android.R.attr.state_hovered});
view.postInvalidate();
return false;

case MotionEvent.ACTION_CANCEL:
case MotionEvent.ACTION_HOVER_EXIT:
view.getBackground().setState(new int[]{android.R.attr.state_active});
binding.setIsHovered(false);
view.postInvalidate();
return false;
}

Expand Down Expand Up @@ -207,15 +223,6 @@ public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int
}
return false;
});

return new DownloadItemViewHolder(binding);
}

@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
DownloadItemViewHolder item = (DownloadItemViewHolder) holder;
item.binding.setItem(mDownloadsList.get(position));
item.binding.setIsNarrow(mIsNarrowLayout);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ public int getItemPosition(long id) {
return 0;
}

@SuppressLint("ClickableViewAccessibility")
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
if (viewType == TYPE_ITEM) {
Expand All @@ -137,15 +137,44 @@ public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int
binding.setCallback(mHistoryItemCallback);
binding.setIsHovered(false);
binding.setIsNarrow(mIsNarrowLayout);

return new HistoryItemViewHolder(binding);

} else if (viewType == TYPE_HEADER){
HistoryItemHeaderBinding binding = DataBindingUtil
.inflate(LayoutInflater.from(parent.getContext()), R.layout.history_item_header,
parent, false);

return new HistoryItemViewHeaderHolder(binding);
}

throw new RuntimeException("there is no type that matches the type " + viewType + " + make sure your using types correctly");
}

@SuppressLint("ClickableViewAccessibility")
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
VisitInfo item = mHistoryList.get(position);

if (holder instanceof HistoryItemViewHolder) {
HistoryItemViewHolder historyHolder = (HistoryItemViewHolder) holder;
HistoryItemBinding binding = historyHolder.binding;
binding.setItem(item);
binding.setIsNarrow(mIsNarrowLayout);
binding.layout.setOnHoverListener((view, motionEvent) -> {
int ev = motionEvent.getActionMasked();
switch (ev) {
case MotionEvent.ACTION_HOVER_ENTER:
binding.setIsHovered(true);
view.getBackground().setState(new int[]{android.R.attr.state_hovered});
view.postInvalidate();
return false;

case MotionEvent.ACTION_CANCEL:
case MotionEvent.ACTION_HOVER_EXIT:
view.getBackground().setState(new int[]{android.R.attr.state_active});
binding.setIsHovered(false);
view.postInvalidate();
return false;
}

Expand Down Expand Up @@ -216,29 +245,9 @@ public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int
return false;
});

return new HistoryItemViewHolder(binding);

} else if (viewType == TYPE_HEADER){
HistoryItemHeaderBinding binding = DataBindingUtil
.inflate(LayoutInflater.from(parent.getContext()), R.layout.history_item_header,
parent, false);

return new HistoryItemViewHeaderHolder(binding);
}

throw new RuntimeException("there is no type that matches the type " + viewType + " + make sure your using types correctly");
}

@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
if (holder instanceof HistoryItemViewHolder) {
HistoryItemViewHolder item = (HistoryItemViewHolder) holder;
item.binding.setItem(mHistoryList.get(position));
item.binding.setIsNarrow(mIsNarrowLayout);

} else if (holder instanceof HistoryItemViewHeaderHolder) {
HistoryItemViewHeaderHolder item = (HistoryItemViewHeaderHolder) holder;
item.binding.setTitle(mHistoryList.get(position).getTitle());
HistoryItemViewHeaderHolder historyHolder = (HistoryItemViewHeaderHolder) holder;
historyHolder.binding.setTitle(item.getTitle());
}
}

Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/layout/bookmark_item.xml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
android:layout_centerVertical="true"
android:gravity="center_vertical"
android:layout_toStartOf="@id/buttons_container"
android:addStatesFromChildren="true"
android:orientation="@{isNarrow ? LinearLayout.VERTICAL : LinearLayout.HORIZONTAL}">

<TextView
Expand Down
3 changes: 3 additions & 0 deletions app/src/main/res/layout/bookmark_item_folder.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
</data>

<LinearLayout
android:id="@+id/layout"
android:layout_width="match_parent"
android:layout_height="@dimen/library_item_row_height"
android:orientation="vertical"
Expand All @@ -23,6 +24,8 @@
app:leftMargin="@{item.level*100}"
android:paddingStart="10dp"
android:background="@drawable/library_item_background_color"
android:clickable="true"
android:focusable="true"
android:onClick="@{(view) -> callback.onClick(view, item)}">

<TextView
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/layout/download_item.xml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
android:layout_centerVertical="true"
android:layout_toStartOf="@id/time_buttons"
android:gravity="center_vertical"
android:addStatesFromChildren="true"
android:orientation="@{isNarrow ? LinearLayout.VERTICAL : LinearLayout.HORIZONTAL}">

<TextView
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/layout/history_item.xml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
android:layout_centerVertical="true"
android:layout_toStartOf="@id/time_buttons"
android:gravity="center_vertical"
android:addStatesFromChildren="true"
android:orientation="@{isNarrow ? LinearLayout.VERTICAL : LinearLayout.HORIZONTAL}">

<TextView
Expand Down

0 comments on commit e32c2f1

Please sign in to comment.