2017-03-02 65 views
0

我的視圖尋呼機實現與FragmentPagerAdapter不顯示任何內容。我試圖將其更改爲FragmentStatePagerAdapter,但它根本沒有幫助。FragmentPagerAdapter不顯示內容

主要活動類

public enum TYPES { 
     OBJECT_TYPE, TRACKS} 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     SimpleObject simpleObject = new SimpleObject("left","inside","right"); 
     ViewPager viewPager = (ViewPager)findViewById(R.id.visitMuseumPager); 
     viewPager.setAdapter(new ViewPagerAdapter(this.getSupportFragmentManager(), simpleObject, TYPES.TRACKS)); 
    } 

ViewPagerAdapter

public class ViewPagerAdapter extends FragmentStatePagerAdapter { 

private SimpleObject simpleObject; 
MainActivity.TYPES type; 
public ViewPagerAdapter(FragmentManager fm, SimpleObject simpleObjects, MainActivity.TYPES type) { 
    super(fm); 
    this.simpleObject =simpleObjects; 
    this.type=type; 
} 

@Override 
public Object instantiateItem(ViewGroup container, int position) { 
    return super.instantiateItem(container, position); 
} 

@Override 
public Fragment getItem(int position) { 
    Bundle bundle = new Bundle(); 
    bundle.putParcelable("simpleObject", simpleObject); 
    if(type.equals(TRACKS)){ 
    switch (position){ 
     case 0:{ 
      LeftFragment leftFragment = new LeftFragment(); 
      leftFragment.setArguments(bundle); 
      return leftFragment; 
     } 
     case 1:{ 
      InsideFragment insideFragment = new InsideFragment(); 
      insideFragment.setArguments(bundle); 
      return insideFragment; 
     } 
     case 2:{ 
      RightFragment rightFragment = new RightFragment(); 
      rightFragment.setArguments(bundle); 
     return rightFragment; 
     } 
    }} 
    else if (type.equals(OBJECT_TYPE)){ 
     switch (position){ 
     case 0:{ 
      LeftFragment leftFragment = new LeftFragment(); 
      leftFragment.setArguments(bundle); 
      return leftFragment; 
     } 
     case 1:{ 
      InsideFragment insideFragment = new InsideFragment(); 
      insideFragment.setArguments(bundle); 
      return insideFragment; 
     }} 
    } 

    return null; 
} 

@Override 
public int getCount() { 

    if (type.equals(TRACKS))return 3; 
    else return 2; 
}} 

片段實現(其中其餘類同)

public class LeftFragment extends Fragment { 


    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     View view = inflater.inflate(R.layout.left_tab, container, false); 
     TextView textView=(TextView) view.findViewById(R.id.leftTab); 
     SimpleObject simpleObject = getArguments().getParcelable("simpleObject"); 
     textView.setText(simpleObject.getLeftVal()); 
     return view; 
    } 
} 


主要活動XML

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:id="@+id/activity_main" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
tools:context="com.example.k.testy.MainActivity" 
android:orientation="vertical" 
android:weightSum="1"> 



<android.support.v4.view.ViewPager 
    android:id="@+id/visitMuseumPager" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:visibility="gone" 
    android:animateLayoutChanges="true" 
    /></LinearLayout> 


左片段XML

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:weightSum="1" 
android:background="@color/black"> 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="0dp" 
    android:layout_weight=".1"> 
    <include layout="@layout/tab_buttons"/> 
</LinearLayout> 

<TextView 
    android:id="@+id/leftTab" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:gravity="center" 
    android:text="LEFT"/> 

回答

2

從ViewPager在XML刪除

android:visibility="gone" 
+0

:我怎麼能錯過,謝謝:D – Expiredmind