0

所以我正在使用谷歌的指南为我的应用程序开发一个选项卡式(滑动)视图:http:
//developer.android.com/training/implementing-navigation/lateral.html#tabs

我成功地实现了一切FragmentPagerAdapter,只是因为我只需要 3 个选项卡。为此我使用了这个页面:http: //developer.android.com/reference/android/support/v4/app/FragmentPagerAdapter.html

问题是视图正在根据 id 更改(视图是动态创建的),我的问题是如何更改它以显示任何片段(先前创建的)而不是当前行为?

正如我所说,我使用了上面的教程。

一些代码:

这是根据选项卡/片段的 idonCreateView设置 a 值的方法:TextView

public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.fragment_pager_list, container, false);
    View tv = v.findViewById(R.id.text);
    ((TextView)tv).setText("Fragment #" + mNum);
    return v;
}

然后是xml布局fragment_pager_list

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:drawable/gallery_thumb">

<TextView android:id="@+id/text"
    android:layout_width="match_parent" android:layout_height="wrap_content"
    android:gravity="center_vertical|center_horizontal"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:text="@string/hello_world"/>

<!-- The frame layout is here since we will be showing either
the empty view or the list view.  -->
<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="0dip"
    android:layout_weight="1" >
    <!-- Here is the list. Since we are using a ListActivity, we
         have to call it "@android:id/list" so ListActivity will
         find it -->
    <ListView android:id="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:drawSelectorOnTop="false"/>

    <!-- Here is the view to show if the list is emtpy -->
    <TextView android:id="@android:id/empty"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="No items."/>

</FrameLayout>

4

1 回答 1

0

Do you mean, you want to save the state of tab ?

use this to save the state of tab
this.mViewPager.setOffscreenPageLimit(3)

Let me know, this is the solution or not.

于 2013-07-29T09:05:32.880 回答