如何获取webView的滚动位置

时间:2013-06-13 18:12:42

标签: android

我要做的是将我的webView滚动位置设置为当它不在顶部时,使我的标题不透明。我已经尝试了很多东西来获取我的滚动位置,比如实现OnScrollListener和OnScrollChangedListener到我的活动,但是任何人都可以工作,我不知道该怎么做。我的活动中有以下xml布局

   <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        tools:context=".BrowserActivity"
         >

    <LinearLayout
        android:id="@+id/header"
        android:layout_width="fill_parent"
        android:layout_height="44dp"
        android:background="@drawable/header" >

        <TextView
                android:id="@+id/browser_title"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:maxLines="1"
                android:layout_gravity="center"
                android:gravity="center"
                android:layout_marginLeft="@dimen/margin_left_browser_title"
                android:layout_marginRight="@dimen/margin_right_browser_title"
                android:text="@string/empty"
                android:textStyle="bold"
                android:textSize="@dimen/txt_size_browser_title"
                android:textColor="@color/white"
                />

        <ProgressBar
            android:id="@+id/progressUrl"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="@dimen/margin_left_browser_progress"
            android:layout_marginTop="5dp"
            android:layout_marginBottom="5dp"  />

        <Button
            android:id="@+id/back"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="@dimen/margin_left_browser_progress" 
            android:layout_marginTop="3dp"
            android:layout_marginBottom="3dp"
            android:background="@drawable/btn_navigation_left"
            />

        <Button
            android:id="@+id/forward"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="3dp"
            android:layout_marginBottom="3dp"
            android:layout_marginRight="@dimen/margin_right_btn_download"
            android:background="@drawable/btn_navigation_right"
            />
    </LinearLayout>

         <WebView
             android:id="@+id/webView1"
             android:layout_below="@id/header"
             android:layout_width="fill_parent"
             android:layout_height="fill_parent"
             />

    </RelativeLayout>

以下活动:

public class BrowserActivity extends Activity {

    String url;
    WebView webView;
    ProgressBar progressUrl;

@Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_browser);

        progressUrl = (ProgressBar) findViewById(R.id.progressUrl);
        Bundle extras = getIntent().getExtras();

        if (extras != null) {

            metaDado = extras.getParcelable("metaDado");
        }

        TextView browserTitle = (TextView) findViewById(R.id.browser_title);

        browserTitle.setText(metaDado.getName());

        webView = (WebView) findViewById(R.id.webView1);

        webView.getSettings().setJavaScriptEnabled(true);
        webView.getSettings().setBuiltInZoomControls(true);
        webView.getSettings().setSupportZoom(true);
        webView.getSettings().setUseWideViewPort(true);
        webView.getSettings().setLoadWithOverviewMode(true);

        if (metaDado.getType().equals(Constants.FULL)) {

            url = "file://" + Constants.DEST_PATH_PARENT + "/" + metaDado.getUuidInstituicao() + "/full/"
                    + metaDado.getOfflineUuid() + "/index.html";

        } else if (metaDado.getType().equals(Constants.PRES)) {

            url = "file://" + Constants.DEST_PATH_PARENT + "/" + metaDado.getUuidInstituicao() + "/pres/"
                    + metaDado.getOfflineUuid() + "/" + metaDado.getOfflineUuid() + "/index.html";
        }

        webView.setWebViewClient(new WebViewClient() {

            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {

                progressUrl.setVisibility(View.VISIBLE);

                super.onPageStarted(view, url, favicon);
            }

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {

                view.loadUrl(url);
                return true;
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);

                progressUrl.setVisibility(View.GONE);
            }

        });

        webView.loadUrl(url);

    }
}

1 个答案:

答案 0 :(得分:0)

我所做的是创建一个覆盖onScrollChanged方法的自定义WebView。您可以看到here

的完整代码