0
我想使用擴展片段史迪威片段
public Fragment getItem(int position) {
// getItem is called to instantiate the fragment for the given page.
// Return a DummySectionFragment (defined as a static inner class
// below) with the page number as its lone argument.
Fragment fragment = new MappingPage();
Bundle args = new Bundle();
args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, position + 1);
fragment.setArguments(args);
return fragment;
}
我已經寫了一個名爲MappingPage(類中的方法),如果我嘗試使用上面的方法,我創建這個片段類型MappingPage的,因爲有已經擴展並作爲函數返回一個片段(而不是MappingPage對象)
EDIT因此拋出一個錯誤:我收到的時候我做
Fragment fragment = new MappingPage();
Eclipse的告訴我,我的東東錯誤d將片段更改爲MappingPage類型,這意味着我必須更改函數的返回類型
兩個問題 1)您應如何將自定義片段放入此頁?
2)爲什麼dummySectionFrament返回一個Fragment對象而不是DummySectionFragment對象?
在此先感謝
public static class DummySectionFragment extends Fragment {
/**
* The fragment argument representing the section number for this
* fragment.
*/
public static final String ARG_SECTION_NUMBER = "section_number";
public DummySectionFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main_dummy,
container, false);
TextView dummyTextView = (TextView) rootView
.findViewById(R.id.section_label);
dummyTextView.setText(Integer.toString(getArguments().getInt(
ARG_SECTION_NUMBER)));
return rootView;
}
}
這裏是我的類
public class MappingPage extends Fragment
{
private MapView map;
private MapController myMapController;
public static final String ARG_SECTION_NUMBER = "1";
public MappingPage() {
}
public View onCreateView (LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Context context = new ContextThemeWrapper(getActivity(), R.style.fragment_theme);
//LayoutInflater Inflater = inflater.cloneInContext(context);
View view=inflater.inflate(R.layout.fragment_main_dummy, container, false);
return view;
}
@Override
public void onResume() {
map = (MapView)getActivity().findViewById(R.id.openmapview);
map.setBuiltInZoomControls(true);
myMapController = map.getController();
myMapController.setZoom(15);
super.onResume();
}
}
這應該工作。你得到什麼樣的錯誤? –
你不會混淆'Fragment'導入。碎片既可以在SDK中也可以在支持庫中生存,並且不能混合使用。這是很常見的錯誤。您需要使用'ViewPager'的支持風格,因此請確保導入點指向'android.support.v4.app.Fragment'和* not *'android.app.Fragment'。 –
MH我已經更改了MappingPage上的導入,它的工作原理非常完美,非常感謝 –