输入不匹配的错误

时间:2015-08-18 12:48:21

标签: android android-viewpager

我尝试按照答案How to implement a ViewPager with different Fragments / Layouts

创建Viewpager

但是我收到错误FirstFragment,SecondFragment,ThirdFragment无法在以下方法中转换为Fragment

    @Override
    public Fragment getItem(int pos) {
        switch(pos) {
        case 0: return FirstFragment.newInstance("FirstFragment, Instance 1");
        case 1: return SecondFragment.newInstance("SecondFragment, Instance 1");
        case 2: return ThirdFragment.newInstance("ThirdFragment, Instance 1");
        case 3: return ThirdFragment.newInstance("ThirdFragment, Instance 2");
        case 4: return ThirdFragment.newInstance("ThirdFragment, Instance 3");
        default: return ThirdFragment.newInstance("ThirdFragment, Default");
        }
    }

2 个答案:

答案 0 :(得分:0)

看看你的导入必须看起来像:

import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;

或:

import android.app.Fragment;
import android.view.ViewPager;

包括所有碎片。

请检查导入是否正确FirstFragmentSecondFragment

答案 1 :(得分:0)

Android Developers网站确实对ViewPager有很好的解释。你应该检查一下: http://developer.android.com/training/animation/screen-slide.html

以下是我写的一个例子:

activity_screen_slide.xml

<android.support.v4.view.ViewPager
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

Java代码:

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
...

public class ScreenSlidePagerActivity extends FragmentActivity {

    private static final int NUM_PAGES = 2;
    private ViewPager mPager;
    private PagerAdapter mPagerAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_screen_slide);

        mPager = (ViewPager) findViewById(R.id.pager);
        mPagerAdapter = new ScreenSlidePagerAdapter(getSupportFragmentManager());
        mPager.setAdapter(mPagerAdapter);
    }

    @Override
    public void onBackPressed() {
        if (mPager.getCurrentItem() == 0) {
            super.onBackPressed();
        } else {
            mPager.setCurrentItem(mPager.getCurrentItem() - 1);
        }
    }

    private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {
        public ScreenSlidePagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            //Create these fragments with your preferable names
            switch (position) {
                case 0:
                    return new ScreenSlidePageFragment();
                case 1:
                    return new ScreenSlidePageFragment2();
                default:
                    break;
            }
        }

        @Override
        public int getCount() {
            return NUM_PAGES;
        }
    }
}

以下是您的一个视图,看起来像fragment_ screen_slide_page是您的布局之一:

import android.support.v4.app.Fragment;
...

public class ScreenSlidePageFragment extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.fragment_screen_slide_page, container, false);

        return rootView;
    }
}

你应该真正阅读Android开发者网站了解更多细节。