Skip to content

Instantly share code, notes, and snippets.

@yuriyskulskiy
yuriyskulskiy / NestedScrollView.java
Last active July 28, 2020 00:18
Overscrolling: add pulling hand
...
private Bitmap mBitmap;
...
private void initScrollView() {
...
Resources res = getResources();
mBitmap = BitmapFactory.decodeResource(res, R.drawable.hand_rope);
mBitmap = Bitmap.createScaledBitmap(mBitmap, mBitmap.getWidth() / 2,
mBitmap.getHeight() / 2, false);
}
@yuriyskulskiy
yuriyskulskiy / activity_main.xml
Created July 27, 2020 23:42
Overscrolling: add background and elevation
...
<com.example.replica.NestedScrollView
android:layout_height="match_parent"
android:layout_width="match_parent"
android:background="@color/overscroll_background">
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:elevation="8dp"
@yuriyskulskiy
yuriyskulskiy / NestedScrollView.java
Created July 27, 2020 23:01
Overscrolling: remove edge effect
private boolean useEdgeEffect = false;
...
@Override
public boolean onTouchEvent(MotionEvent ev) {
...
// if (canOverscroll) {
if (useEdgeEffect) {
deltaY -= mScrollConsumed[1];
@yuriyskulskiy
yuriyskulskiy / NestedScrollView.java
Created July 27, 2020 22:33
Overscrolling: modify overScrollByCompat() with dragging check
boolean overScrollByCompat(int deltaX, int deltaY,
int scrollX, int scrollY,
int scrollRangeX, int scrollRangeY,
int maxOverScrollX, int maxOverScrollY,
boolean isTouchEvent) {
...
// if (clampedY && !hasNestedScrollingParent(ViewCompat.TYPE_NON_TOUCH)) {
if (clampedY && !hasNestedScrollingParent(ViewCompat.TYPE_NON_TOUCH)
&& !mIsBeingDragged) {
mScroller.springBack(newScrollX, newScrollY, 0, 0, 0, getScrollRange());
@yuriyskulskiy
yuriyskulskiy / NestedScrollView.java
Created July 27, 2020 19:20
Overscroll: fix fling method
public void fling(int velocityY) {
if (getChildCount() > 0) {
// standart implementation:
// mScroller.fling(getScrollX(), getScrollY(), // start
// 0, velocityY, // velocities
// 0, 0, // x
// Integer.MIN_VALUE, Integer.MAX_VALUE, // y
// 0, 0); // overscroll
// fixed implementation:
int height = getHeight() - getPaddingBottom() - getPaddingTop();
@yuriyskulskiy
yuriyskulskiy / NestedScrollView.java
Last active July 27, 2020 19:04
Overscroll: use mOverflingDistance in computeScroll() method
@Override
public void computeScroll() {
...
overScrollByCompat(0, unconsumed, getScrollX(),
oldScrollY, 0, range, 0, mOverflingDistance, false);
...
}
@yuriyskulskiy
yuriyskulskiy / NestedScrollView.java
Created July 27, 2020 18:53
Overscroll: update onTouch() with mOverscrollDistance
@Override
public boolean onTouchEvent(MotionEvent ev) {
...
case MotionEvent.ACTION_MOVE:
...
// Calling overScrollByCompat will call onOverScrolled, which
// calls onScrollChanged if applicable.
if (overScrollByCompat(0, deltaY, 0, getScrollY(), 0, range, 0,
mOverscrollDistance, true) && !hasNestedScrollingParent(ViewCompat.TYPE_TOUCH)) {
@yuriyskulskiy
yuriyskulskiy / NestedScrollView.java
Created July 27, 2020 16:11
Overscrolling starter
public class NestedScrollView extends FrameLayout implements NestedScrollingParent3,
NestedScrollingChild3, ScrollingView {
static final int ANIMATED_SCROLL_GAP = 250;
static final float MAX_SCROLL_FACTOR = 0.5f;
private static final String TAG = "NestedScrollView";
/**
* Interface definition for a callback to be invoked when the scroll
@yuriyskulskiy
yuriyskulskiy / ExpandableTextView.java
Last active April 30, 2023 11:57
ExpandableTV with static layout, solution code
public class ExpandableTextView extends AppCompatTextView
implements View.OnClickListener {
private final int COLLAPSED_MAX_LINES = 3;
private final static String POSTFIX = "...see more ";
private ValueAnimator mAnimator;
private boolean isCollapsing;
private CharSequence mOriginalText;
@yuriyskulskiy
yuriyskulskiy / ExpandableTextView.java
Created July 15, 2020 06:44
ExpandableTv fix ellipsize bug
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
super.onLayout(changed, left, top, right, bottom);
if (getLineCount() <= COLLAPSED_MAX_LINES) {
deEllipsize(); // add to fix current bug
setClickable(false);
} else {
setClickable(true);
if (!animator.isRunning() && isCollapsed()) {
ellipsizeColored();