我正在尝试使用ViewPager显示带有三个选项卡的片段。最初,我使用FragmentMgr从Activity中实例化片段,这很好用。但当我使用ViewPager进行导航时,此片段不再显示。
MainActivity.java
如果我像这样初始化片段,则会显示它。例如:
我尝试修改上面的代码,使用ViewPager,代码如下:
当我调试这个程序时,我发现LaunchListFragment已经被实例化了,onCreate和onCreateView方法也被调用了,但它就是不显示出来。:(
请问有没有人能告诉我在ViewPager的实现中我是否漏掉了什么?
更新:
当我不使用ViewPager调用LaunchListFragment时,我首先设置内容视图,然后传递R.id.item_detail_container,这是我想要显示该片段的FrameLayout的ID。请参阅前面的main.xml。
当我将它改为使用ViewPager时,我不确定我要在哪里指定要显示的Fragment。在我的Fragment的onView方法中,我正在填充fragment_launch_list并返回View。但是viewpager将如何知道在哪里设置片段视图?它是在id pager内部吗?
我是一个绝对的初学者,如果这些问题都很幼稚,请原谅。
干杯。
MainActivity.java
如果我像这样初始化片段,则会显示它。例如:
LaunchListFragment fragment = new LaunchListFragment();
fragment.newInstance(LIST_TYPE);
getSupportFragmentManager().beginTransaction()
.add(R.id.item_detail_container,
fragment).commit();
我尝试修改上面的代码,使用ViewPager,代码如下:
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(new LaunchPageAdapter(getSupportFragmentManager(),LIST_TYPE));
mViewPager.setCurrentItem(0);
在哪里LaunchPageAdapter调用LaunchListFragment。
LaunchPageAdapter.java
public class LaunchPageAdapter extends FragmentPagerAdapter {
private static String select="";
public LaunchPageAdapter(FragmentManager fm, String type) {
super(fm);
select=type;
}
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return LaunchListFragment.newInstance(select);
case 1:
return AddTeamFragment.newInstance();
}
return null;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return 2;
}
这里是LaunchListFragment.java
public class LaunchListFragment extends ListFragment implements ActionBar.TabListener {
public static String LIST_TYPE = "invalid";
GenericListData g_data[] = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View result = inflater.inflate(R.layout.fragment_launch_list,
container, false);
setActionTabs();
setList();
return (result);
}
public static Fragment newInstance(String type) {
Fragment frag = new LaunchListFragment();
Bundle args = new Bundle();
LIST_TYPE=type;
args.putString(LIST_TYPE, type);
frag.setArguments(args);
return (frag);
}
这是MainActivity使用的main.xml布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools= "match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:baselineAligned="false"
android:divider="?android:attr/dividerHorizontal"
android:orientation="horizontal"
android:showDividers="middle"
tools:context=".MainActivity" >
<fragment
android:id="@+id/item_list"
android:name="com.teammanager.ItemListFragment"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
tools:layout="@android:layout/list_content" />
<FrameLayout
android:id="@+id/item_detail_container"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="3" >
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
</FrameLayout>
fragment_launch_list.xml 是 LaunchListFragment 使用的文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:drawSelectorOnTop="false" >
</ListView>
<TextView
android:id="@+id/itemName"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentTop="true"
android:layout_marginBottom="5dp"
android:layout_marginTop="5dp"
android:gravity="center_vertical"
android:textColor="#000000"
android:textSize="22dp"
android:textStyle="bold" />
当我调试这个程序时,我发现LaunchListFragment已经被实例化了,onCreate和onCreateView方法也被调用了,但它就是不显示出来。:(
请问有没有人能告诉我在ViewPager的实现中我是否漏掉了什么?
更新:
当我不使用ViewPager调用LaunchListFragment时,我首先设置内容视图,然后传递R.id.item_detail_container,这是我想要显示该片段的FrameLayout的ID。请参阅前面的main.xml。
setContentView(R.layout.main.xml);
LaunchListFragment fragment = new LaunchListFragment();
fragment.newInstance(LIST_TYPE);
getSupportFragmentManager().beginTransaction()
.add(R.id.item_detail_container,
fragment).commit();
当我将它改为使用ViewPager时,我不确定我要在哪里指定要显示的Fragment。在我的Fragment的onView方法中,我正在填充fragment_launch_list并返回View。但是viewpager将如何知道在哪里设置片段视图?它是在id pager内部吗?
我是一个绝对的初学者,如果这些问题都很幼稚,请原谅。
干杯。
select
变量是static
的? - Glenn