2013-12-18 3 views
5

क्रोम में, जब उपयोगकर्ता सामग्री को ऊपर/नीचे स्वाइप करता है तो पता बार छुपा/दिखाया जाएगा।मैं अपने एंड्रॉइड ऐप के लिए "ऑटो-छुपा नेविगेशन" जैसे क्रोम को कैसे कार्यान्वित कर सकता हूं?

क्या मैं अपने ऐप के समान तर्क लागू कर सकता हूं?

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

मुझे आश्चर्य है कि अगर वहाँ कुछ भी मैं निम्नलिखित कोड से कर सकते हैं, ताकि ActionBar छिपा दिया जाएगा/दिखाया जाता है जब उपयोगकर्ता WebView ऊपर/नीचे स्वाइप करें।

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

उत्तर

2

सबसे पहले आप alignParentBottom और match_parent WebView और शीर्ष पर कुछ पट्टी के साथ लेआउट की जरूरत है:

1) घोषित और कुछ आंतरिक मूल्यों और आकार सेट:

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

तो फिर तुम कोड की जरूरत इसे देखने के लिए हमारी बार:

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 और परिवर्तन वाई मूल्य बनाएं की सीमा के साथ नए ऊंचाई जाँच

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

छोटी "मोबाइल" साइट के लिए काम है, लेकिन बड़ी साइटों पर भी अंतराल है, मैं अभी भी सबसे अच्छे समाधान के तहत काम कर रहा हूं। –

-2

, निम्नलिखित कोड की कोशिश आशा है कि यह आप के लिए काम करता है:

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' जब उपयोगकर्ता की उंगली स्पर्श के लिए संदर्भित करता है प्रदर्शन पर नीचे। इस प्रकार, यह कोड जल्दी से दिखाएगा और फिर प्रत्येक स्पर्श के लिए एक्शन बार छुपाएगा। –

5

यह पैटर्न त्वरित वापसी कहा जाता है। इस blog post से शुरू करें।

हालांकि, इसके उपयोग पर विचार करते समय this पढ़ने योग्य है।

0

डिज़ाइन सपोर्ट लाइब्रेरी का उपयोग करके जावा कोड लिखने के बिना इसे पूरा करने का वास्तव में एक साफ तरीका है। नीचे धीर प्रताप की जवाब देखें: (। मैं एक टिप्पणी के रूप में इस रखा है, लेकिन मैं काफी प्रतिनिधि की जरूरत नहीं है)

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

आपके पास सही वृत्ति है: एक टिप्पणी किसी अन्य प्रश्न पर सहायक उत्तर को इंगित करने का एक उचित तरीका होगा। कृपया [डुप्लिकेट उत्तरों] (// meta.stackexchange.com/a/211726/206345) के लिंक पोस्ट न करें। इसके बजाय, उन अन्य कार्रवाइयों पर विचार करें जो भविष्य में उपयोगकर्ताओं को उनके द्वारा आवश्यक उत्तर ढूंढने में सहायता कर सकते हैं, जैसा कि लिंक किए गए पोस्ट में वर्णित है। – Mogsdad

संबंधित मुद्दे

 संबंधित मुद्दे