2012-07-20 88 views
0

我正在開發一個轉換器,我有兩個Android輪子實例。當用戶滾動一個車輪時,第二個車輪應自動計算並將第二個車輪的數據更改爲該結果。反之亦然。Android滾輪滾動第一輪滾動第一輪時

問題是,當輪子滾動和更改時,邏輯是循環的,每次都會調用滾動的偵聽器和更改的偵聽器,這會導致StackOverFlowError。

如何獲得第二個輪轉換並設置用戶滾動第一個輪的日期?反之亦然。

代碼:

@Override 
public void onCreate(Bundle savedInstanceState) { 

    ...... 


    changedlistener = new OnWheelChangedListener() { 
     public void onChanged(WheelView wheel, int oldValue, int newValue) { 

     if (wheel.getId() == R.id.month || wheel.getId() == R.id.day || wheel.getId() == R.id.year) { 
       updateDays(year, month, day); 
     } else if (wheel.getId() == R.id.hijrimonth || wheel.getId() == R.id.hijriday || wheel.getId() == R.id.hijriyear) { 
       hijriDays(hijriyear, hijrimonth, hijriday); 
     } 


     } 
    }; 

    .....scrolledlistener event...... 

    // month 

    month.addChangingListener(changedlistener); 
    month.addScrollingListener(scrolledlistener); 

    // year 
    year.addChangingListener(changedlistener); 
    year.addScrollingListener(scrolledlistener); 


    //day 
    day.addChangingListener(changedlistener); 
    day.addScrollingListener(scrolledlistener); 

    hijrimonth.addChangingListener(changedlistener); 
    hijrimonth.addScrollingListener(scrolledlistener); 

    hijriyear.addChangingListener(changedlistener); 
    hijriyear.addScrollingListener(scrolledlistener); 

    hijriday.addChangingListener(changedlistener); 
    hijriday.addScrollingListener(scrolledlistener); 

} 

/** 
* Updates day wheel. Sets max days according to selected month and year 
*/ 
void updateDays(WheelView year, WheelView month, WheelView day) { 
    Calendar calendar = Calendar.getInstance(); 
    calendar.set(Calendar.YEAR, calendar.get(Calendar.YEAR) + year.getCurrentItem()); 
    calendar.set(Calendar.MONTH, month.getCurrentItem()); 

    int maxDays = calendar.getActualMaximum(Calendar.DAY_OF_MONTH); 
    day.setViewAdapter(new DateNumericAdapter(this, 1, maxDays, calendar.get(Calendar.DAY_OF_MONTH) - 1)); 
    int curDay = Math.min(maxDays, day.getCurrentItem() + 1); 
    day.setCurrentItem(curDay - 1, true); 
    int curYear = calendar.get(Calendar.YEAR); 
    year.setCurrentItem(curYear); 
    text.setText((day.getCurrentItem()+1) + " " + getMonthAsString(month.getCurrentItem()) + " " +year.getCurrentItem()); 
    //setHijri(year.getCurrentItem(), month.getCurrentItem(), day.getCurrentItem()); 
} 

void hijriDays(WheelView year, WheelView month, WheelView day) { 

    hijritext.setText((day.getCurrentItem()+1) + " " + hijri.getHijriMonthName(month.getCurrentItem()+1) + " " + year.getCurrentItem()); 
    setGreg(year.getCurrentItem(), month.getCurrentItem(), day.getCurrentItem()); 
} 

public void setGreg(int y, int m, int d) { 
    hdate = hijri.islToGreg(y, m, d, 0); 
    hmonth = hdate[1]; 
    hyear = hdate[2]; 
    hday = hdate[0]; 

    month.setCurrentItem(hmonth); 
    day.setCurrentItem(hday); 
    year.setCurrentItem(hyear); 

} 

public void setHijri(int y, int m, int d) { 
    hdate = hijri.GregToIsl(y, m, d, 0); 
    hmonth = hdate[1]; 
    hyear = hdate[2]; 
    hday = hdate[0]; 

    hijrimonth.setCurrentItem(hmonth); 
    hijriday.setCurrentItem(hday); 
    hijriyear.setCurrentItem(hyear); 
} 

回答

2

我認爲最好的解決方案是使用一個標誌來從輪子上跳過重複的事件;像下一樣:

changedlistener = new OnWheelChangedListener() { 
    boolean inProgress = false; 

    public void onChanged(WheelView wheel, int oldValue, int newValue) { 

    if (!inProgress) { 
     return; 
    } 
    inProgress = true; 

    try { 
     if (wheel.getId() == R.id.month || wheel.getId() == R.id.day || wheel.getId() == R.id.year) { 
      updateDays(year, month, day); 
     } else if (wheel.getId() == R.id.hijrimonth || wheel.getId() == R.id.hijriday || wheel.getId() == R.id.hijriyear) { 
      hijriDays(hijriyear, hijrimonth, hijriday); 
     } 
     } 
     finally { 
      inProgress = false; 
     } 
    } 
}; 
+0

小修正:if(inProgress){return; } – kankan 2012-07-26 09:21:04

0

當用戶開始滾動啓動一個倒數計時器。定期對每個增量進行計算和調整。換句話說,您不需要捕捉每個滾動更改事件。如果它還沒有開始,趕上開始倒數計時器。然後檢查滾動位置並相應地進行調整。

+0

示例代碼將不勝感激。 – input 2012-07-20 23:24:25