如何在android选项卡式菜单中显示片段(ViewPager)

时间:2013-07-28 15:20:32

标签: java android android-fragments android-viewpager

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

我使用FragmentPagerAdapter成功实现了所有内容,只是因为我只需要3个标签。为此,我使用了这个页面:http://developer.android.com/reference/android/support/v4/app/FragmentPagerAdapter.html

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

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

一些代码:

这是onCreateView根据标签/片段的ID设置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;
}

然后是fragment_pager_list

的xml布局
<?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>

1 个答案:

答案 0 :(得分:0)

你的意思是,你想保存标签的状态吗?

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

请告诉我,这是解决方案。