美文网首页
实现web置顶效果

实现web置顶效果

作者: 安卓_背包客 | 来源:发表于2018-03-16 15:28 被阅读0次
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:orientation="vertical"
    tools:context="com.lawyee.mycelue.web.Main4Activity">

    <LinearLayout

        android:layout_width="match_parent"
        android:layout_height="56dp"
        android:background="@color/colorPrimaryDark"
        android:orientation="horizontal">

        <Button
            android:id="@+id/btn_back"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="返回" />

        <TextView
          android:textColor="@android:color/white"
            android:layout_gravity="center|center_horizontal"
            android:gravity="center"
            android:maxLength="8"
            android:ellipsize="end"
            android:id="@+id/tv_title"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1" />

        <Button
            android:id="@+id/btn_refresh"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="刷新" />
    </LinearLayout>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <com.lawyee.mycelue.web.MyWebView
            android:id="@+id/web_content"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1">

        </com.lawyee.mycelue.web.MyWebView>

        <Button
            android:id="@+id/btn_top"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_alignParentRight="true"
            android:layout_marginBottom="16dp"
            android:layout_marginRight="16dp"
            android:text="置顶"/>
    </RelativeLayout>
</LinearLayout>

image.png
  • 自定义webview滑动接口
  @Override
    protected void onScrollChanged(int l, int t, int oldl, int oldt) {
        super.onScrollChanged(l, t, oldl, oldt);
        if (scrollChangedCallback!=null){
            scrollChangedCallback.onScroll(l-oldl,t-oldt);
        }

    }

    public OnScrollChangedCallback scrollChangedCallback;

    public void setScrollChangedCallback(OnScrollChangedCallback scrollChangedCallback) {
        this.scrollChangedCallback = scrollChangedCallback;
    }

    public interface OnScrollChangedCallback{
        public void onScroll(int dx,int dy);
    }

实现的activity

    private void initView() {
        WebSettings settings = webContent.getSettings();
        settings.setJavaScriptEnabled(true);
        settings.setSupportZoom(true);
        settings.setBuiltInZoomControls(true);
        settings.setLoadsImagesAutomatically(true);
        settings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
        settings.setUseWideViewPort(true);
        btnTop.setVisibility(View.GONE);
        settings.setLoadWithOverviewMode(true);
        webContent.loadUrl("http://www.baidu.com");
        webContent.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                return super.shouldOverrideUrlLoading(view, url);
            }
        });
   //设置标题
        webContent.setWebChromeClient(new WebChromeClient(){
            @Override
            public void onReceivedTitle(WebView view, String title) {
                super.onReceivedTitle(view, title);
                tvTitle.setText(title);
            }
        });
        //处理滑动时是否显示置顶按钮
        webContent.setScrollChangedCallback(new MyWebView.OnScrollChangedCallback() {

           @Override
            public void onScroll(int dx, int dy) {
                if (dy > 0) {
                    btnTop.setVisibility(View.VISIBLE);
                } else {
                    btnTop.setVisibility(View.GONE);
                }
            }

        });

    }
//    public boolean onKeyDown(int keyCode, KeyEvent event) {
//        if ((keyCode == KEYCODE_BACK) && webContent.canGoBack()) {
//            webContent.goBack();
//            return true;
//        }
//        return super.onKeyDown(keyCode, event);
//    }

    @Override
    public void onBackPressed() {
        if (webContent.canGoBack()) {
            webContent.goBack();
        } else {
            super.onBackPressed();
        }

    }

    @OnClick({R.id.btn_back, R.id.btn_refresh, R.id.btn_top})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_back:
                if (webContent.canGoBack()) {
                    webContent.goBack();
                } else {
                    super.onBackPressed();
                }
                break;
       
            case R.id.btn_refresh:
                   webContent.reload();
                break;
            case R.id.btn_top://置顶按钮
                webContent.scrollTo(0,0);
                btnTop.setVisibility(View.GONE);
                break;
        }
    }
20180315_154231 (1).gif

相关文章

网友评论

      本文标题:实现web置顶效果

      本文链接:https://www.haomeiwen.com/subject/ibyhqftx.html