gpt4 book ai didi

android - 如何在不使用 nestedScrollView 的情况下在 webview 滚动时隐藏和显示 FAB

转载 作者:行者123 更新时间:2023-11-29 14:30:32 25 4
gpt4 key购买 nike

这是我的 XML 布局中的 float 操作按钮。

我在 NestedScrollView 中的 Webview 导致双指缩放支持崩溃。

<android.support.design.widget.FloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:src="@drawable/filter_icon"
android:tint="#FFFFFF"
app:backgroundTint="@color/colorPrimary"
app:elevation="4dp"
app:fabSize="normal"
app:useCompatPadding="true"
app:layout_anchorGravity="bottom|end|right"
app:layout_behavior=".ScrollAwareFABBehavior">
</android.support.design.widget.FloatingActionButton>

最佳答案

Full working code to hide and show FAB on scroll of webview without using NestedScrollView.

public class NestedWebView extends WebView {

private OnScrollChangedCallback mOnScrollChangedCallback;

public NestedWebView(Context context) {
super(context);
}

public NestedWebView(Context context, AttributeSet attrs) {
super(context, attrs);
}

public NestedWebView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}

@Override
protected void onScrollChanged(int l, int t, int oldl, int oldt) {
super.onScrollChanged(l, t, oldl, oldt);
if (mOnScrollChangedCallback != null) {
mOnScrollChangedCallback.onScrollChange(this, l, t, oldl, oldt);
}

}

public void setOnScrollChangedCallback(final OnScrollChangedCallback mOnScrollChangedCallback) {
this.mOnScrollChangedCallback = mOnScrollChangedCallback;
}

public OnScrollChangedCallback getOnScrollChangedCallback() {
return mOnScrollChangedCallback;
}

public interface OnScrollChangedCallback {
/**
* Called when the scroll position of a view changes.
*
* @param v The view whose scroll position has changed.
* @param scrollX Current horizontal scroll origin.
* @param scrollY Current vertical scroll origin.
* @param oldScrollX Previous horizontal scroll origin.
* @param oldScrollY Previous vertical scroll origin.
*/
void onScrollChange(WebView v, int scrollX, int scrollY, int oldScrollX, int oldScrollY);
}
}

In xml use custom Webview like this,

<com.essence.linuxcommands.NestedWebView
android:id="@+id/myWebView"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:elevation="@dimen/card_margin"
android:scrollbarStyle="outsideOverlay"
android:shadowRadius="2"
android:visibility="gone"
/>

In Activity or fragment

NestedWebView webview = (NestedWebView) rootView.findViewById(R.id.myWebView);
webview.setOnScrollChangedCallback(new NestedWebView.OnScrollChangedCallback() {
@Override
public void onScrollChange(WebView v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
if (scrollY > oldScrollY && scrollY > 0) {
fab.hide();
}
if (scrollY < oldScrollY) {
fab.show();
}
}
});

关于android - 如何在不使用 nestedScrollView 的情况下在 webview 滚动时隐藏和显示 FAB,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37893937/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com