我想要使用ViewPager顯示三個選項卡片段。最初我曾經使用FragmentMgr從Activity實例化片段,這工作得很好。當我使用ViewPager轉換此導航時,此片段不再顯示。ViewPager顯示片段時遇到的問題
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_laun ch_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和的onCreate查看被調用,它只是將不會被顯示出來。 :(
能有人在這裏讓我知道如果我在我的ViewPager實現這裏缺少什麼?
更新
當我打電話LaunchListFragment不使用Viewpager,我首先設置內容視圖,並通過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();
當我把它改爲使用v iewPager,我不確定我指示要顯示片段的位置。在我的片段onView中,我正在給fragment_launch_list充氣並返回視圖。但是,視圖分頁器將如何知道在哪裏設置分段視圖。它是否在ID傳呼機內?
我是一個絕對的初學者,我很抱歉,如果這些都是天真的問題。
乾杯。
只是好奇,爲什麼它'選擇'變量'靜態'? – Geros
我以前用來設置從其他片段中選擇的值,這是在對象之間傳遞數據的一種懶惰方式。 – Geoplex
當您想要將值從一個片段傳輸到另一個片段,然後使用接口。不使用靜態變量,這是不正確的方式..! –