【问题标题】:How can I implement a chrome like "auto-hide navigation" for my Android app?如何为我的 Android 应用实现类似“自动隐藏导航”的 chrome?
【发布时间】:2013-12-18 03:18:37
【问题描述】:

在 Chrome 中,当用户向上/向下滑动内容时,地址栏将被隐藏/显示。

我可以在我的应用中实现类似的逻辑吗?

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:tools="http://schemas.android.com/tools"
     android:id="@+id/container"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     tools:context=".MainActivity"
     tools:ignore="MergeRootFrame">
         <WebView
         android:id="@+id/my_webview"
         android:layout_width="match_parent"
         android:layout_height="match_parent" />
</FrameLayout>

我想知道我是否可以通过以下代码执行任何操作,以便在用户向上/向下滑动 webView 时隐藏/显示 ActionBar。

WebView webView = (WebView) findViewById(R.id.my_webview);
webView.setOnTouchListener(new View.OnTouchListener() {
     public boolean onTouch (View v, MotionEvent event) {
            webView.onTouchEvent(event);
     }
});

【问题讨论】:

    标签: android android-webview android-gesture


    【解决方案1】:

    这种模式称为快速返回。 从这个blog post开始。

    不过,在考虑使用 this 时,值得一读。

    【讨论】:

      【解决方案2】:

      首先,您需要带有 alignParentBottomma​​tch_parent WebView 的布局以及顶部的一些栏:

      <?xml version="1.0" encoding="utf-8"?>
      <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:background="#ffff" >
      
      <WebView
          android:id="@+id/webView"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:layout_alignParentBottom="true" />
      
      <RelativeLayout
          android:id="@+id/actionBar"
          android:layout_width="match_parent"
          android:layout_height="50dp" >
      
          <TextView
              android:id="@+id/tvTitle"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_centerInParent="true"
              android:text="TextView" />
      
      </RelativeLayout>
      
      </RelativeLayout>
      

      那么你需要代码:

      1) 声明并设置一些内部值并resize WebView 使其低于我们的栏:

      private int baseHeight;
      private int webViewOnTouchHeight;
      private int barHeight;
      private float heightChange;
      private float startEventY;
      
      barHeight = actionBar.getMeasuredHeight();
      baseHeight = getView().getMeasuredHeight();
      webView.getLayoutParams().height = webView.getMeasuredHeight() - barHeight;
      webView.requestLayout();
      webView.setOnTouchListener(listener);
      

      2) 创建调整大小的方法。在这里,我们通过屏幕顶部和条形高度的限制检查新高度

      private boolean resizeView(float delta) {
          heightChange = delta;
          int newHeight = (int)(webViewOnTouchHeight - delta);
          if (newHeight > baseHeight){ // scroll over top
              if (webView.getLayoutParams().height < baseHeight){
                  webView.getLayoutParams().height = baseHeight;
                  webView.requestLayout();
                  return true;
              }       
          }else if (newHeight < baseHeight - barHeight){ // scroll below bar
              if (webView.getLayoutParams().height > baseHeight - barHeight){
                  webView.getLayoutParams().height = baseHeight - barHeight;
                  webView.requestLayout();
                  return true;
              }
          } else { // scroll between top and bar
              webView.getLayoutParams().height = (int)(webViewOnTouchHeight - delta);
              webView.requestLayout();
              return true;
          }
          return false;
      }
      

      3) 创建自定义 onTouch 侦听器,我们将在其中 调整大小 WebView 并更改条形的 Y 值

      private OnTouchListener listener = new OnTouchListener() {
          @Override
          public boolean onTouch(View v, MotionEvent event) {
              switch (event.getAction() & MotionEvent.ACTION_MASK) {
              case MotionEvent.ACTION_DOWN:
                  startEventY = event.getY();
                  heightChange = 0;
                  webViewOnTouchHeight = webView.getLayoutParams().height;
                  break;
              case MotionEvent.ACTION_MOVE:
                  float delta = (event.getY()+heightChange)-startEventY;
                  boolean heigthChanged = resizeView(delta);
                  if (heigthChanged){
                      actionBar.setTranslationY(baseHeight - webView.getLayoutParams().height - barHeight);
                  }
              }
              return false;
          }
      };
      

      【讨论】:

      • 适用于小型“移动”网站,但在大型网站上过于滞后,我仍在使用最佳解决方案。
      【解决方案3】:

      现在有一种非常简洁的方法来完成此任务,而无需使用设计支持库编写任何 Java 代码。请参阅以下 Dhir Pratap 的回答:

      (我会将此作为评论,但我没有足够的代表。)

      How to Hide ActionBar/Toolbar While Scrolling Down in Webview

      <android.support.design.widget.CoordinatorLayout 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.AppBarLayout
              android:id="@+id/appbar"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
      
              <android.support.v7.widget.Toolbar
                  android:id="@+id/toolbar"
                  android:layout_width="match_parent"
                  android:layout_height="?attr/actionBarSize"
                  android:background="?attr/colorPrimary"
                  app:layout_scrollFlags="scroll|enterAlways"
                  app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
          </android.support.design.widget.AppBarLayout>
      
          <android.support.v4.widget.NestedScrollView 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:layout_gravity="fill_vertical"
              app:layout_behavior="@string/appbar_scrolling_view_behavior">
      
              <WebView
                  android:id="@+id/webview"
                  android:layout_width="match_parent"
                  android:layout_height="match_parent"/>
          </android.support.v4.widget.NestedScrollView>
      </android.support.design.widget.CoordinatorLayout>
      

      【讨论】:

      • 你有正确的直觉:评论是指出另一个问题的有用答案的一种适当方式。请不要发布指向duplicate answers 的链接。相反,请考虑其他可以帮助未来用户找到所需答案的操作,如链接帖子中所述。
      【解决方案4】:

      试试下面的代码,希望对你有用:

          private void init(){
          WebView web = new WebView(this);
          final Context context = this;
          web.setOnTouchListener(new OnTouchListener() {
      
              public boolean onTouch(View arg0, MotionEvent event) {
                  // TODO Auto-generated method stub
                  switch (event.getAction()) {
                  case MotionEvent.ACTION_DOWN:
                      // show action bar
                      ((Activity) context).getActionBar().show();
                      break;
      
                  case MotionEvent.ACTION_UP:
                      // hide action bar
                      ((Activity) context).getActionBar().hide();
                      break;
      
                  default:
                      break;
                  }
                  return false;
              }
          });
      }
      

      【讨论】:

      • ACTION_DOWN 是指用户的手指在显示屏上向下触摸的时间。因此,此代码将快速显示然后隐藏每次触摸的操作栏。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多