在我的活动中,我必须创建一个自定义的Web视图,所以,我把它,我想在相同的活动中得到一个FAB,但我没有成功。这是我的班级代码:
package com.mydomain.app;
import android.app.Activity;
import android.content.Context;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;
import android.util.AttributeSet;
import android.view.View;
import android.webkit.WebView;
public class CustomWebView extends WebView {
FloatingActionButton fab;
public CustomWebView(Context context, AttributeSet attrs) {
super(context, attrs);
Activity activity = (Activity) context;
fab = (FloatingActionButton) activity.findViewById(R.id.fab);
}
@Override
protected void onScrollChanged(int l, int t, int oldl, int oldt) {
super.onScrollChanged(l, t, oldl, oldt);
if(t >= oldt)
fab.hide();
if(t < oldt)
fab.show();
}
}
当触发onScrollChanged时,应用程序会中断。我怎样才能让这个FAB显示或隐藏它?
修改1:
编辑2:
以下是我使用此课程的活动:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<RelativeLayout
android:id="@+id/loading_panel"
android:layout_centerInParent="true"
android:elevation="4dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="visible"
android:clickable="true">
<ProgressBar
style="@android:style/Widget.Material.ProgressBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/corners"
android:id="@+id/progressBar2"
android:indeterminate="true"
android:layout_centerInParent="true"
android:indeterminateTintMode="src_atop"
android:indeterminateTint="@color/colorAccent" />
</RelativeLayout>
<android.support.design.widget.CoordinatorLayout
android:id="@+id/webViewContainer"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|right"
app:backgroundTint="@color/colorAccent"
app:fabSize="normal"
android:src="@drawable/ic_apps"
app:layout_anchor="@+id/webView"
android:layout_margin="16dp"
app:layout_anchorGravity="bottom|right|end"
android:minHeight="179dp" />
<android.support.v4.widget.SwipeRefreshLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/swipeContainer"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.usefashion.useapp.CustomWebView
android:id="@+id/webView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:elevation="0dp"
android:clickable="true">
</com.usefashion.useapp.CustomWebView>
</android.support.v4.widget.SwipeRefreshLayout>
</android.support.design.widget.CoordinatorLayout>
</RelativeLayout>