View Pager setting the index to the last element on calling notifydatasetchanged()

383 views Asked by At

I have a PagerAdapter that takes in a Cursor in the constructor and uses that cursor to get the values from the database to populate the views.

This is what I am doing:

  • On 5 times swipe down action of the view page, I set the visibility of one of the view to be invisible.
  • To save the count I just used the preferences where I increment and the count.
  • In the onResumeFragment(), I do a check if the count is some_number and I do a adapter.notifyDataSetChanged(). I do have extra check on the adapter level as well.
  • On calling the notifyDataSetChanged(), the index moves to the last item in an array and I am assuming its because of the notifyChanged() in DataSetObservable class.
  • notifyChanged() is actually called when we do call the notifyDataSetChanged(). Notify Changed method matches the list in the reverse order and that' probably the reason I am getting the last index when calling the notifyDataSetChanged().

This is how the code for notifyChanged() looks like from the source:

/**
     * Invokes {@link DataSetObserver#onChanged} on each observer.
     * Called when the contents of the data set have changed.  The recipient
     * will obtain the new contents the next time it queries the data set.
     */
    public void notifyChanged() {
        synchronized(mObservers) {
            // since onChanged() is implemented by the app, it could do anything, including
            // removing itself from {@link mObservers} - and that could cause problems if
            // an iterator is used on the ArrayList {@link mObservers}.
            // to avoid such problems, just march thru the list in the reverse order.
            for (int i = mObservers.size() - 1; i >= 0; i--) {
                mObservers.get(i).onChanged();
            }
        }
    }

What I want to achieve :

  • After calling the notifyDataSetChanged(), my view pager should just refresh with one of the view disabled and the position of the ViewPager shouldn't go the last index of the ViewPager.
  • Need to know exactly how and when does the notifyDataSetChanged() works. I did get some info through this STACK_OVERFLOW_POST though.

What I have tried so far :

  • called notifyDataSetChanged() on the onResumeFragment() and called the viewPager.setCurrentPosition(some_index) after that.

  • Tried to Recreate the adapter(Least expected way).

  • I have a swapCursor method in the adapter that I used to switch the cursor when any of the conditions come true.

This is code for SwapCursor :

public void swapCursor(final Cursor c) {
        if (someCursor == c)
            return;
        else
        if(someCursor!=null)
            someCursor.close();

        this.someCursor = c;
        this.someCursor.moveToFirst();
        itemPosition = 0;
        notifyDataSetChanged();

    } 

Any recommendations on how I can do a workaround or maybe a good solution for it. Help is really appreciated. Thanks.

0

There are 0 answers