0

ホロ フラグメントとタブとセクション PagerAdapter を使用してテンプレート プロジェクトを作成すると、次のような結果が得られます。

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;
    }
}

次に、このコードを Fragment の独自の拡張機能に置き換えます。

@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);
    }
    [...]

getItem で TargetsFragment を Fragment に変換できないというエラーが表示されます。ここで単純なものが欠けているように感じます。しかし、何?さらにコードが必要な場合は、お問い合わせください。

4

1 に答える 1

3

解決策: インポートをよく見てください。両方のクラスで同じ Fragment をインポートして拡張してください。例: android.app.fragment または import android.support.v4.app.Fragment;

于 2013-05-16T20:11:28.493 に答える