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

Commit

Permalink
Scrollbars standarization (#1925)
Browse files Browse the repository at this point in the history
  • Loading branch information
keianhzo authored and bluemarvin committed Oct 7, 2019
1 parent 12295cb commit dc39a84
Show file tree
Hide file tree
Showing 20 changed files with 1,226 additions and 61 deletions.

Large diffs are not rendered by default.

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);
}
}
Loading

0 comments on commit dc39a84

Please sign in to comment.