0
當創建與全息碎片和標籤和sectionsPagerAdapter模板項目,你得到這樣的事情使用SectionsPagerAdapter定製片段
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int i) {
Fragment fragment = new DummySectionFragment();
Bundle args = new Bundle();
args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, i + 1);
fragment.setArguments(args);
return fragment;
}
DummySectionFragment的定義:
/**
* A dummy fragment representing a section of the app, but that simply displays dummy text.
*/
public static class DummySectionFragment extends Fragment {
public DummySectionFragment() {
}
public static final String ARG_SECTION_NUMBER = "section_number";
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
TextView textView = new TextView(getActivity());
textView.setGravity(Gravity.CENTER);
Bundle args = getArguments();
textView.setText(Integer.toString(args.getInt(ARG_SECTION_NUMBER)));
return textView;
}
}
現在,我將這段代碼我自己的片段擴展:
@Override
public Fragment getItem(int i) {
Fragment fragment = new TargetsFragment();
Bundle args = new Bundle();
我在自己的定義文件:
public class TargetsFragment extends Fragment {
boolean mDualPane;
int mCurCheckPosition = 0;
private CheckedExpandableListAdapter expandableListAdapter;
private ExpandableListView expandableListView;
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
//TODO: serve with correct data instead of this!
final ArrayList<ListNode> dummyList = buildDummyData();
loadHosts(dummyList);
}
[...]
我得到的錯誤是不能將TargetsFragment轉換爲getItem中的片段。我覺得我在這裏錯過了一些簡單的東西。但是什麼?如果您需要更多代碼,請詢問。