我想在滚动webview时隐藏Action Bar

时间:2017-04-07 10:17:59

标签: android android-fragments webview

基本上我有BottomNavigationView活动,因为有3个片段(tab1,tab2,tab3)包含3个webview,我实际想要做的是向下滚动我的操作栏应该隐藏的webview,如getSupportActionBar().hide(); i知道已经有相同问题的堆栈溢出的答案,但我不知道在我的情况下该怎么做,因为我是Android开发的初学者

这是我的 activity_main.xml

<?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:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.hackerinside.jaisonjoseph.polysocial.MainActivity">

<FrameLayout
    android:id="@+id/content"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:background="@android:color/holo_blue_dark">

    <TextView
        android:id="@+id/message"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="@dimen/activity_vertical_margin"
        android:layout_marginLeft="@dimen/activity_horizontal_margin"
        android:layout_marginRight="@dimen/activity_horizontal_margin"
        android:layout_marginTop="@dimen/activity_vertical_margin"
        />

</FrameLayout>

<android.support.design.widget.BottomNavigationView
    android:id="@+id/navigation"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom"
    android:background="?android:attr/windowBackground"
    android:layout_alignParentBottom="true"
    app:menu="@menu/navigation" />

这是我的第一个片段 fragment_tab1

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context="com.hackerinside.jaisonjoseph.polysocial.tab1">


<FrameLayout
    android:id="@+id/frame1"
    android:layout_width="match_parent"
    android:layout_height="3dp"
    android:background="@android:color/transparent">


    <ProgressBar
        android:id="@+id/progressBar1"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="fill_parent"
        android:layout_height="3dp"
        android:background="@android:color/transparent"
       android:foregroundGravity="top"
        android:progressDrawable="@drawable/custom_progress"
        android:progress="20"/>

</FrameLayout>


<WebView
    android:id="@+id/webview"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

</WebView>

这是 tab1

java 代码
package com.hackerinside.jaisonjoseph.polysocial;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.widget.FrameLayout;
import android.widget.ProgressBar;


  /**
  * A simple {@link Fragment} subclass.


  */

public class tab1 extends Fragment {



 public ProgressBar bar;
 public FrameLayout frameLayout;
public tab1() {
    // Required empty public constructor
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {


    View rootView = inflater.inflate(R.layout.fragment_tab1, null);



    frameLayout=(FrameLayout) rootView.findViewById(R.id.frame1);
    bar=(ProgressBar)rootView.findViewById(R.id.progressBar1);
    bar.setMax(100);

    final WebView view=(WebView) rootView.findViewById(R.id.webview);
    view.loadUrl("http://facebook.com");
    view.getSettings().setJavaScriptEnabled(true);
    view.setWebViewClient(new MyWebViewClient());
    view.setWebChromeClient(new WebChromeClient(){


        public void onProgressChanged(WebView view1,int progress){

            frameLayout.setVisibility(View.VISIBLE);
            bar.setProgress(progress);
            if (progress==100){

 frameLayout.setVisibility(View.GONE);

            }

 super.onProgressChanged(view1,progress);
        }
    });

    view.getSettings().setBuiltInZoomControls(true);
    view.getSettings().setDisplayZoomControls(false);
    bar.setProgress(0);

    return rootView;
}

}

1 个答案:

答案 0 :(得分:0)

以下是实施的示例构思。

 boolean isShowing;
//oncraete code is here
WebView mScrollView = (WebView) findViewById(R.id.scrollView);
  mScrollView.getViewTreeObserver().addOnScrollChangedListener(new ViewTreeObserver.OnScrollChangedListener() {
        @Override
        public void onScrollChanged() {
            float mfloat = mScrollView.getScrollY();
            if (mfloat >= tToolbar.getHeight() && isShowing) {
                toolbarAnimateHide();
            } else if (mfloat == 0 && !isShowing) {
                toolbarAnimateShow(0);
            }
        }
    });

    private void toolbarAnimateShow(final int verticalOffset) {
    isShowing = true;
    //tToolbar is your view(any view)

    // add getSupportActionBar().hide() this code here to hide actionbar
    // remove below code  or comment
    tToolbar.animate()
            .translationY(0)
            .setInterpolator(new LinearInterpolator())
            .setDuration(180)
            .setListener(new AnimatorListenerAdapter() {
                @Override
                public void onAnimationStart(Animator animation) {
                    toolbarSetElevation(verticalOffset == 0 ? 0 : TOOLBAR_ELEVATION);
                }
            });

}

private void toolbarAnimateHide() {
    isShowing = false;

    // add getSupportActionBar().show() this code here to showactionbar
    // remove below code or comment

    //tToolbar is your view(any view)
    tToolbar.animate()
            .translationY(-tToolbar.getHeight())
            .setInterpolator(new LinearInterpolator())
            .setDuration(180)
            .setListener(new AnimatorListenerAdapter() {
                @Override
                public void onAnimationEnd(Animator animation) {
                    toolbarSetElevation(0);
                }
            });

}