Explorar o código

[소개][New] 웹뷰 nested scroll (임시)

hyodong.min %!s(int64=7) %!d(string=hai) anos
pai
achega
d3276efd67

+ 158 - 0
app/src/main/java/kr/co/zumo/app/lifeplus/view/custom/NestedWebView.java

@@ -0,0 +1,158 @@
+/*
+ * COPYRIGHT (c) 2018 All rights reserved by HANWHA LIFE.
+ */
+
+/*
+ * Copyright (C) 2015 takahirom
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package kr.co.zumo.app.lifeplus.view.custom;
+
+import android.content.Context;
+import android.support.v4.view.MotionEventCompat;
+import android.util.AttributeSet;
+import android.view.MotionEvent;
+import android.webkit.WebView;
+
+import android.support.v4.view.NestedScrollingChild;
+import android.support.v4.view.NestedScrollingChildHelper;
+import android.support.v4.view.ViewCompat;
+
+/**
+ * TouchyWebView
+ * <pre>
+ * </pre>
+ *
+ * @author 민효동
+ * @version 1.0
+ * @history 민효동   [2018. 12. 16.]   [최초 작성]
+ * @since 2018. 12. 16.
+ */
+public class NestedWebView extends WebView implements NestedScrollingChild {
+  private int mLastY;
+  private final int[] mScrollOffset = new int[2];
+  private final int[] mScrollConsumed = new int[2];
+  private int mNestedOffsetY;
+  private NestedScrollingChildHelper mChildHelper;
+
+  public NestedWebView(Context context) {
+    this(context, null);
+  }
+
+  public NestedWebView(Context context, AttributeSet attrs) {
+    this(context, attrs, android.R.attr.webViewStyle);
+  }
+
+  public NestedWebView(Context context, AttributeSet attrs, int defStyleAttr) {
+    super(context, attrs, defStyleAttr);
+    mChildHelper = new NestedScrollingChildHelper(this);
+    setNestedScrollingEnabled(true);
+  }
+
+  @Override
+  public boolean onTouchEvent(MotionEvent ev) {
+    boolean returnValue = false;
+
+    MotionEvent event = MotionEvent.obtain(ev);
+    final int action = MotionEventCompat.getActionMasked(event);
+    if (action == MotionEvent.ACTION_DOWN) {
+      mNestedOffsetY = 0;
+    }
+    int eventY = (int) event.getY();
+    event.offsetLocation(0, mNestedOffsetY);
+    switch (action) {
+      case MotionEvent.ACTION_MOVE:
+        int deltaY = mLastY - eventY;
+        // NestedPreScroll
+        if (dispatchNestedPreScroll(0, deltaY, mScrollConsumed, mScrollOffset)) {
+          deltaY -= mScrollConsumed[1];
+          mLastY = eventY - mScrollOffset[1];
+          event.offsetLocation(0, -mScrollOffset[1]);
+          mNestedOffsetY += mScrollOffset[1];
+        }
+        returnValue = super.onTouchEvent(event);
+
+        // NestedScroll
+        if (dispatchNestedScroll(0, mScrollOffset[1], 0, deltaY, mScrollOffset)) {
+          event.offsetLocation(0, mScrollOffset[1]);
+          mNestedOffsetY += mScrollOffset[1];
+          mLastY -= mScrollOffset[1];
+        }
+        break;
+      case MotionEvent.ACTION_DOWN:
+        returnValue = super.onTouchEvent(event);
+        mLastY = eventY;
+        // start NestedScroll
+        startNestedScroll(ViewCompat.SCROLL_AXIS_VERTICAL);
+        break;
+      case MotionEvent.ACTION_UP:
+      case MotionEvent.ACTION_CANCEL:
+        returnValue = super.onTouchEvent(event);
+        // end NestedScroll
+        stopNestedScroll();
+        break;
+      default:
+        break;
+    }
+    return returnValue;
+  }
+
+  // Nested Scroll implements
+  @Override
+  public void setNestedScrollingEnabled(boolean enabled) {
+    mChildHelper.setNestedScrollingEnabled(enabled);
+  }
+
+  @Override
+  public boolean isNestedScrollingEnabled() {
+    return mChildHelper.isNestedScrollingEnabled();
+  }
+
+  @Override
+  public boolean startNestedScroll(int axes) {
+    return mChildHelper.startNestedScroll(axes);
+  }
+
+  @Override
+  public void stopNestedScroll() {
+    mChildHelper.stopNestedScroll();
+  }
+
+  @Override
+  public boolean hasNestedScrollingParent() {
+    return mChildHelper.hasNestedScrollingParent();
+  }
+
+  @Override
+  public boolean dispatchNestedScroll(int dxConsumed, int dyConsumed, int dxUnconsumed, int dyUnconsumed,
+                                      int[] offsetInWindow) {
+    return mChildHelper.dispatchNestedScroll(dxConsumed, dyConsumed, dxUnconsumed, dyUnconsumed, offsetInWindow);
+  }
+
+  @Override
+  public boolean dispatchNestedPreScroll(int dx, int dy, int[] consumed, int[] offsetInWindow) {
+    return mChildHelper.dispatchNestedPreScroll(dx, dy, consumed, offsetInWindow);
+  }
+
+  @Override
+  public boolean dispatchNestedFling(float velocityX, float velocityY, boolean consumed) {
+    return mChildHelper.dispatchNestedFling(velocityX, velocityY, consumed);
+  }
+
+  @Override
+  public boolean dispatchNestedPreFling(float velocityX, float velocityY) {
+    return mChildHelper.dispatchNestedPreFling(velocityX, velocityY);
+  }
+
+}

+ 3 - 3
app/src/main/res/layout/life_plus_brand_layout.xml

@@ -6,12 +6,12 @@
   android:layout_width="match_parent"
   android:layout_height="match_parent">
 
-  <WebView
+  <kr.co.zumo.app.lifeplus.view.custom.NestedWebView
     android:id="@+id/web_view"
     android:layout_width="match_parent"
-    android:layout_height="match_parent"
+    android:layout_height="wrap_content"
     app:layout_constraintEnd_toEndOf="parent"
     app:layout_constraintStart_toStartOf="parent">
 
-  </WebView>
+  </kr.co.zumo.app.lifeplus.view.custom.NestedWebView>
 </android.support.constraint.ConstraintLayout>

+ 1 - 2
build.gradle

@@ -1,7 +1,7 @@
 // Top-level build file where you can add configuration options common to all sub-projects/modules.
 
 buildscript {
-    
+
     repositories {
         google()
         jcenter()
@@ -9,7 +9,6 @@ buildscript {
     }
     dependencies {
         classpath 'com.android.tools.build:gradle:3.2.1'
-        
 
         // NOTE: Do not place your application dependencies here; they belong
         // in the individual module build.gradle files