this is my Fragment Page Adapter.
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
public class MyFragmentPagerAdapter extends FragmentPagerAdapter {
private String[] pageTitle = {
"Page1", "Page2", "Page3"
};
public MyFragmentPagerAdapter(FragmentManager fragmentManager) {
super(fragmentManager);
}
@Override
public Fragment getItem(int position) {
switch (position) {
case 0: return new PageFragment();
case 1: return new PageFragment2();
case 2: return new PageFragment3();
}
Fragment fragment = new PageFragment();
Bundle arguments = new Bundle();
arguments.putString("pageIndex", Integer.toString(position + 1));
fragment.setArguments(arguments);
return fragment;
}
@Override
public int getCount() {
return pageTitle.length;
}
@Override
public CharSequence getPageTitle(int position) {
return pageTitle[position];
}
}
Why i get an error in this line: case 0: return new PageFragment(); and Fragment fragment = new PageFragment(); the error is "Type mismatch: cannot convert from PageFragment to Fragment" How to fix? Is possible make it (Viewpager) compatible with Android 2.2 or 2.3?
PageFragment
should extend Fragement
or another Fragment class (eg ListFragment
). You haven't posted a code for PageFragment
so it's hard to say.
Also, I understand you have 3 pages only so the code in getItem
is not effective as it will always return in one of the cases of switch
and the code after switch
will never be reached.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.