【发布时间】:2012-04-01 14:55:34
【问题描述】:
我有一个主 TabActivity,它有两个选项卡,A 和 B(目前)。 Tab A 加载了一个 FragmentActivity(代码如下),它只包含一个 FrameLayout,所以我可以为其中的特定 Tab 加载我的 Fragments。
第一个 Fragment 有一些 TextView 和一个 ListView。数据是从 Web 服务中提取的。当我单击 ListView 的项目时,我会将该项目的详细信息加载到另一个 Fragment(这也来自 Web 服务)中,并用另一个详细信息 Fragment 替换当前的 Fragment(带有 ListView 和其他控件)。
为了实现这一点,我使用 android-support-v4.jar 库来使用 Fragments,因为它们是首选。
Tab A 的 FragmentActivity 的 XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<FrameLayout
android:id="@+id/updates_frame"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/background"/>
</LinearLayout>
Tab A 的 FragmentActivity Java 代码:
public class UpdatesFragmentActivity extends FragmentActivity implements
IUpdateNotifier {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.updates);
//Load Initial Fragment into FrameLayout
//I am adding this Fragment to BackStack
Fragment newFragment = new UpdatesFragment();
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.addToBackStack(null);
ft.add(R.id.updates_frame, newFragment);
ft.commit();
}
//This is an Interface method which I call with the clicked "FEED" object to load its detail in another Fragment
@Override
public void onFeedSelected(Feed feed) {
// Instantiate a new fragment.
Fragment newFragment = new FeedDetailFragment(feed);
// Add the fragment to the activity, pushing this transaction
// on to the back stack.
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.updates_frame, newFragment);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
ft.addToBackStack(null);
ft.commit();
}
//This is another Interface Method which I call when the user presses "BACK".
//I am trying to load the previously loaded Fragment, which I added to BackStack.
//But this causes reconstruction of the previously loaded fragment. LIST in this case
//which call the web service. I DONT WANT TO CALL SERVICE AGAIN.
@Override
public void onBackPressed() {
FragmentManager fm = getSupportFragmentManager();
if (fm.getBackStackEntryCount() > 0) {
fm.popBackStack();
}
}
}
我创建了一个接口IUpdateNotifier,其中包含两个方法:
public void onFeedSelected(Feed feed);
public void onBackPressed();
父 UpdatesFragmentActivity 实现这些方法。在执行以下操作时,我将这些方法从子片段中调用。
- 我从具有 ListView 的 Fragment 调用 onFeedSelected(Feed feed)。我将点击的提要项发送到父 FragmentActivity,因此它会加载另一个包含该提要详细信息的 Fragment。
- 当用户按下一个按钮时,我从第二个提要详细信息片段中调用 onBackPressed(),该按钮应该带回包含 ListView 和其他控件的第一个片段。如您所见,我尝试调用 FragmentManager 的 popBackStack() 方法来恢复第一个 Fragment...
但第一个 Fragment 会刷新并从 Web 服务加载所有数据。
实际上,我不能只获取和存储一次数据,也不能在某些时间间隔内频繁更新。用户可以在需要时更新列表。最初,列表会加载服务中排名前 10 的项目,然后如果用户想要加载更多项目,可以单击列表末尾的“更多”按钮。
它将加载接下来的 10 个项目,依此类推。但我认为我可以将检索到的 ArrayList 存储在 UpdatesFragmentActivity 中的某个变量中,然后将该 ArrayList 重新分配给列表的适配器,而不是从服务中加载数据,但我不知道如何使 Fragment 不再次致电服务。
我希望它的行为类似于我单击选项卡 2,然后再次单击选项卡 1。它只是将加载的数据显示为隐藏,并且不调用服务。
我怎样才能做到这一点?
【问题讨论】:
-
你有解决办法吗
标签: android android-listview android-fragments