Question

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?

Was it helpful?

Solution

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);

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top