[英]How to show fragments in android tabbed menu (ViewPager)
So I'm working on a tabbed (swipe) view for my app using google's guide: 因此,我正在使用Google指南在我的应用程序的选项卡式(滑动式)视图中工作:
http://developer.android.com/training/implementing-navigation/lateral.html#tabs http://developer.android.com/training/implementing-navigation/lateral.html#tabs
I implemented everything successfully using FragmentPagerAdapter
instead, just because I only need 3 tabs. 我改用
FragmentPagerAdapter
成功实现了所有操作,因为我只需要3个标签即可。 For that I used this page: http://developer.android.com/reference/android/support/v4/app/FragmentPagerAdapter.html 为此,我使用了此页面: http : //developer.android.com/reference/android/support/v4/app/FragmentPagerAdapter.html
The thing is that the views are changing according to an id (the view is created on the fly), and my question is how'd I change it to show any fragment (that was previously created) instead of the current behavior? 问题是视图根据ID进行更改(视图是动态创建的),我的问题是如何更改它以显示任何片段(先前创建的)而不是当前行为?
As I said, I used the tutorials above. 正如我所说,我使用了上面的教程。
Some code: 一些代码:
This is the onCreateView
that sets the value of a TextView
according to the id of the tab/fragment: 这是
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;
}
Then there is the xml layout of fragment_pager_list
然后是
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>
Do you mean, you want to save the state of tab ? 您是说要保存tab状态吗?
use this to save the state of tab
this.mViewPager.setOffscreenPageLimit(3)
Let me know, this is the solution or not. 让我知道,这是解决方案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.