Browse Source

Don't include RecyclerView dependency, mark it as provided instead

Marking it provided allows you to work with it in your namespace as usual, but not actually worry about including it as an unnecessary transitive dependency for those who use the library but don't want RecyclerView.
Henri Sweers 10 years ago
parent
commit
4e38f449a1

+ 1 - 1
library/build.gradle

@@ -20,7 +20,7 @@ dependencies {
     compile fileTree(dir: 'libs', include: ['*.jar'])
     compile fileTree(dir: 'libs', include: ['*.jar'])
     compile 'com.android.support:appcompat-v7:21.0.3'
     compile 'com.android.support:appcompat-v7:21.0.3'
     compile 'com.android.support:support-annotations:21.0.3'
     compile 'com.android.support:support-annotations:21.0.3'
-    compile 'com.android.support:recyclerview-v7:21.0.3'
+    provided 'com.android.support:recyclerview-v7:21.0.3'
 }
 }
 
 
 publish {
 publish {

+ 13 - 24
library/src/main/java/com/afollestad/materialdialogs/MaterialDialog.java

@@ -535,8 +535,8 @@ public class MaterialDialog extends DialogBase implements View.OnClickListener {
             return canAdapterViewScroll((AdapterView) view);
             return canAdapterViewScroll((AdapterView) view);
         } else if (view instanceof WebView) {
         } else if (view instanceof WebView) {
             return canWebViewScroll((WebView) view);
             return canWebViewScroll((WebView) view);
-        } else if (view instanceof RecyclerView) {
-            return canRecyclerViewScroll((RecyclerView) view);
+        } else if (isRecyclerView(view)) {
+            return RecyclerUtil.canRecyclerViewScroll((RecyclerView) view);
         } else {
         } else {
             if (atBottom) {
             if (atBottom) {
                 return canViewOrChildScroll(getBottomView((ViewGroup) view), true);
                 return canViewOrChildScroll(getBottomView((ViewGroup) view), true);
@@ -546,6 +546,17 @@ public class MaterialDialog extends DialogBase implements View.OnClickListener {
         }
         }
     }
     }
 
 
+    private static boolean isRecyclerView(View view) {
+        boolean isRecyclerView = false;
+        try {
+            Class.forName("android.support.v7.widget.RecyclerView");
+
+            // We got here, so now we can safely check
+            isRecyclerView = view instanceof RecyclerView;
+        } catch (ClassNotFoundException ignored) {}
+
+        return isRecyclerView;
+    }
 
 
     private static boolean canWebViewScroll(WebView view) {
     private static boolean canWebViewScroll(WebView view) {
         return view.getMeasuredHeight() > view.getContentHeight();
         return view.getMeasuredHeight() > view.getContentHeight();
@@ -571,28 +582,6 @@ public class MaterialDialog extends DialogBase implements View.OnClickListener {
         }
         }
     }
     }
 
 
-    private static boolean canRecyclerViewScroll(RecyclerView rv) {
-        final RecyclerView.LayoutManager lm = rv.getLayoutManager();
-        final int count = rv.getAdapter().getItemCount();
-        int lastVisible;
-
-        if (lm instanceof LinearLayoutManager) {
-            LinearLayoutManager llm = (LinearLayoutManager) lm;
-            lastVisible = llm.findLastVisibleItemPosition();
-        } else if (lm instanceof GridLayoutManager) {
-            GridLayoutManager glm = (GridLayoutManager) lm;
-            lastVisible = glm.findLastVisibleItemPosition();
-        } else {
-            throw new NotImplementedException("Material Dialogs currently only supports LinearLayoutManager and GridLayoutManager. Please report any new layout managers.");
-        }
-
-        if (lastVisible == -1)
-            return false;
-        /* We scroll if the last item is not visible */
-        final boolean lastItemVisible = lastVisible == count - 1;
-        return !lastItemVisible || rv.getChildAt(rv.getChildCount() - 1).getBottom() > rv.getHeight() - rv.getPaddingBottom();
-    }
-
     /**
     /**
      * Detects whether or not the content TextView can be scrolled.
      * Detects whether or not the content TextView can be scrolled.
      */
      */

+ 29 - 0
library/src/main/java/com/afollestad/materialdialogs/RecyclerUtil.java

@@ -0,0 +1,29 @@
+package com.afollestad.materialdialogs;
+
+import android.support.v7.widget.GridLayoutManager;
+import android.support.v7.widget.LinearLayoutManager;
+import android.support.v7.widget.RecyclerView;
+
+public class RecyclerUtil {
+    static boolean canRecyclerViewScroll(RecyclerView rv) {
+        final RecyclerView.LayoutManager lm = rv.getLayoutManager();
+        final int count = rv.getAdapter().getItemCount();
+        int lastVisible;
+
+        if (lm instanceof LinearLayoutManager) {
+            LinearLayoutManager llm = (LinearLayoutManager) lm;
+            lastVisible = llm.findLastVisibleItemPosition();
+        } else if (lm instanceof GridLayoutManager) {
+            GridLayoutManager glm = (GridLayoutManager) lm;
+            lastVisible = glm.findLastVisibleItemPosition();
+        } else {
+            throw new MaterialDialog.NotImplementedException("Material Dialogs currently only supports LinearLayoutManager and GridLayoutManager. Please report any new layout managers.");
+        }
+
+        if (lastVisible == -1)
+            return false;
+        /* We scroll if the last item is not visible */
+        final boolean lastItemVisible = lastVisible == count - 1;
+        return !lastItemVisible || rv.getChildAt(rv.getChildCount() - 1).getBottom() > rv.getHeight() - rv.getPaddingBottom();
+    }
+}