3

我想要使用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傳呼機內?

我是一個絕對的初學者,我很抱歉,如果這些都是天真的問題。

乾杯。

+1

只是好奇,爲什麼它'選擇'變量'靜態'? – Geros

+0

我以前用來設置從其他片段中選擇的值,這是在對象之間傳遞數據的一種懶惰方式。 – Geoplex

+0

當您想要將值從一個片段傳輸到另一個片段,然後使用接口。不使用靜態變量,這是不正確的方式..! –

回答

1

這是對您有用,

public class ViewPagerAdapter extends FragmentStatePagerAdapter { 
    private List<Fragment> fragments=null; 
    private FragmentManager fragmentManager=null; 
    public ViewPagerAdapter(FragmentManager fragmentManager,List<Fragment> fragments) { 
     super(fragmentManager); 
     this.fragments=fragments; 
     this.fragmentManager=fragmentManager; 
    } 
    @Override 
    public Fragment getItem(int position) { 
     fragmentManager.beginTransaction().commitAllowingStateLoss(); 
     return fragments.get(position); 
    } 
    @Override 
    public int getCount() { 
     return fragments.size(); 
    } 
    @Override 
    public void setPrimaryItem(ViewGroup container, int position, Object object) 
    { 
     super.setPrimaryItem(container,0,fragments.get(0)); 
    } 
    @Override 
    public void notifyDataSetChanged() 
    { 
     super.notifyDataSetChanged(); 
    } 
    @Override 
    public void destroyItem(ViewGroup collection, int position, Object view) { 
     fragmentManager.executePendingTransactions(); 
     fragmentManager.saveFragmentInstanceState(fragments.get(position)); 
    } 
    public void replaceItem(int position,Fragment fragment) 
    { 
     fragments.set(position, fragment); 
     this.notifyDataSetChanged(); 
    } 
} 

MainActivity.java

public class MainActivity extends FragmentActivity implements OnPageChangeListener,TabListener 
{ 
    private android.support.v4.view.ViewPager mViewPager=null; 
    private ViewPagerAdapter mPagerAdapter=null; 
    private ActionBar action=null; 
    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main_layout); 
     initilizeViewPager(); 
     action=getActionBar(); 
       action.setDisplayShowHomeEnabled(false); 
       action.setDisplayShowTitleEnabled(false); 
       action.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); 
    } 
     public void initilizeViewPager() 
    { 
       mViewPager=(android.support.v4.view.ViewPager)findViewById(R.id.viewPager); 
     List<Fragment> fragments = new Vector<Fragment>(); 
       fragments.add(LaunchListFragment.newInstance(select)); 
       fragments.add(AddTeamFragment.newInstance()); 
       fragments.add(thirdFragment.newInstance()); 
       viewPagerAdapter=new ViewPagerAdapter(); 
mViewPager.setAdapter(viewPagerAdapter(getSupportFragmentManager(),fragments)); 
       new setAdapterTask().execute(); 
     mViewPager.setOnPageChangeListener(this); 
    } 
    private class setAdapterTask extends AsyncTask<Void,Void,Void> 
    { 
     protected Void doInBackground(Void... params) 
     { 
      return null; 
     } 
     @Override 
     protected void onPostExecute(Void result) 
     { 
      mViewPager.setAdapter(mPagerAdapter); 
      Tab first=action.newTab().setTag("first").setText("First").setTabListener(MainActivity.this); 
      Tab second=action.newTab().setTag("second").setText("Second").setTabListener(MainActivity.this); 
Tab third=action.newTab().setTag("third").setText("Third").setTabListener(MainActivity.this); 
      action.addTab(first); 
      action.addTab(second); 
        action.addTab(third); 
      } 
     } 
    } 
    public void onPageScrollStateChanged(int arg0) 
    {} 
    public void onPageScrolled(int arg0, float arg1, int arg2) 
    {} 
    public void onPageSelected(int position) 
    { 
     getActionBar().setSelectedNavigationItem(position); 
    } 
public void onTabReselected(Tab tab, FragmentTransaction ft) {} 
    public void onTabSelected(Tab tab, FragmentTransaction ft) { 
     mViewPager.setCurrentItem(tab.getPosition()); 
    } 
    public void onTabUnselected(Tab tab, FragmentTransaction ft) {} 
} 

main_layout。xml

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
    <android.support.v4.view.ViewPager 
     android:id="@+id/viewpager" 
     android:layout_width="fill_parent" 
     android:layout_below="@android:id/tabs" 
     android:layout_height="fill_parent"/> 
</RelativeLayout> 
+0

謝謝你的回答。我複製了您的代碼並按照說明操作,LaunchListFragment沒有顯示出來。我調試過,它添加了操作欄上的選項卡 - 該部分工作,然後用正確的值填充列表等,onView的片段被調用,只是沒有列表顯示。你認爲這與ViewPager的可見性設置有關嗎? – Geoplex

+0

設置所有片段事務應該填充到它們的父級(如活動或其父級片段)。我認爲你的交易對他們的活動,所以你處理所有查看頁面交易和添加標籤內的活動。 –

+0

我已經找出你的主要佈局有一些問題,修復第一.. –

2

問題描述非常適合我剛纔的問題。我知道這個問題很老,但這可能會幫助其他面對同樣的問題。 @Geoplex沒有顯示LaunchPageAdapter類的代碼。所以我不確定這是否解決了他的問題。但對我來說,工作。

在我的PagerAdapter中, public boolean isViewFromObject(View view, Object object)被覆蓋。我刪除了該方法並允許superClass處理它。這開始給我預期的結果。