2016-05-23 180 views
0

我有一個應用程序屏幕,我有一個web視圖,並在屏幕底部的按鈕線性佈局。我有一個滾動監聽器附加到webview,這是給我滾動時的值。我真正想做的事情是,在向下滾動時逐漸隱藏底部線性佈局,並在滾動時顯示,如在一些公平的應用程序中看到的,但我可以看到的所有示例都是屏幕頂部的工具欄。隱藏webview滾動的linearlayout

我已經嘗試了一些東西,我試圖獲得線性佈局的高度,然後在滾動,將其移動到Y軸隱藏和顯示,並且這移動了按鈕但不是實際的酒吧。

我試過的最主要的事情是滾動時,降低了酒吧的高度,當它達到0隱藏它,然後再次顯示它,但這是擠壓按鈕,而不是滾動它們離開屏幕底部而不改變它們的大小。

這是佈局:

<LinearLayout 
    android:orientation="horizontal" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignParentBottom="true" 
    android:id="@+id/buttonBar" 
    android:background="#403152" 
    android:visibility="gone" 
    android:layout_marginTop="5dp" 
    android:layout_marginBottom="5dp"> 

    <ImageButton 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:id="@+id/facebookLink" 
     android:src="@string/button1" 

     android:layout_weight="1" 
     android:onClick="openLink" 
     android:background="@android:color/transparent"/> 

    <ImageButton 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:id="@+id/linkedinLink" 
     android:src="@string/button2" 

     android:layout_weight="1" 
     android:background="?android:selectableItemBackground" 
     android:onClick="openLink"/> 

    <ImageButton 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:id="@+id/twitterLink" 
     android:src="@string/button3" 

     android:layout_weight="1" 
     android:background="?android:selectableItemBackground" 
     android:onClick="openLink"/> 

    <ImageButton 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:id="@+id/instagramLink" 
     android:src="@string/button4" 

     android:layout_weight="1" 
     android:background="?android:selectableItemBackground" 
     android:onClick="openLink"/> 

    <ImageButton 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:src="@mipmap/ic_overflow" 

     android:layout_weight="1" 
     android:layout_gravity="center" 
     android:background="?android:selectableItemBackground" 
     android:onClick="showPopup" 
     android:id="@+id/aboutoption"/> 
</LinearLayout> 


<holidays.ObservableWebView 
    android:id="@+id/scrollableWebview" 
    android:layout_width="fill_parent" 
    android:clickable="false" 
    android:layout_height="fill_parent" 
    android:layout_alignParentTop="true" 
    android:layout_above="@id/buttonBar" 
    android:visibility="gone"/> 

,然後這是迄今爲止我已經嘗試的代碼,其中包括改變高度等 - 希望有人也許能夠指出我在正確的方向是什麼我做錯了,或者可能是更好的方法來解決這個問題。

mWebView.setOnScrollChangedCallback(new ObservableWebView.OnScrollChangedCallback(){ 
       public void onScroll(int l, int t){ 

        int height = mWebView.getMeasuredHeight(); 
        Log.d("HEIGHT", Integer.toString(height)); 


        Display display = getWindowManager().getDefaultDisplay(); 
        Point size = new Point(); 
        display.getSize(size); 
        int screenHeight = size.y; 

        Log.d("Screen HEIGHT", Integer.toString(screenHeight)); 


        LinearLayout layout = (LinearLayout)findViewById(R.id.buttonBar); 
        View contentsView = findViewById(R.id.buttonBar); 

        if(firstScroll == true){ 

         mWebView.setLayoutParams(new RelativeLayout.LayoutParams(mWebView.getMeasuredWidth(), mWebView.getMeasuredHeight() + 10)); 

         /*originalButtonBarHeight = findViewById(R.id.buttonBar).getHeight(); 
         currentButtonBarHeight = originalButtonBarHeight; 

         contentsView.getLocationOnScreen(screenBarPosition); 
         contentsView.setY(screenBarPosition[1] + 1); 

         firstScroll = false; 
         layout.getLayoutParams().height = t + 1; 
         layout.requestLayout(); 

         directionVal = t;*/ 
        } 
        else if(firstScroll == false){ 
         if(directionVal < t){ 
          mWebView.setLayoutParams(new RelativeLayout.LayoutParams(mWebView.getMeasuredWidth(), mWebView.getMeasuredHeight() + 10)); 
          /* Log.d("DOWN", Integer.toString(t)); 
          if(currentButtonBarHeight <= 0){ 
           //do nothing 
          } 
          else{ 
           currentButtonBarHeight = currentButtonBarHeight - 5; 

           if(currentButtonBarHeight <= 0){ 
            currentButtonBarHeight = 0; 
            layout.getLayoutParams().height = 0; 
            layout.requestLayout(); 

            findViewById(R.id.buttonBar).setVisibility(View.GONE); 
           } 
           else{ 


            contentsView.getLocationOnScreen(screenBarPosition); 
            contentsView.setY(screenBarPosition[1] + 1); 

            layout.getLayoutParams().height = currentButtonBarHeight; 
            layout.requestLayout(); 
           } 

          } 
          /*if(t + 5 < originalButtonBarHeight) { 

          }*/ 
         } 
         else if(directionVal > t){ 
          Log.d("UP", Integer.toString(currentButtonBarHeight)); 


          /*decreasedAmount = t - 5; 
          if (decreasedAmount < originalButtonBarHeight){ 
           layout.getLayoutParams().height = t - 5; 
           layout.requestLayout(); 
          }*/ 

         } 

         directionVal = t; 
        } 

       } 
      }); 

回答

0

你可以這樣做一個由CustomWebView

CustomWebView.java

public class CustomWebView extends WebView { 
    private GestureDetector gestureDetector; 
    public CustomWebView(Context context) { 
     super(context); 
    } 
    public CustomWebView(Context context, AttributeSet attrs) { 
     super(context, attrs); 
    } 
    public CustomWebView(Context context, AttributeSet attrs, int defStyle) { 
     super(context, attrs, defStyle); 
    } 
    @Override 
    protected void onScrollChanged(int l, int t, int oldl, int oldt) { 
     super.onScrollChanged(l, t, oldl, oldt); 
    } 

    @Override 
    public boolean onTouchEvent(MotionEvent ev) { 
     return gestureDetector.onTouchEvent(ev) || super.onTouchEvent(ev); 
    } 

    public void setGestureDetector(GestureDetector gestureDetector) { 
     this.gestureDetector = gestureDetector; 
    } 
} 

web_fragment.xml

<?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" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="@android:color/transparent" 
    android:orientation="vertical">  

    <com.customview.CustomWebView 
      android:id="@+id/customWebView" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:focusable="true" /> 

    // Your other Linear layout 

</LinearLayout> 

CustomeGestureDetector CLSS的手勢檢測(我的片段添加):

private class CustomeGestureDetector extends GestureDetector.SimpleOnGestureListener { 
     @Override 
     public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { 
      if(e1 == null || e2 == null) return false; 
      if(e1.getPointerCount() > 1 || e2.getPointerCount() > 1) return false; 
      else { 
       try { 
        if(e1.getY() - e2.getY() > 20) { 
          // Show Your Linear Layout 

         customWebView.invalidate(); 
         return false; 
        } 
        else if (e2.getY() - e1.getY() > 20) { 
          // Hide Your Linear Layout 

         customWebView.invalidate(); 
         return false; 
        } 

       } catch (Exception e) { 
        customWebView.invalidate(); 
       } 
       return false; 
      } 


     } 
    } 

WebFragment.java

private CustomWebView customWebView; 

customWebView= (CustomWebView) view.findViewById(R.id.customWebView); 

customWebView.setGestureDetector(new GestureDetector(new CustomeGestureDetector())); 

希望它會幫助你。

+0

這只是顯示/隱藏,而不是逐漸顯示/隱藏? – Navvy