Fix scrolling comments list

AppBarLayout mostly gets it, but we still need to uphold our own part -
expanding it back after focus returns to it
This commit is contained in:
Alexander 2019-09-20 16:02:16 +07:00 committed by Alexander--
parent eeeeeef3a7
commit eaa1179572
3 changed files with 94 additions and 1 deletions

View File

@ -1,10 +1,13 @@
package com.google.android.material.appbar;
import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.OverScroller;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.coordinatorlayout.widget.CoordinatorLayout;
@ -13,10 +16,46 @@ import java.lang.reflect.Field;
// check this https://stackoverflow.com/questions/56849221/recyclerview-fling-causes-laggy-while-appbarlayout-is-scrolling/57997489#57997489
public final class FlingBehavior extends AppBarLayout.Behavior {
private final Rect focusScrollRect = new Rect();
public FlingBehavior(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public boolean onRequestChildRectangleOnScreen(@NonNull CoordinatorLayout coordinatorLayout, @NonNull AppBarLayout child, @NonNull Rect rectangle, boolean immediate) {
focusScrollRect.set(rectangle);
coordinatorLayout.offsetDescendantRectToMyCoords(child, focusScrollRect);
int height = coordinatorLayout.getHeight();
if (focusScrollRect.top <= 0 && focusScrollRect.bottom >= height) {
// the child is too big to fit inside ourselves completely, ignore request
return false;
}
int offset = getTopAndBottomOffset();
int dy;
if (focusScrollRect.bottom > height) {
dy = focusScrollRect.top;
} else if (focusScrollRect.top < 0) {
// scrolling up
dy = -(height - focusScrollRect.bottom);
} else {
// nothing to do
return false;
}
//int newOffset = offset + dy;
int consumed = scroll(coordinatorLayout, child, dy, getMaxDragOffset(child), 0);
return consumed == dy;
}
@Override
public boolean onInterceptTouchEvent(CoordinatorLayout parent, AppBarLayout child, MotionEvent ev) {
switch (ev.getActionMasked()) {

View File

@ -34,6 +34,7 @@ import org.schabi.newpipe.util.NavigationHelper;
import org.schabi.newpipe.util.OnClickGesture;
import org.schabi.newpipe.util.StateSaver;
import org.schabi.newpipe.util.StreamDialogEntry;
import org.schabi.newpipe.views.SuperScrollLayoutManager;
import java.util.List;
import java.util.Queue;
@ -147,7 +148,7 @@ public abstract class BaseListFragment<I, N> extends BaseStateFragment<I> implem
}
protected RecyclerView.LayoutManager getListLayoutManager() {
return new LinearLayoutManager(activity);
return new SuperScrollLayoutManager(activity);
}
protected RecyclerView.LayoutManager getGridLayoutManager() {

View File

@ -0,0 +1,53 @@
/*
* Copyright (C) Eltex ltd 2019 <eltex@eltex-co.ru>
* SuperScrollLayoutManager.java is part of NewPipe.
*
* NewPipe is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* NewPipe is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with NewPipe. If not, see <http://www.gnu.org/licenses/>.
*/
package org.schabi.newpipe.views;
import android.content.Context;
import android.graphics.Rect;
import android.view.FocusFinder;
import android.view.View;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
public final class SuperScrollLayoutManager extends LinearLayoutManager {
private final Rect handy = new Rect();
public SuperScrollLayoutManager(Context context) {
super(context);
}
@Override
public boolean requestChildRectangleOnScreen(@NonNull RecyclerView parent, @NonNull View child, @NonNull Rect rect, boolean immediate, boolean focusedChildVisible) {
if (!parent.isInTouchMode()) {
// only activate when in directional navigation mode (Android TV etc) fine grained
// touch scrolling is better served by nested scroll system
if (!focusedChildVisible || getFocusedChild() == child) {
handy.set(rect);
parent.offsetDescendantRectToMyCoords(child, handy);
parent.requestRectangleOnScreen(handy, immediate);
}
}
return super.requestChildRectangleOnScreen(parent, child, rect, immediate, focusedChildVisible);
}
}