2013-12-18 8 views
5

Chrome'da, kullanıcı içeriği yukarı/aşağı kaydırdığında adres çubuğu gizlenir/gösterilir.Android uygulamam için "otomatik gizle gezinme" gibi bir kromu nasıl uygularım?

Uyguladığım benzer mantığı uygulayabilir miyim? Aşağıdaki kodu ile yapabileceğiniz bir şey İşlem Çubuğu gizlenmiş olacağını kullanıcı Web Görünümü yukarı/aşağı kaydırma yaptığınızda/gösterilen nedenle, varsa

<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> 

merak ediyorum.

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

cevap

2

Önce alignParentBottom ve match_parent WebView'da ve üstündeki bazı bar düzeni gerekir:

1) beyan ve bazı iç değerlerin ve yeniden boyutlandırma ayarlayın:

<?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> 

Sonra kodu gerekir WebView bunu altında yapmak için bizim bar:

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) Yeniden boyutlandırma yöntemi oluşturun. Burada ekranın üstünde ve çubuk yüksekliği

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) biz bizim çubuğunun WebView ve değişim Y değerini boyutlandırmak olacak özel onTouch dinleyici oluşturun sınırları ile yeni yüksekliğini kontrol

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; 
    } 
}; 
+0

Küçük "mobil" site için çalışıyor, ancak büyük sitelerde de gecikme, hala en iyi çözüm altında çalışıyorum. –

-2

, aşağıdaki kodu deneyin sizin için çalışıyor umut:

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; 
     } 
    }); 
} 
+0

'ACTION_DOWN' kullanıcının parmağı dokunuşlar zaman anlamına gelir Ekranda aşağı. Bu nedenle, bu kod hızlı bir şekilde gösterilecek ve her dokunuş için hareket çubuğunu gizleyecektir. –

5

Bu desen Hızlı Dönüş denir. Bu blog post ile başlayın. Bununla birlikte, kullanım göz önüne alındığında, this değerini okumak faydalı olacaktır.

0

Tasarım Desteği Kitaplığı'nı kullanarak herhangi bir Java kodu yazmadan bunu gerçekleştirmenin gerçekten temiz bir yolu var. Aşağıda Dhir Pratap cevabını bakınız: (. Ben bir yorum olarak bu yer olurdu ama yeterince temsilcisi yok)

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> 
+0

Doğru içgüdüye sahipsiniz: Bir yorum, başka bir soruya yararlı bir cevaba işaret etmenin uygun bir yolu olurdu. Lütfen [yinelenen cevaplar] 'a bağlantı göndermeyin (// meta.stackexchange.com/a/211726/206345). Bunun yerine, gelecekteki kullanıcıların bağlı postada açıklandığı gibi ihtiyaç duydukları cevabı bulmasına yardımcı olabilecek diğer eylemleri göz önünde bulundurun. – Mogsdad

İlgili konular