2014-10-03 39 views
1

我收到此錯誤:調用API需要14級(當前分鐘8):android.view.ViewGroup#canScrollHorizo​​ntally

Call requires API level 14 (current min is 8): android.view.ViewGroup#canScrollHorizontally.

我怎麼能14之前在API層面解決呢?

public class ViewPagerEx extends ViewGroup{ 
    @Override 
    public boolean performAccessibilityAction(View host, int action, Bundle args) { 
     if (super.performAccessibilityAction(host, action, args)) { 
      return true; 
     } 
     switch (action) { 
      case AccessibilityNodeInfoCompat.ACTION_SCROLL_FORWARD: { 
       if (canScrollHorizontally(1)) { 
        setCurrentItem(mCurItem + 1); 
        return true; 
       } 
      } return false; 
      case AccessibilityNodeInfoCompat.ACTION_SCROLL_BACKWARD: { 
       if (canScrollHorizontally(-1)) { 
        setCurrentItem(mCurItem - 1); 
        return true; 
       } 
      } return false; 
     } 
     return false; 
    } 

    public boolean canScrollHorizontally(int direction) { 
     if (mAdapter == null) { 
      return false; 
     } 
     final int width = getClientWidth(); 
     final int scrollX = getScrollX(); 
     if (direction < 0) { 
      return (scrollX > (int) (width * mFirstOffset)); 
     } else if (direction > 0) { 
      return (scrollX < (int) (width * mLastOffset)); 
     } else { 
      return false; 
     } 
    } 
} 
+4

使用支持庫:http://developer.android.com/reference/android/support/v4 /view/ViewCompat.html#canScrollHorizo​​ntal(android.view.View,int) – 2014-10-03 08:43:51

+0

它取決於你,如果你想支持更多最新的Android手機,你可以增加你的API級別爲14 ...否則你必須找到另一種替代方案你想要做什麼,這只是一個建議 – danidee 2014-10-03 08:44:41

+1

你不清楚你是否寫了一個名爲canScrollHorizo​​tnally的方法,或者你想使用框架提供的那個 – Blackbelt 2014-10-03 08:45:01

回答

1

AndroidManifest.xml,你會發現這樣的代碼:

`<uses-sdk 
    android:minSdkVersion="14" 
    android:targetSdkVersion="19" />` 

android:minSdkVersion是你找什麼,我是指您的應用將具有相同API級別圖謀工作。

android:targetSdkVersion是您的應用程序設計用於處理的API級別。

android:maxSdkVersion是您的應用程序可以使用的最大API級別。

意味着你的應用程序可以android:minSdkVersionandroid:maxSdkVersion

之間的設備上工作,所以你要做的就是在改變你API級別從8到14,支持android.view.ViewGroup#canScrollHorizontally.因爲它是在14級添加了什麼。

考慮到降低android:minSdkVersion將支持更多的Android設備,但您將無法使用更高API級別中引入的方法。

更多信息check this detailed description

額外的信息:

  1. 奇巧24.5%(API等級19)
  2. 豆形軟糖53.8%(API級別16,17,18)
  3. ICECREAMSANDWICH 9.6% (API等級15)
  4. GINGERBREAD 11.4%(API等級10)
  5. FROYO 0.7%(API等級8)

0.7%???你可能會在設置之前三思而後行android:minSdkVersion="8"

+1

我認爲你是con將targetSdkVersion與maxSdkVersion屬性融合在一起。你可能想再次檢查該鏈接:) – Chilledrat 2014-10-03 16:43:21

+0

這個答案是誤導。首先,如上所述,targetSdkVersion不是您應用可運行的最大API版本。其次,降低minSDK確實不會讓你失去功能。你可以使用它們來代碼_。但是,您需要做的是注意不要在沒有它們的設備上調用所述API。但是,說「你會失去在更高API級別添加的高級功能」,這是錯誤的。那是錯的。 – davidcesarino 2014-10-03 17:06:35

+0

@Chilledrat,我怎麼沒注意到,我會編輯我的答案。謝謝:) – 2014-10-03 20:36:07

0

試試看: 嘗試改變你的本地方法名稱,然後調用它。 例如,將其更改爲canScrollHorizo​​ntallyLocal(),以便您調用它。 ViewGroup有一個類似的方法,它可能會嘗試超級方法而不是你的方法。

1

從@ KenWolf的建議:使用support-v4庫,你可以使用ViewCompat進行檢查:

// From within the View itself, just invoke the ViewCompat 
// implementation with 'this' as the View parameter. 
if (ViewCompat.canScrollHorizontally(this, 1)) { 
    // ... 
} 
相關問題