Seems the refresh issue is discussed before, but none of the solutions worked for me.
What I am trying to do: I am using FragmentStatePagerAdapter. Each position of the adapter holds a fragment that has a linear layout that has chess like appearance, with each cell represented by a CheckedTextView.
And a listener that should listens for clicks and removes the previous selection on the cells and makes the new selection.
Whats working: Each fragment is loaded correctly with all the data/default states (calculated while the fragment is loaded in a list)
When I click a cell all the states from the saved cells list are fetched correctly and I can change see the state change in the code while debugging. At the same time I assign a new state for the CheckedTextView which represent a cell. This is set like:
CheckedTextView cellView = (CheckedTextView) GridRow.getChildAt(c);
cellView.setSelected(false);
cellView.setChecked(false);
cellView.setTag(cellsChess.get(i).get(c));
The Issue: Even though the values are stored and assigned correctly the view can't be refreshed the values of the cells in the view do not get updated.
Tips I have Tried that didn't worked: 1. Call adapter.notifyDataSetChanged: Can be called while using FragmentStatePagerAdapter only when we have overridden:
@Override
public int getItemPosition(Object object){
return POSITION_NONE;
}
2. Adding Overriden methods for DestroyItem and also adding the fragments to a Map on getItem:
This is not appropriate as I am working with FragmentStatePagerAdapter, so the whole purpose of using this pagerAdapter would be gone.
3. Calling invalidate: This is inappropriate for this scenario, I need realtime refresh.
4. setTag() method in instantiateItem(): I don't think this is appropriate in this situation.
Referenced threads: Refresh images on FragmentStatePagerAdapter on resuming activity
ViewPager PagerAdapter not updating the View
notifyDataSetChanged fails to update ListView
I have tried to state my issue as clear as possible, if anyone needs more clarification, do ask.
Urgent replies will be highly appreciated,
Cheers
****Update*****
I am using the ViewPagerIndicator, instead of the default ViewPager.
Fragment
s inFragmentAdapter
s are generally self-contained - unless theFragment
changes its views on its own, you really can't do much about it without hacks like aWeakReference
Map
andfindViewByTag
. If you need to do it from theActivity
and abhor the hacks, you need to establish your own way to doActivity
->Fragment
communication that doesn't rely on theActivity
being able to get a reference to theFragment
(e.g., broadcasts, event buses like Guava's - or Otto - or Ambassador, etc.). Personally, I choose the WeakReference Map - it's far simpler than the clean way. – PessaGridView
combination in your code orGridRow
is just some layout likeLinearLayout
, if it is aGridView
then consider updating your data and callingmyGridAdapter.notifyDataSetChanged();
but I guess you are not usingGridView
. In that case try callingGridRow.requestLayout();
If you are updating yourFragment
s GUI fromFragment
's code then you shouldn't be needing all this, something else is wrong. Try making one pager, one cell fragment and share your code. – FickFragmentStatePagerAdapter
. If it does not work then Please post your code so that I can serve you better... – Charleycharlie