0

I am trying to create a calendar using the Gallery widget. I have a monthview class that handles creating a view for a specific month. My MonthGallery class uses monthview as its child views while my monthviewadapter creates the monthviews for the monthgallery. The problem I am having is trying to update the adapter with the previous and next month while the user scrolls or flings to the previous or next month. I use the position given in the gallery onItemSelectedListener to add months to the front and back of the adapter depending on this position. For example:

OnItemSelectedListener mGalleryOnItemSelectedListener = new OnItemSelectedListener() {

    @Override
    public void onItemSelected(AdapterView<?> adapterview, View view, int position, long id) {

        MonthView mv = (MonthView) view;
        int month = mv.getMonth();
        int year = mv.getYear();
        updateMonthBar(month, year);


        if ((position + 1) == mMonthViewGallery.getCount()) {
            if (Calendar.DECEMBER == month) {
                mMonthViewAdapter.addView(Calendar.JANUARY, year + 1);
            } else {
                mMonthViewAdapter.addView(month + 1, year);
            }
            mMonthViewGallery.setSelection(position);
        }

        if (0 == position) {

            if (Calendar.JANUARY == month) {
                mMonthViewAdapter.addViewToFront(Calendar.DECEMBER, year - 1);
            } else {
                mMonthViewAdapter.addViewToFront(month - 1, year);
            }
            mMonthViewGallery.setSelection(1);
        }
    }

PROBLEM
Once the user has scrolled to another month and then tries to scroll to the first month the gallery does not automatically snap to the next selection. In the adapter, when addView(int month, int year) is called it will call notifyDataSetChanged() so that it updates the gallery with the previous or next month but this seems to cause the unexpected "snapping" while scrolling slowly. Just not sure how to structure this so that scrolling is smooth.

EDIT
I am now using Gallery.setCallbackDuringFling(false) but is there a way to disable the onitemselected callback while scrolling the gallery and then call onitemselected() when it's done scrolling?

2 Answers2

1

used gallery from Does a replacement for Gallery widget with View recycling exist?

removed one line from onScroll()

postDelayed(mDisableSuppressSelectionChangedRunnable, SCROLL_TO_FLING_UNCERTAINTY_TIMEOUT);
Community
  • 1
  • 1
0

Is that guaranteed about notifyDatasetChanged() called when addView() called

I think, you may check that..

I used to call notifyDatasetChanged() immediately when I touch the views inside a adapter.

lulumeya
  • 1,619
  • 11
  • 14
  • yes, in both addView() and addViewToFront(), I call notifyDatasetChanged() to update the gallery. When you say touch the views are you talking about in an onclicklistener? – Seiji Morikami Jan 17 '12 at 05:22
  • Yes, in my case, I usually called notifyDatasetChanged() with refresh buttons. my guess in your case is, (views inside gallery)'s onDraw() called when it appears to screen, then it snaps the scrolling. – lulumeya Jan 17 '12 at 05:26
  • another, I recommend, in adapterView like gallery, don't modify views inside adapterView, just modify the 'data' wired with adapter and call notifyDatasetChanged(), and let the views automatically changed with getView(). I m using this way and I having no prolem at all. – lulumeya Jan 17 '12 at 05:36