Код: Выделить всё
android:id="@+id/progressBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center" />
, как вы видите, у меня есть Quickhideappbarbehavior для Appbar:
Код: Выделить всё
public class QuickHideAppBarBehavior extends QuickHideBehavior {
private int mActionBarHeight;
//Required to instantiate as a default behavior
@SuppressWarnings("unused")
public QuickHideAppBarBehavior() {
}
//Required to attach behavior via XML
@SuppressWarnings("unused")
public QuickHideAppBarBehavior(Context context, AttributeSet attrs) {
super(context, attrs);
// Calculate ActionBar height
mActionBarHeight = getActionBarHeight(context);
}
@Override
public boolean onNestedFling(@NonNull CoordinatorLayout coordinatorLayout, @NonNull View child,
@NonNull View target, float velocityX, float velocityY,
boolean consumed) {
if (!(target instanceof RecyclerView)) {
return super.onNestedFling(coordinatorLayout, child, target, velocityX, velocityY, consumed);
}
return false;
}
@Override
protected float getTargetHideValue(ViewGroup parent, View target) {
return -target.getHeight();
}
@Override
protected void removeSpace(View view) {
view.setPadding(0, 0, 0, 0);
}
@Override
protected void addSpace(View view) {
mHandler.postDelayed(() -> view.setPadding(0, mActionBarHeight, 0, 0), ADD_SPACE_DELAY);
}
}
< /code>
и его базовый класс: < /p>
public abstract class QuickHideBehavior extends CoordinatorLayout.Behavior {
private static final int DIRECTION_UP = 1;
private static final int DIRECTION_DOWN = -1;
protected static final int ADD_SPACE_DELAY = 100;
protected final Handler mHandler = new Handler(Looper.getMainLooper());
/* Tracking last threshold crossed */
private int mScrollTrigger;
private ObjectAnimator mAnimator;
private int mVelocity;
protected abstract float getTargetHideValue(ViewGroup parent, View target);
protected abstract void removeSpace(View view);
protected abstract void addSpace(View recyclerView);
//Required to instantiate as a default behavior
public QuickHideBehavior() {
}
//Required to attach behavior via XML
public QuickHideBehavior(Context context, AttributeSet attrs) {
super(context, attrs);
mVelocity = (int) (ViewConfiguration.get(context).getScaledMaximumFlingVelocity() / 3.5f);
}
//Called before a nested scroll event. Return true to declare interest
@Override
public boolean onStartNestedScroll(@NonNull CoordinatorLayout coordinatorLayout,
@NonNull View child, @NonNull View directTargetChild,
@NonNull View target, int nestedScrollAxes, int type) {
//We have to declare interest in the scroll to receive further events
return (nestedScrollAxes & ViewCompat.SCROLL_AXIS_VERTICAL) != 0;
}
//Called after the scrolling child handles the fling
@Override
public boolean onNestedFling(@NonNull CoordinatorLayout coordinatorLayout,
@NonNull View child, @NonNull View target, float velocityX,
float velocityY, boolean consumed) {
//We only care when the target view is already handling the fling
if (consumed) {
if (velocityY > 0 && mScrollTrigger != DIRECTION_UP && velocityY > mVelocity &&
coordinatorLayout.findViewById(R.id.recyclerView).canScrollVertically(1)) {
mScrollTrigger = DIRECTION_UP;
restartAnimator(child, getTargetHideValue(coordinatorLayout, child));
removeSpace(target);
} else if (velocityY < 0 && mScrollTrigger != DIRECTION_DOWN &&
velocityY < (float) -mVelocity / 2) {
mScrollTrigger = DIRECTION_DOWN;
restartAnimator(child, 0f);
addSpace(target);
}
}
return false;
}
/* Helper Methods */
//Helper to trigger hide/show animation
private void restartAnimator(View target, float value) {
if (mAnimator != null) {
mAnimator.cancel();
mAnimator = null;
}
mAnimator = ObjectAnimator
.ofFloat(target, View.TRANSLATION_Y, value)
.setDuration(250);
mAnimator.start();
}
}
< /code>
Проблема в том, что я использовал накладку, так как я хочу, чтобы приложение работало по обеим ориентации, у Swiperefreshlayout не хватает места сверху, поэтому я должен использовать: < /p>
binding.swipeRefresh.setProgressViewOffset(true, 0, getActionBarHeight(requireContext()) + SWIPE_REFRESH_OFFSET);
Подробнее здесь: https://stackoverflow.com/questions/796 ... se-padding