This repository has been archived by the owner on Jul 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 217
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
12295cb
commit dc39a84
Showing
20 changed files
with
1,226 additions
and
61 deletions.
There are no files selected for viewing
611 changes: 611 additions & 0 deletions
611
app/src/common/shared/org/mozilla/vrbrowser/ui/views/CustomFastScroller.java
Large diffs are not rendered by default.
Oops, something went wrong.
92 changes: 92 additions & 0 deletions
92
app/src/common/shared/org/mozilla/vrbrowser/ui/views/CustomRecyclerView.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
package org.mozilla.vrbrowser.ui.views; | ||
|
||
import android.content.Context; | ||
import android.content.res.Resources; | ||
import android.content.res.TypedArray; | ||
import android.graphics.drawable.Drawable; | ||
import android.graphics.drawable.StateListDrawable; | ||
import android.util.AttributeSet; | ||
import android.view.ViewGroup; | ||
|
||
import androidx.annotation.NonNull; | ||
import androidx.annotation.Nullable; | ||
import androidx.annotation.VisibleForTesting; | ||
import androidx.recyclerview.widget.RecyclerView; | ||
|
||
import org.mozilla.vrbrowser.R; | ||
|
||
public class CustomRecyclerView extends RecyclerView { | ||
|
||
private CustomFastScroller mFastScroller; | ||
|
||
public CustomRecyclerView(@NonNull Context context) { | ||
this(context, null); | ||
} | ||
|
||
public CustomRecyclerView(@NonNull Context context, @Nullable AttributeSet attrs) { | ||
this(context, attrs, 0); | ||
} | ||
|
||
public CustomRecyclerView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyle) { | ||
super(context, attrs, defStyle); | ||
|
||
if (attrs != null) { | ||
int defStyleRes = 0; | ||
TypedArray a = context.obtainStyledAttributes(attrs, androidx.recyclerview.R.styleable.RecyclerView, | ||
defStyle, defStyleRes); | ||
StateListDrawable verticalThumbDrawable = (StateListDrawable) a | ||
.getDrawable(androidx.recyclerview.R.styleable.RecyclerView_fastScrollVerticalThumbDrawable); | ||
Drawable verticalTrackDrawable = a | ||
.getDrawable(androidx.recyclerview.R.styleable.RecyclerView_fastScrollVerticalTrackDrawable); | ||
StateListDrawable horizontalThumbDrawable = (StateListDrawable) a | ||
.getDrawable(androidx.recyclerview.R.styleable.RecyclerView_fastScrollHorizontalThumbDrawable); | ||
Drawable horizontalTrackDrawable = a | ||
.getDrawable(androidx.recyclerview.R.styleable.RecyclerView_fastScrollHorizontalTrackDrawable); | ||
a.recycle(); | ||
|
||
TypedArray customAttributes = context.obtainStyledAttributes(attrs, R.styleable.CustomRecyclerView, | ||
defStyle, defStyleRes); | ||
boolean alwaysVisible = customAttributes.getBoolean(R.styleable.CustomRecyclerView_android_fastScrollAlwaysVisible, false); | ||
boolean enabled = customAttributes.getBoolean(R.styleable.CustomRecyclerView_customFastScrollEnabled, false); | ||
customAttributes.recycle(); | ||
if (enabled) { | ||
initFastScroller(alwaysVisible, verticalThumbDrawable, verticalTrackDrawable, horizontalThumbDrawable, horizontalTrackDrawable); | ||
} | ||
|
||
} else { | ||
setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS); | ||
} | ||
|
||
getViewTreeObserver().addOnGlobalLayoutListener(() -> { | ||
if (getVisibility() == VISIBLE) { | ||
mFastScroller.updateScrollPosition(computeHorizontalScrollOffset(), | ||
computeVerticalScrollOffset()); | ||
} | ||
}); | ||
} | ||
|
||
String exceptionLabel() { | ||
return " " + super.toString() | ||
+ ", adapter:" + getAdapter() | ||
+ ", layout:" + getLayoutManager() | ||
+ ", context:" + getContext(); | ||
} | ||
|
||
@VisibleForTesting | ||
void initFastScroller(boolean alwaysVisible, StateListDrawable verticalThumbDrawable, | ||
Drawable verticalTrackDrawable, StateListDrawable horizontalThumbDrawable, | ||
Drawable horizontalTrackDrawable) { | ||
if (verticalThumbDrawable == null || verticalTrackDrawable == null | ||
|| horizontalThumbDrawable == null || horizontalTrackDrawable == null) { | ||
throw new IllegalArgumentException( | ||
"Trying to set fast scroller without both required drawables." + exceptionLabel()); | ||
} | ||
|
||
Resources resources = getContext().getResources(); | ||
mFastScroller = new CustomFastScroller(this, verticalThumbDrawable, verticalTrackDrawable, | ||
horizontalThumbDrawable, horizontalTrackDrawable, | ||
resources.getDimensionPixelSize(R.dimen.fastscroll_default_thickness), | ||
resources.getDimensionPixelSize(R.dimen.fastscroll_minimum_range), | ||
resources.getDimensionPixelOffset(R.dimen.fastscroll_margin), alwaysVisible); | ||
} | ||
} |
Oops, something went wrong.