StaggeredGridView
StaggeredGridView copied to clipboard
notifyDataSetChanged(); problem
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
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 Why don't you provide your fix via a pull request?
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 can you please provide this via pull request.
@myksb1223 Its still not done with me... infact notifyDataSetChanged() is not refreshing data on UI at all.
@zhangxin1989 thanks for your solution, you solved my problem