Android ViewPager + Fragments with dynamic ListViews

南楼画角 提交于 2019-12-24 03:07:27

问题


In my app I have activity with tabs (let's say 10 tabs). Each tab page contains Fragment with ListView(data displayed in this ListView is loaded dynamically from my server). I use ViewPagerto display these pages. I don't want to keep all the Fragments in memory, so I decided to use FragmentStatePagerAdapter (My adapter class extends these class).

  1. Let's say 3rd tab is selected. Then, when I go for example to the first tab, Fragment for this tab should be created from scratch (that's fine, it's how FragmentStatePagerAdapterworks) but without restoring previous state of this fragment (e.g., ListViewshouldn't be scrolled to the position saved when this Fragment was last accessed - now it is scrolled). So my first question is: how can I achieve such a behaviour? In other words, I want my adapter not to restore Fragmentstate, when it is created from scratch.

  2. Once again, let's say 3rd tab is selected. Then, when I go for example to the second tab, Fragmentfor this tab should be still in memory (that's fine, it's how FragmentStatePagerAdapterworks), but in this situation I want to decide if my ListViewdata should be updated (depending on some conditions). How can I notify this Fragment that it's selected?


回答1:


For #1, take a look at this blog post. They talk about fixing FragmentStatePagerAdpater to handle the case where the fragment class for an index changes, which isn't exactly what you want. However, it shows in detail how state is saved/restored and you should be able to figure out how to prevent it from there.

For #2, implement OnPageChangeListener in the Fragment that holds the ViewPager. Then use this to get the current fragment when it changes so you can call some public method on it, telling it to update.




回答2:


I want my adapter not to restore Fragmentstate, when it is created from scratch.

add below code to the FragmentStatePagerAdapter

@Override
public int getItemPosition(Object object){
    return PagerAdapter.POSITION_NONE;
}

How can I notify this Fragment that it's selected?

When you load each fragment in getItem of fragmentStatePagerAdapter also pass position to the fragment using getInstance(the example of the link is good) design pattern. in the onPageSelected(int position) call a LocalBroadcastManager and put position as an extra. in each fragment check the intent and if that position in the extra is the same of the position stored in the fragment that means user selects that fragment so in onReceive method update the list.




回答3:


for #1, AFAIK, when go to the first tab, associated with that tab's Fragment would create once more just like it never visit if you're used FragmentStatePagerAdapter. now you said the ListView is scrolled, are you sure you're using FragmentStatePagerAdapter??

in other hand, if you're using FragmentPagerAdapter, it would detaching all the off Fragments from the FragmentManager but keep them in memory so that's why when you come back from 3rd tab to 1st tab directly you saw the ListView stay at last state. the behavior your mentioned make me feel that it seems you're using FragmentPagerAdapter rather than FragmentStatePagerAdapter.

if you confirm that was FragmentStatePagerAdapter, I believe if you don't restore the state manually(i.e. working with the onSaveInstanceState() method in Fragment), the come back Fragment will certainly restore as just been born.

for #2, if you want to do something when Fragment comes fully visible, you can override the setUserVisibleHint method to putting your code over there.

public class YourFragement extend android.support.v4.app.Fragment {
    ...
    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (conditions satisfied) {
            // do your job
        }
    }
    ...
}


来源:https://stackoverflow.com/questions/27474270/android-viewpager-fragments-with-dynamic-listviews

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!