2015-04-21 30 views
15

我已實施代碼爲android times square,但當月份發生更改時,我無法檢測到該事件。 I have to highlight multiple range of dates from months在安卓時代廣場上滾動的月份檢測監聽器

這是我做的方式,但我無法在本月得到了改變,以檢測在滾動

代碼:

public class Calender extends Activity implements OnDateChangeListener,CalendarCellDecorator { 

String BOOKING_URL="http://www.example.com/app/webroot/mobile/booking.php"; 
ArrayList<String>list=new ArrayList<String>(); 
CalendarPickerView calender; 
ArrayList<Date> dates = new ArrayList<Date>(); 
Calendar lastYear,nextYear; 
Calendar today; 



@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.calender); 

    getActionBar().setTitle("CALENDER"); 
    getActionBar().setIcon(R.drawable.back); 
    getActionBar().setDisplayOptions(getActionBar().getDisplayOptions()| ActionBar.DISPLAY_SHOW_CUSTOM); 
    ImageView imageView = new ImageView(getActionBar().getThemedContext()); 
    imageView.setScaleType(ImageView.ScaleType.CENTER); 
    imageView.setImageResource(R.drawable.manu_btn); 
    ActionBar.LayoutParams layoutParams = new ActionBar.LayoutParams(ActionBar.LayoutParams.WRAP_CONTENT,ActionBar.LayoutParams.WRAP_CONTENT, Gravity.RIGHT| Gravity.CENTER_VERTICAL); 
    layoutParams.rightMargin = 40; 
    imageView.setLayoutParams(layoutParams); 
    getActionBar().setCustomView(imageView); 
    centerActionBarTitle(); 
    imageView.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 

     } 
    }); 

    nextYear = Calendar.getInstance(); 
    nextYear.add(Calendar.YEAR, 2); 

    lastYear= Calendar.getInstance(); 
    lastYear.add(Calendar.YEAR, -2); 

    today = Calendar.getInstance(); 

    calender = (CalendarPickerView) findViewById(R.id.calendar); 
    calender.setOnScrollListener(new OnScrollListener() { 

     private int mLastFirstVisibleItem; 

     @Override 
     public void onScrollStateChanged(AbsListView view, int scrollState) { 

     } 

     @Override 
     public void onScroll(AbsListView view, int firstVisibleItem, 
       int visibleItemCount, int totalItemCount) { 



      if(mLastFirstVisibleItem<firstVisibleItem) 
      { 
       Log.d("SCROLLING DOWN","TRUE"); 
      } 
      if(mLastFirstVisibleItem>firstVisibleItem) 
      { 
       Log.d("SCROLLING UP","TRUE"); 

      } 
      mLastFirstVisibleItem=firstVisibleItem; 

     } 

    }); 


    calender1.init(lastYear.getTime(), nextYear.getTime());//.inMode(SelectionMode.RANGE); 



    new MyAsyncTask(BOOKING_URL).execute(); 

} 

..... 


try { 

      JSONArray array=new JSONArray(response); 
      for (int i = 0; i < array.length(); i++) { 

       JSONObject json=array.getJSONObject(i); 
       list.add(json.getString("fromDate")+" "+json.getString("toDate")); 

      } 

      SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); 
      TimeZone gmt = TimeZone.getTimeZone("GMT"); 
      sdf.setTimeZone(gmt); 
      sdf.setLenient(false); 

      try { 
       Date date = sdf.parse(list.get(0).split(" ")[0]); 
       Date date1 = sdf.parse(list.get(0).split(" ")[1]); 
       System.out.println(date); 
       dates.add(date); 
       dates.add(date1); 
      } catch (Exception e) { 
       e.printStackTrace(); 
      }     

      calendar.init(lastYear.getTime(), nextYear.getTime()).inMode(SelectionMode.RANGE).withSelectedDates(dates);    

     } catch (JSONException e) { 
      e.printStackTrace(); 

     } 

.... 

@Override 
public void decorate(CalendarCellView calendarCellView, Date date) { 

    Log.d("TAG", "a"+date); 

    if (date.getDate() < 5) { 
     calendarCellView.setBackgroundResource(R.drawable.side_testi); 
    } else { 
     calendarCellView.setBackgroundResource(R.drawable.side_book); 
    } 
} 

JSON響應:

[ 
    { 
     "fromDate": "2014-03-03", 
     "toDate": "2014-03-07", 
     "name": "Anurag", 
     "email": "[email protected]", 
     "phone": "2147483647", 
     "address": "Y-51, 3rd Floor, Sector 12", 
     "comment": "I wanna view here." 
    }, 
    { 
     "fromDate": "2014-04-09", 
     "toDate": "2014-04-09", 
     "name": "Dana Edgar", 
     "email": "[email protected]", 
     "phone": "0", 
     "address": "BHM Construction Inc.\r\n522 Walnut Avenue\r\nVallejo, CA 94592", 
     "comment": "Primary guest will be Jeffery Mazet and his contact phone number is (530)601-0570. His email is [email protected]" 
    }, 
    { 
     "fromDate": "2015-02-09", 
     "toDate": "2015-02-25", 
     "name": "Ghfb", 
     "email": "[email protected]", 
     "phone": "2147483647", 
     "address": "\nGffbbnn", 
     "comment": "Dhgbngh" 
    }, 
    { 
     "fromDate": "2015-02-09", 
     "toDate": "2015-02-25", 
     "name": "Ghfb", 
     "email": "[email protected]", 
     "phone": "2147483647", 
     "address": "\nGffbbnn", 
     "comment": "Dhgbngh" 
    }, 
    { 
     "fromDate": "2015-06-04", 
     "toDate": "2015-09-04", 
     "name": "rewr", 
     "email": "[email protected]", 
     "phone": "2147483647", 
     "address": "cbvcb", 
     "comment": "vnbvbbm" 
    } 
] 

佈局:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="@android:color/white" 
    android:orientation="vertical" > 

    <com.squareup.timessquare.CalendarPickerView 
     android:id="@+id/calendar" 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1" 
     android:paddingLeft="16dp" 
     android:paddingRight="16dp" 
     android:paddingBottom="16dp" 
     android:scrollbarStyle="outsideOverlay" 
     android:clipToPadding="false" 
     android:background="#FFFFFF" 
     /> 


</LinearLayout> 

enter image description here


問題提出

看來我失去了一些東西。任何建議表示讚賞。

+3

[我有同樣的問題(http://chat.stackoverflow.com/transcript/message/22850437)。 –

回答

7

使用CalendarCellDecorator你可以寫這樣的事情:

public class MonthDecorator implements CalendarCellDecorator { 

    @Override 
    public void decorate(CalendarCellView calendarCellView, Date date) { 
     if (date.getDate() < 5) { 
      calendarCellView.setBackgroundResource(R.drawable.red_background); 
     } else { 
      calendarCellView.setBackgroundResource(R.drawable.blue_background); 
     } 
    } 
} 

這是一個簡單的例子 - 第5天在每月都會有不同的背景比天的休息。每個月中的每個單元格都通過裝飾器,因此您可以使用它在多個月內更改單元格的UI。 通常,您必須存儲應突出顯示的日期CalendarCellDecoratordecorate方法檢查給定的cellView是否顯示這些日期之一。就我而言,我只是檢查日期是否在5日之前,但是你可以在那裏做一個更有意義的檢查 - 例如給出一個日期範圍並檢查給定日期是否在這個範圍內。

編輯:展示如何應用裝飾:

calendar.init(lastYear.toDate(), nextYear.toDate()).inMode(CalendarPickerView.SelectionMode.RANGE); 
List<CalendarCellDecorator> decoratorList = new ArrayList<>(); 
decoratorList.add(new MonthDecorator()); 
calendar.setDecorators(decoratorList); 
+0

我會盡力並更新你。 – Gattsu

+0

@Samuil Yanovski'裝飾(CalendarCellView calendarCellView,日期日期)'不會在構建日曆時或在滾動時調用.....所以不適用於我:-( –

+0

你的意思是說它永遠不會被調用?可以嗎?編輯你的問題,包括與CalendarCellDecorator相關的代碼 - 即類和日曆的setDecorators方法的調用?:) –