2016-07-06 94 views
12

編輯/注:我真的需要一個答案,我想留在「股票」谷歌Android API。我已經創建了一個+100的賞金,但如果我在未來幾天使用股票API獲得一個簡單的解決方案,我會再添加+100,使其值得200點。箭在哪裏?

我與Android CalendarView控制試驗。我使用NestedScrollView中的按鈕和CalendarView創建了一個小應用程序。我做了按鈕和它的邊緣真的很大,所以我可以驗證滾動工作。在運行Android 6.01的三星Galaxy S5上,它工作正常。但在三星小號二重奏(這是我預期的目標)運行4.2.2有沒有辦法提前一個月(通知沒有旁邊的箭頭月

下面是三星S5運行Android 6.01

截圖

s5 screenshot

,這裏是一個運行4.2.2

S Duo screenshot

的content_main.xm三星小號二重奏我看起來像這樣。 。

<?xml version="1.0" encoding="utf-8"?> 
<android.support.v4.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"> 

    <!-- This linear layout is because the scrollview can have only 1 direct child --> 
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
        android:layout_width="match_parent" 
        android:layout_height="match_parent" 
        android:orientation="vertical"> 

     <Button 
      android:id="@+id/recordEnd" 
      android:layout_width="200dp" 
      android:layout_height="100dp" 
      android:layout_marginTop="100dp" 
      android:layout_marginBottom="100dp" 
      android:gravity="center" 
      android:text="Record End"/> 

     <CalendarView 
      android:id="@+id/thecalendar" 
      android:layout_width="240dp" 
      android:layout_height="300dp" 
      android:minDate="01/01/2016" 
      android:maxDate="11/30/2016"/> 
    </LinearLayout> 
</android.support.v4.widget.NestedScrollView> 

...在Android Studio中的設計視圖顯示箭頭。我不知道如何調試。

+0

它可能是特定於版本我猜..由於每個版本的變化''CalendarView'的變化..! –

+0

顯然它是特定於版本的,但CalendarView自API 11以來一直不存在,所以在Android 5之前,用戶是如何在不同月份前進的? – user316117

+0

只是交換..我猜.. .. –

回答

8

Iulian Popescu的一個很好很深的解釋,我只是想簡化它。首先我想澄清幾件事情。

  1. 根據this官方Android顯影劑鏈路,CalenderView插件的確切外觀和相互作用模型可以OS版本和主題(例如全息對比材料)之間變化。
  2. 作爲尤利安·波佩斯庫粘貼從android.widget.CalendarView類的Android的代碼,你可以看到,CalendarViewLegacyDelegate類是負責的HOLO主題CalendarViewMaterialDelegate類負責在MATERIAL主題的情況下渲染CalenderView情況下渲染CalenderView。我再次發佈該代碼僅供參考。
public CalendarView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { 
     super(context, attrs, defStyleAttr, defStyleRes); 
     final TypedArray a = context.obtainStyledAttributes(
       attrs, R.styleable.CalendarView, defStyleAttr, defStyleRes); 
     final int mode = a.getInt(R.styleable.CalendarView_calendarViewMode, MODE_HOLO); 
     a.recycle(); 
     switch (mode) { 
      case MODE_HOLO: 
       mDelegate = new CalendarViewLegacyDelegate(
         this, context, attrs, defStyleAttr, defStyleRes); 
       break; 
      case MODE_MATERIAL: 
       mDelegate = new CalendarViewMaterialDelegate(
         this, context, attrs, defStyleAttr, defStyleRes); 
       break; 
      default: 
       throw new IllegalArgumentException("invalid calendarViewMode attribute"); 
     } 
    } 
  1. CalendarViewMaterialDelegate(android.widget.CalendarViewMaterialDelegate)類我們可以看到,DayPickerView(android.widget.DayPickerView)類被用作一個容器以使砑光機認爲,我們可以看到材質主題。在DayPickerViewViewPager用於在一頁中顯示一個月。還有兩個ImageButton用於下一個和上一個。由於有兩個按鈕來改變月份,它不會產生任何問題,並且在MATERIAL主題中工作得很好。
private final ViewPager mViewPager; 
    private final ImageButton mPrevButton; 
    private final ImageButton mNextButton; 

  • 正如我們可以在CalendarViewLegacyDelegate(android.widget.CalendarViewLegacyDelegate)類看到,ListView用於顯示星期的列表(垂直滾動)在CalenderView。因爲它將垂直滾動,所以沒有任何ImageButton用於上一個和下一個。
  • /** 
        * The adapter for the weeks list. 
        */ 
        private WeeksAdapter mAdapter; 
    
        /** 
        * The weeks list. 
        */ 
        private ListView mListView; 
    

    按本link Android開發者網站,我們不應該使用帶有ListView一個ScrollView,因爲ListView需要其自身的垂直滾動的照顧。因爲HOLO主題CalenderViewScrollView內使用時會出現意外情況。

    解決方案: -

    您可以使用下面給出的自定義滾動視圖類,而不是NestedScrollView類。這將使你的CalenderView垂直滾動的情況下HOLO主題。

    public class VerticalScrollView extends ScrollView{ 
    
        public VerticalScrollView(Context context) { 
         super(context); 
        } 
    
        public VerticalScrollView(Context context, AttributeSet attrs) { 
         super(context, attrs); 
        } 
    
        public VerticalScrollView(Context context, AttributeSet attrs, int defStyle) { 
         super(context, attrs, defStyle); 
        } 
    
        @Override 
        public boolean onInterceptTouchEvent(MotionEvent ev) { 
         final int action = ev.getAction(); 
         switch (action) 
         { 
          case MotionEvent.ACTION_DOWN: 
           super.onTouchEvent(ev); 
           break; 
    
          case MotionEvent.ACTION_MOVE: 
           return false; // redirect MotionEvents to ourself 
    
          case MotionEvent.ACTION_CANCEL: 
           super.onTouchEvent(ev); 
           break; 
    
          case MotionEvent.ACTION_UP: 
           return false; 
    
          default: 
           break; 
         } 
    
         return false; 
        } 
    
        @Override 
        public boolean onTouchEvent(MotionEvent ev) { 
         super.onTouchEvent(ev); 
         return true; 
        } 
    } 
    

    我希望這會消除你的疑惑。

    10

    具有CalendarView代碼快看,它看起來像日曆外觀從系統推斷,我無法找到一個方法來改變它。下面你可以找到日曆的觀點是如何選擇取決於mode

    final TypedArray a = context.obtainStyledAttributes(
         attrs, R.styleable.CalendarView, defStyleAttr, defStyleRes); 
    final int mode = a.getInt(R.styleable.CalendarView_calendarViewMode, MODE_HOLO); 
    a.recycle(); 
    
    switch (mode) { 
        case MODE_HOLO: 
         mDelegate = new CalendarViewLegacyDelegate(
           this, context, attrs, defStyleAttr, defStyleRes); 
         break; 
        case MODE_MATERIAL: 
         mDelegate = new CalendarViewMaterialDelegate(
           this, context, attrs, defStyleAttr, defStyleRes); 
         break; 
        default: 
         throw new IllegalArgumentException("invalid calendarViewMode attribute"); 
    } 
    

    編輯

    經過一番花在我有一些更好吃茶的問題更多的時間:

    1. 預覽顯示帶箭頭的視圖,因爲您尚未設置適當的API版本。如果你看看下面的圖片,你會發現在棒棒糖之前的版本中,按鈕丟失了,之後,一切都很好,很好。要更改API版本,只需單擊以紅色突出顯示的按鈕並選擇所需的API級別,即可在不同平臺上精確預覽xml。

      KitKat versionAndroid N version

    2. 爲什麼箭頭是從材料設計的先前版本丟失? 在這裏,我是不是能夠找到從谷歌的人給出的答案,但我認爲,這是主題相關的屏幕分辨率(舊設備上,你沒有那麼多的空間來顯示CalendarView像新型Android版本,這就是爲什麼他們選擇了ListView,因爲它只能顯示幾行仍然功能齊全,而與ViewPager顯示會進行一些裁剪的最後幾行的)。

      正如我在原來的職位說,是基於材料設計(Android 5.0以上版本)的可用性選擇CalendarView的風格。

      The 舊版本(在下面找到)有一個ListView和下一個/上一個按鈕丟失。由於日曆使用ListView顯示,因此您無法滾動到下一個月,因爲CalendarView的父視圖是Scroll。要解決這個問題,你可以找到一些解釋和解決方案here

      <TextView android:id="@+android:id/month_name" 
          android:layout_width="wrap_content" 
          android:layout_height="wrap_content" 
          android:layout_gravity="center_horizontal" 
          android:paddingTop="10dip" 
          android:paddingBottom="10dip" 
          style="@android:style/TextAppearance.Medium" /> 
      
      <!-- This is the header representing the days of the week --> 
      <LinearLayout android:id="@+android:id/day_names" 
          android:orientation="horizontal" 
          android:layout_width="match_parent" 
          android:layout_height="wrap_content" 
          android:layout_marginBottom="6dip" 
          android:layout_marginEnd="2dip" 
          android:layout_marginStart="2dip" 
          android:gravity="center" > 
      
          <TextView android:layout_width="0dip" 
           android:layout_height="wrap_content" 
           android:layout_weight="1" 
           android:gravity="center" 
           android:visibility="gone" /> 
      
          <TextView android:layout_width="0dip" 
           android:layout_height="wrap_content" 
           android:layout_weight="1" 
           android:gravity="center" /> 
      
          <TextView android:layout_width="0dip" 
           android:layout_height="wrap_content" 
           android:layout_weight="1" 
           android:gravity="center" /> 
      
          <TextView android:layout_width="0dip" 
           android:layout_height="wrap_content" 
           android:layout_weight="1" 
           android:gravity="center" /> 
      
          <TextView android:layout_width="0dip" 
           android:layout_height="wrap_content" 
           android:layout_weight="1" 
           android:gravity="center" /> 
      
          <TextView android:layout_width="0dip" 
           android:layout_height="wrap_content" 
           android:layout_weight="1" 
           android:gravity="center" /> 
      
          <TextView android:layout_width="0dip" 
           android:layout_height="wrap_content" 
           android:layout_weight="1" 
           android:gravity="center" /> 
      
          <TextView android:layout_width="0dip" 
           android:layout_height="wrap_content" 
           android:layout_weight="1" 
           android:gravity="center" /> 
      
      </LinearLayout> 
      
      <ImageView android:layout_width="match_parent" 
          android:layout_height="1dip" 
          android:scaleType="fitXY" 
          android:gravity="fill_horizontal" 
          android:src="?android:attr/dividerHorizontal" /> 
      
      <ListView android:id="@android:id/list" 
          android:layout_width="match_parent" 
          android:layout_height="match_parent" 
          android:drawSelectorOnTop="false" 
          android:cacheColorHint="@android:color/transparent" 
          android:fastScrollEnabled="false" 
          android:overScrollMode="never" /> 
      

      材料設計版本替換ListViewViewPager並添加上一個和下一個按鈕。

      <android.widget.DayPickerViewPager 
          android:id="@+id/day_picker_view_pager" 
          android:layout_width="match_parent" 
          android:layout_height="match_parent" /> 
      
      <ImageButton 
          android:id="@+id/prev" 
          android:layout_width="wrap_content" 
          android:layout_height="wrap_content" 
          android:minWidth="48dp" 
          android:minHeight="48dp" 
          android:src="@drawable/ic_chevron_start" 
          android:background="?attr/selectableItemBackgroundBorderless" 
          android:contentDescription="@string/date_picker_prev_month_button" 
          android:visibility="invisible" /> 
      
      <ImageButton 
          android:id="@+id/next" 
          android:layout_width="wrap_content" 
          android:layout_height="wrap_content" 
          android:minWidth="48dp" 
          android:minHeight="48dp" 
          android:src="@drawable/ic_chevron_end" 
          android:background="?attr/selectableItemBackgroundBorderless" 
          android:contentDescription="@string/date_picker_next_month_button" 
          android:visibility="invisible" /> 
      

      此刻,我無法找到一個方法來樣式化CalendarView因爲stylable屬性是從公共API中刪除。

      android.R.styleable類及其字段已從公共API中刪除,以更好地確保應用程序的向前兼容性。

      話雖這麼說,你可以保留舊的外觀風格較舊的Android版本,因爲你不能改變它,但請記住,一個ListView不應該在一個Scroll,或者嘗試找到一個存儲庫是穩定的並且更適合您的需求。

    編輯II:

    CalendarView不破,它完美的作品在所有的Android版本,即使,它可能給人的印象是它的行爲是因爲使用ListView實施的破壞。現在,」讓我們深入一點的問題,使得大家能夠理解發生了什麼:

    1. 問題沒有。 1:使用ListView in ScrollView
      一般來說,使用這些嵌套元素是一種糟糕的做法,因爲它們無法正確處理滾動操作。

    2. 問題編號: 2:使用NestedScrollView不能解決問題
      從Android 5.0谷歌添加支持庫以支持嵌套滾動,但ListView將無法​​處理滾動,除非它實現NestedScrollingChild。爲此,子類ListView類和實現接口方法,並從每個方法調用相應的方法從NestedScrollingChildHelper。按照此步驟,您可以在NestedScrollView中使用ListView,但不能使用CalendarView(請參閱問題3)

    3. 問題編號:3:你不能改變列表CalendarView
      真正的問題是,CalendarView沒有任何方法可以讓你用你寫的自定義列表來改變默認列表。假設你有這樣一種方法,你可以通過用你自定義的默認列表替換默認列表來使工作,並且滾動可以很好地工作,但不幸的是你不能。

    爲了解決這個問題,你有兩個選擇:
    - 重新設計您查看使得CalendarView不包含Scroll在其名稱中
    類 - 使用普拉Divraniya的回答,這會使你的日曆完美的工作,但請記住,嵌套的滾動是不好的(尤其是在屏幕較小的舊款Android設備上)

    +0

    這很有趣,但我不明白。這兩位代表中的一位是否有箭頭,另一位不是?沒有任何意義的是,Android會有一個本地日曆,無論「模式」如何,您都無法更改日期。我會更新我的問題,注意我現在將使用Google Android API提供200點直接解決方案,因此我無需等待賞金期開始。 – user316117

    +0

    上面的Github示例說「_CalendarMode.WEEK和所有星期模式功能都被正式標記爲@ @Experimental。所有標記爲'@Experimental的API都可以快速更改,不應該用於生產代碼中,它們可以用於測試和反饋。 _「我的應用程序 - 一旦我得到它的工作 - 是在工業環境中的生產使用,所以我真的寧願留在股票API。 CalendarView在API 11中添加,Android 4.2.2在API 17中添加,所以我不需要考慮它是舊版本。 – user316117

    +0

    謝謝你所有的背景研究,但我不清楚它是如何導致我的問題的解決方案,如何讓本機CalendarView與Android 4.2.2正常工作。你是否說本地API版本存在根本性缺陷,並且這種方式無法工作?也就是說,對於4.2.2,編寫自定義CalendarView總是必要的,即使Google文檔說API 11以後CalendarView存在?我不可能成爲第一個使用CalendarView的人! – user316117