Reduce one more layout depth
This commit is contained in:
1 parent
57e69ba7be
commit
f718fc0c89
3 files changed
+149
-70
No files matched your search
@@ -1,32 +1,167 @@
|
||||
package com.hippo.ehviewer.widget;
|
||||
|
||||
import static androidx.core.util.ObjectsCompat.requireNonNull;
|
||||
|
||||
import android.annotation.SuppressLint;
|
||||
import android.content.Context;
|
||||
import android.util.AttributeSet;
|
||||
import android.view.Gravity;
|
||||
import android.view.MotionEvent;
|
||||
import android.view.View;
|
||||
import android.view.ViewParent;
|
||||
import android.widget.FrameLayout;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.coordinatorlayout.widget.CoordinatorLayout;
|
||||
import androidx.core.math.MathUtils;
|
||||
import androidx.core.view.GravityCompat;
|
||||
import androidx.core.view.ViewCompat;
|
||||
import androidx.drawerlayout.widget.DrawerLayout;
|
||||
import androidx.interpolator.view.animation.FastOutSlowInInterpolator;
|
||||
|
||||
import com.drakeet.drawer.FullDraggableHelper;
|
||||
import com.google.android.material.snackbar.Snackbar;
|
||||
import com.hippo.yorozuya.LayoutUtils;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class AboveSnackerLayout extends FrameLayout implements CoordinatorLayout.AttachedBehavior {
|
||||
public class AboveSnackerLayout extends FrameLayout implements CoordinatorLayout.AttachedBehavior, FullDraggableHelper.Callback {
|
||||
@NonNull
|
||||
private final FullDraggableHelper helper;
|
||||
|
||||
private DrawerLayout drawerLayout;
|
||||
|
||||
private List<View> mAboveSnackViewList;
|
||||
|
||||
public AboveSnackerLayout(Context context, AttributeSet attrs) {
|
||||
super(context, attrs);
|
||||
helper = new FullDraggableHelper(context, this);
|
||||
}
|
||||
|
||||
public AboveSnackerLayout(Context context, AttributeSet attrs, int defStyle) {
|
||||
super(context, attrs, defStyle);
|
||||
helper = new FullDraggableHelper(context, this);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onAttachedToWindow() {
|
||||
super.onAttachedToWindow();
|
||||
ensureDrawerLayout();
|
||||
}
|
||||
|
||||
private void ensureDrawerLayout() {
|
||||
ViewParent parent = getParent().getParent();
|
||||
if (!(parent instanceof DrawerLayout)) {
|
||||
throw new IllegalStateException("This " + this + " must be added to a DrawerLayout");
|
||||
}
|
||||
drawerLayout = (DrawerLayout) parent;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onInterceptTouchEvent(MotionEvent event) {
|
||||
return helper.onInterceptTouchEvent(event);
|
||||
}
|
||||
|
||||
@Override
|
||||
@SuppressLint("ClickableViewAccessibility")
|
||||
public boolean onTouchEvent(MotionEvent event) {
|
||||
return helper.onTouchEvent(event);
|
||||
}
|
||||
|
||||
@NonNull
|
||||
@Override
|
||||
public View getDrawerMainContainer() {
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isDrawerOpen(int gravity) {
|
||||
return drawerLayout.isDrawerOpen(gravity);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasEnabledDrawer(int gravity) {
|
||||
return drawerLayout.getDrawerLockMode(gravity) == DrawerLayout.LOCK_MODE_UNLOCKED
|
||||
&& findDrawerWithGravity(gravity) != null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void offsetDrawer(int gravity, float offset) {
|
||||
setDrawerToOffset(gravity, offset);
|
||||
drawerLayout.invalidate();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void smoothOpenDrawer(int gravity) {
|
||||
drawerLayout.openDrawer(gravity, true);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void smoothCloseDrawer(int gravity) {
|
||||
drawerLayout.closeDrawer(gravity, true);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDrawerDragging() {
|
||||
List<DrawerLayout.DrawerListener> drawerListeners = getDrawerListeners();
|
||||
if (drawerListeners != null) {
|
||||
int listenerCount = drawerListeners.size();
|
||||
for (int i = listenerCount - 1; i >= 0; --i) {
|
||||
drawerListeners.get(i).onDrawerStateChanged(DrawerLayout.STATE_DRAGGING);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Nullable
|
||||
protected List<DrawerLayout.DrawerListener> getDrawerListeners() {
|
||||
try {
|
||||
Field field = DrawerLayout.class.getDeclaredField("mListeners");
|
||||
field.setAccessible(true);
|
||||
// noinspection unchecked
|
||||
return (List<DrawerLayout.DrawerListener>) field.get(drawerLayout);
|
||||
} catch (Exception e) {
|
||||
// throw to let developer know the api is changed
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
protected void setDrawerToOffset(int gravity, float offset) {
|
||||
View drawerView = findDrawerWithGravity(gravity);
|
||||
float slideOffsetPercent = MathUtils.clamp(offset / requireNonNull(drawerView).getWidth(), 0f, 1f);
|
||||
try {
|
||||
Method method = DrawerLayout.class.getDeclaredMethod("moveDrawerToOffset", View.class, float.class);
|
||||
method.setAccessible(true);
|
||||
method.invoke(drawerLayout, drawerView, slideOffsetPercent);
|
||||
drawerView.setVisibility(VISIBLE);
|
||||
} catch (Exception e) {
|
||||
// throw to let developer know the api is changed
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
// Copied from DrawerLayout
|
||||
@Nullable
|
||||
private View findDrawerWithGravity(int gravity) {
|
||||
final int absHorizontalGravity = GravityCompat.getAbsoluteGravity(gravity, ViewCompat.getLayoutDirection(drawerLayout)) & Gravity.HORIZONTAL_GRAVITY_MASK;
|
||||
final int childCount = drawerLayout.getChildCount();
|
||||
for (int i = 0; i < childCount; i++) {
|
||||
final View child = drawerLayout.getChildAt(i);
|
||||
final int childAbsGravity = getDrawerViewAbsoluteGravity(child);
|
||||
if ((childAbsGravity & Gravity.HORIZONTAL_GRAVITY_MASK) == absHorizontalGravity) {
|
||||
return child;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
// Copied from DrawerLayout
|
||||
private int getDrawerViewAbsoluteGravity(View drawerView) {
|
||||
final int gravity = ((DrawerLayout.LayoutParams) drawerView.getLayoutParams()).gravity;
|
||||
return GravityCompat.getAbsoluteGravity(gravity, ViewCompat.getLayoutDirection(drawerLayout));
|
||||
}
|
||||
|
||||
public void addAboveSnackView(View view) {
|
||||
|
||||
-47
@@ -1,47 +0,0 @@
|
||||
/*
|
||||
* Copyright 2022 Tarsin Norbin
|
||||
*
|
||||
* This file is part of EhViewer
|
||||
*
|
||||
* EhViewer 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.
|
||||
*
|
||||
* EhViewer 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 EhViewer.
|
||||
* If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package com.hippo.widget;
|
||||
|
||||
import android.content.Context;
|
||||
import android.util.AttributeSet;
|
||||
import android.view.WindowInsets;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
|
||||
import com.drakeet.drawer.FullDraggableContainer;
|
||||
|
||||
public class IgnoreFitsSystemWindowsFullyDraggableDrawerContentLayout extends FullDraggableContainer {
|
||||
public IgnoreFitsSystemWindowsFullyDraggableDrawerContentLayout(@NonNull Context context) {
|
||||
super(context);
|
||||
}
|
||||
|
||||
public IgnoreFitsSystemWindowsFullyDraggableDrawerContentLayout(@NonNull Context context, @Nullable @org.jetbrains.annotations.Nullable AttributeSet attrs) {
|
||||
super(context, attrs);
|
||||
}
|
||||
|
||||
public IgnoreFitsSystemWindowsFullyDraggableDrawerContentLayout(@NonNull Context context, @Nullable @org.jetbrains.annotations.Nullable AttributeSet attrs, int defStyleAttr) {
|
||||
super(context, attrs, defStyleAttr);
|
||||
}
|
||||
|
||||
@Override
|
||||
public WindowInsets onApplyWindowInsets(WindowInsets insets) {
|
||||
return insets;
|
||||
}
|
||||
}
|
||||
@@ -24,34 +24,25 @@
|
||||
app:edgeToEdge="true"
|
||||
app:fitsSystemWindowsInsets="start|end">
|
||||
|
||||
<com.hippo.widget.IgnoreFitsSystemWindowsFullyDraggableDrawerContentLayout
|
||||
<androidx.coordinatorlayout.widget.CoordinatorLayout
|
||||
android:id="@+id/snackbar"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:fitsSystemWindows="true">
|
||||
android:layout_height="match_parent">
|
||||
|
||||
<androidx.coordinatorlayout.widget.CoordinatorLayout
|
||||
android:id="@+id/snackbar"
|
||||
<com.hippo.ehviewer.widget.AboveSnackerLayout
|
||||
android:id="@+id/absnacker"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent">
|
||||
|
||||
<com.hippo.ehviewer.widget.AboveSnackerLayout
|
||||
android:id="@+id/absnacker"
|
||||
<androidx.fragment.app.FragmentContainerView
|
||||
android:id="@+id/fragment_container"
|
||||
android:name="androidx.navigation.fragment.NavHostFragment"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent">
|
||||
|
||||
<androidx.fragment.app.FragmentContainerView
|
||||
android:id="@+id/fragment_container"
|
||||
android:name="androidx.navigation.fragment.NavHostFragment"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
app:defaultNavHost="true"
|
||||
app:navGraph="@navigation/nav_graph" />
|
||||
|
||||
</com.hippo.ehviewer.widget.AboveSnackerLayout>
|
||||
|
||||
</androidx.coordinatorlayout.widget.CoordinatorLayout>
|
||||
|
||||
</com.hippo.widget.IgnoreFitsSystemWindowsFullyDraggableDrawerContentLayout>
|
||||
android:layout_height="match_parent"
|
||||
app:defaultNavHost="true"
|
||||
app:navGraph="@navigation/nav_graph" />
|
||||
</com.hippo.ehviewer.widget.AboveSnackerLayout>
|
||||
</androidx.coordinatorlayout.widget.CoordinatorLayout>
|
||||
|
||||
<com.google.android.material.navigation.NavigationView
|
||||
android:id="@+id/nav_view"
|
||||
|
||||
Reference in new issue
Block a user