Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FragmentPagerAdapter override getItem() method clash

In order to implement sliding tabs in Android, i am following this guide: Google Play Style Tabs using TabLayout

At the point of implement the FragmentPageAdapter i am having a problem whith the "getItem()" method which is supossed to return the fragment with the associated position, in this case "PageFragment.newinstance(position + 1)". Being PageFragment a generic Fragment. The problem itself is:

'getItem(int)' in 'com.myProject.SampleFragmentPagerAdapter' clashed with 'getItem(int)' in 'android.support.v4.app.FragmentPagerAdapter'; attempting to use incompatible return types

Can someone figure out where the problem is?

I have attached the SampleFragmentPagerAdapter of the guide for faster checks:

    public class SampleFragmentPagerAdapter extends FragmentPagerAdapter {
    final int PAGE_COUNT = 3;
    private String tabTitles[] = new String[] { "Tab1", "Tab2", "Tab3" };
    private Context context;

    public SampleFragmentPagerAdapter(FragmentManager fm, Context context) {
        super(fm);
        this.context = context;
    }

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

    @Override
    public Fragment getItem(int position) {
        return PageFragment.newInstance(position + 1);
    }

    @Override
    public CharSequence getPageTitle(int position) {
        // Generate title based on item position
        return tabTitles[position];
    }
}
like image 640
SamuelPS Avatar asked Dec 19 '22 01:12

SamuelPS


1 Answers

Solved, the problem was that SampleFragmentPagerAdapter class uses android.support.v4.app.Fragment

I was using, android.app.Fragment in PageFragment class.

That resulted in getItem method of SampleFragmentPagerAdapter having a clash between types because of diferent libraries.

Solution? Change the import line from

android.app.Fragment 

to

android.support.v4.app.Fragmentin PageFragment Class.
like image 57
SamuelPS Avatar answered Dec 27 '22 11:12

SamuelPS