FragmentPagerAdapter不显示内容

时间:2017-03-02 08:12:50

标签: android android-fragments android-viewpager

我的FragmentPagerAdapter视图寻呼机实现没有显示任何内容。我试图将其更改为FragmentStatePagerAdapter,但它根本没用。

主要活动类

   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"/>

1 个答案:

答案 0 :(得分:3)

从XML中的ViewPager中删除

android:visibility="gone"