问题描述
我试图与viewpager和片段一个slidescreen,这样我可以每个片段加载不同的布局,并给每个页面不同的功能。
我也跟着tutorial要做到这一点。
我徘徊在公开片段的getItem(INT为arg0)
时收到错误:返回类型是不符合 FragmentPagerAdapter.getItem(INT )
和错误#2:构造 FragmentPagerAdapter(FragmentManager)
未定义 - >获得这个徘徊超(FM)时;
包com.example.spui;
进口android.os.Bundle;
进口android.app.Fragment;
进口android.app.FragmentManager;
进口android.support.v4.app.FragmentPagerAdapter;
公共类MyFragmentPagerAdapter扩展FragmentPagerAdapter {
最终诠释PAGE_COUNT = 5;
/ **类的构造函数* /
公共MyFragmentPagerAdapter(FragmentManager FM){
超(FM);
}
/ **当一个页面被请求创建*这个方法将被调用/
@覆盖
公共片段的getItem(INT为arg0){
MyFragment myFragment =新MyFragment();
捆绑数据=新包();
data.putInt(current_page,为arg0 + 1);
myFragment.setArguments(数据);
返回myFragment;
}
/ **返回的页面数量* /
@覆盖
公众诠释getCount将(){
返回PAGE_COUNT;
}
}
您使用了错误的 FragmentManager
导入。使用 android.support.v4.app.FragmentManager
代替。
同样的问题与片段
- 使用 android.support.v4.app.Fragment
请注意:如果你正在建立一个API11 +只应用程序,并要使用本机的碎片,那么你应该将改变你的 FragmentPagerAdapter
进口安卓.support.v13.app.FragmentPagerAdapter
。
I'm trying to make a slidescreen with viewpager and fragments so that I can load different layouts per fragment and give each page different functionality.
I followed a tutorial to accomplish this.
The error I'm getting when hovering over public Fragment getItem(int arg0)
: The return type is incompatible with FragmentPagerAdapter.getItem(int)
and error #2: The constructor FragmentPagerAdapter(FragmentManager)
is undefined--> getting this one when hovering super(fm);
package com.example.spui;
import android.os.Bundle;
import android.app.Fragment;
import android.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
public class MyFragmentPagerAdapter extends FragmentPagerAdapter{
final int PAGE_COUNT = 5;
/** Constructor of the class */
public MyFragmentPagerAdapter(FragmentManager fm) {
super(fm);
}
/** This method will be invoked when a page is requested to create */
@Override
public Fragment getItem(int arg0) {
MyFragment myFragment = new MyFragment();
Bundle data = new Bundle();
data.putInt("current_page", arg0+1);
myFragment.setArguments(data);
return myFragment;
}
/** Returns the number of pages */
@Override
public int getCount() {
return PAGE_COUNT;
}
}
You are using the wrong FragmentManager
import. Use android.support.v4.app.FragmentManager
instead.
Same problem with Fragment
- use android.support.v4.app.Fragment
Note: if you are building an API11+ only application and want to use the native Fragments, then you should instead change your FragmentPagerAdapter
import to android.support.v13.app.FragmentPagerAdapter
.
这篇关于与FragmentPagerAdapter问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!