StaggeredGridView icon indicating copy to clipboard operation
StaggeredGridView copied to clipboard

notifyDataSetChanged(); problem

Open navabi opened this issue 12 years ago • 6 comments

hi,

how can i return to last position that user were scrolled to , after calling notifyDataSetChanged(); in adapter.

I googled about it and found the method setSelection(int position) but it is used in listview not StaggeredGridView

tanks in advance

navabi avatar Jan 29 '13 09:01 navabi

hello...

I am not good at English. I apologize in advance regarding this.

I had same problem. But I solved this issue.

StaggeredGridView.java please annotate... private class AdapterDataSetObserver extends DataSetObserver { @Override public void onChanged() { ... 2032 : //mFirstPosition = 0; 2033 : //Arrays.fill(mItemTops, 0); 2034 : //Arrays.fill(mItemBottoms, 0); } ... }

But if you change your data (ex, (ArrayList<String>)data.clear();), don't work notifyDataSetChanged() method. So you add function in StaggeredGridView.java.

public void setDefaultState() {
mFirstPosition = 0;
Arrays.fill(mItemTops, 0);
Arrays.fill(mItemBottoms, 0);       
}

Before you call notifyDataSetChanged() method, you have to call setDefaultState() method. (ex, (StaggeredGridView)gridview.setDefaultState();)

Thanks...

myksb1223 avatar Feb 14 '13 21:02 myksb1223

@myksb1223 Why don't you provide your fix via a pull request?

ubuntudroid avatar Jul 16 '13 09:07 ubuntudroid

I solve like this: private class AdapterDataSetObserver extends DataSetObserver { @Override public void onChanged() { ....... mSync = true; mSyncPosition = mFirstPosition; mSpecificTop = getChildAt(0).getTop(); // reset list if position does not exist or id for position has changed if(mFirstPosition > mItemCount-1 || mAdapter.getItemId(mFirstPosition) != mFirstAdapterId){ mFirstPosition = 0; Arrays.fill(mItemTops, 0); Arrays.fill(mItemBottoms, 0);

            if(mRestoreOffsets!=null)
            Arrays.fill(mRestoreOffsets, 0);
        }
  .......

}

private void populate(boolean clearData) { ........ mPopulating = false; mDataChanged = false;

    if(mSync) {
        int firstPosition = getFirstPosition();
        int lastVisiblePosition = getLastVisiblePosition();
        while(mSyncPosition < firstPosition || mSyncPosition > lastVisiblePosition) {
            if (mSyncPosition > lastVisiblePosition) {
                trackMotionScroll(-300, false);
            } else if (mSyncPosition < firstPosition ) {
                trackMotionScroll(300, false); 
            }
            firstPosition = getFirstPosition();
            lastVisiblePosition = getLastVisiblePosition();
        }

        View view = getChildAt(mSyncPosition - firstPosition);
        int delta = 0;
        if (view != null) {
            delta = view.getTop() - mSpecificTop;
        }
        if (delta != 0) {
            trackMotionScroll(-delta, false);
        }
        mSync = false;
        mSyncPosition = INVALID_POSITION;
    }
    if(clearData){
        if(mRestoreOffsets!=null)
            Arrays.fill(mRestoreOffsets,0);
    }

}

zhangxin1989 avatar Jul 18 '13 02:07 zhangxin1989

@zhangxin1989 can you please provide this via pull request.

briangriffey avatar Oct 18 '13 01:10 briangriffey

@myksb1223 Its still not done with me... infact notifyDataSetChanged() is not refreshing data on UI at all.

Seema-Android avatar Jul 10 '15 05:07 Seema-Android

@zhangxin1989 thanks for your solution, you solved my problem

scola avatar Aug 05 '15 06:08 scola