动态列表视图的Andr​​oid ViewPager +片段 [英] Android ViewPager + Fragments with dynamic ListViews

本文介绍了动态列表视图的Andr​​oid ViewPager +片段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用我有标签的活动(比方说10片)。每个标签页包含片段的ListView (在此ListView中显示的数据是从我的服务器动态加载)。我使用 ViewPager 来显示这些页面。我不希望保留在内存中的所有片段,所以我决定用 FragmentStatePagerAdapter (我的适配器类扩展这些类)。


  1. 让我们说第三个标签被选中。然后,当我去为例,第一个选项卡,片段这个标签应该从头开始(创建这很好,这是怎么 FragmentStatePagerAdapter 作品),但恢复previous状态(例如,的ListView 不应该被滚动到的位置保存当此片段最后一次访问 - 现在它滚动)。
    所以我的第一个问题是:我怎么能实现这样的行为?换句话说,我希望我的电源适配器没有恢复片段状态,当从头开始创建。


  2. 再一次,假设第三个标签被选中。然后,当我去为例,第二个选项卡,片段这个标签应该还是在内存中(这很好,这是怎么 FragmentStatePagerAdapter 作品),但在这种情况下,我想,以决定是否我的的ListView 数据应该(在某些条件下)进行更新。我怎样才能通知该片段,它的选择?



解决方案

有关#1,看看<一个href=\"http://speakman.net.nz/blog/2014/02/20/a-bug-in-and-a-fix-for-the-way-fragmentstatepageradapter-handles-fragment-restoration/\"相对=nofollow>这个的博客文章。他们谈论固定FragmentStatePagerAdpater处理的片段类指数的变化,这不正是你想要的情况。然而,它显示了详细的状态是如何保存/恢复,你应该能够找出如何prevent从那里。

有关#2,在持有ViewPager分片实施OnPageChangeListener。然后使用以获得当前的片段时,它改变,所以你可以调用一些它公开的方法,告诉它来更新。

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?

解决方案

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.

这篇关于动态列表视图的Andr​​oid ViewPager +片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆