1

私は完全に停電しており、フラグメントの操作に慣れていません。

Eclipse アシスタントを使用して、新しい Android プロジェクトを作成しました。

ここに画像の説明を入力

作成されたデフォルト クラスのスニペットを次に示します。

    /**
 * A {@link FragmentPagerAdapter} that returns a fragment corresponding to
 * one of the sections/tabs/pages.
 */
public class SectionsPagerAdapter extends FragmentPagerAdapter {

    public SectionsPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        // getItem is called to instantiate the fragment for the given page.
        // Return a PlaceholderFragment (defined as a static inner class
        // below).
        return PlaceholderFragment.newInstance(position + 1);
    }

    @Override
    public int getCount() {
        // Show 3 total pages.
        return 3;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        Locale l = Locale.getDefault();
        switch (position) {
        case 0:
            return getString(R.string.title_section1).toUpperCase(l);
        case 1:
            return getString(R.string.title_section2).toUpperCase(l);
        case 2:
            return getString(R.string.title_section3).toUpperCase(l);
        }
        return null;
    }
}

/**
 * A placeholder fragment containing a simple view.
 */
public static class PlaceholderFragment extends Fragment {
    /**
     * The fragment argument representing the section number for this
     * fragment.
     */
    static int sec;
    private static final String ARG_SECTION_NUMBER = "section_number";

    /**
     * Returns a new instance of this fragment for the given section number.
     */
    public static PlaceholderFragment newInstance(int sectionNumber) {
        sec = sectionNumber;
        Log.i("ARG_SECTION_NUMBER"," "+sec);
        PlaceholderFragment fragment = new PlaceholderFragment();
        Bundle args = new Bundle();
        args.putInt(ARG_SECTION_NUMBER, sectionNumber);
        fragment.setArguments(args);
        return fragment;
    }

    public PlaceholderFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        int jjj = sec;

        Log.i("onCreateView"," "+sec);
        View rootView = inflater.inflate(R.layout.fragment_main, container,
                false);
        return rootView;
    }
}

SectionsPagerAdapter はアダプターとして使用され、フラグメントを返します 3 つのフラグメントはすべて同じレイアウトを持っています

fragment_main.xml

3 つのフラグメントに異なるレイアウトを割り当てるにはどうすればよいですか?

PlaceholderFragment には、section_number を持つ Bundle が含まれています。

args.putInt(ARG_SECTION_NUMBER, sectionNumber);

この情報を使用して、各フラグメントに表示するレイアウトを決定できますか?

    // Create the adapter that will return a fragment for each of the three
    // primary sections of the activity.
    mSectionsPagerAdapter = new SectionsPagerAdapter(
            getSupportFragmentManager());

    // Set up the ViewPager with the sections adapter.
    mViewPager = (ViewPager) findViewById(R.id.pager);
    mViewPager.setAdapter(mSectionsPagerAdapter);
4

1 に答える 1

2

ページごとに異なるクラスを作成する必要があります。ページは Fragment から拡張されます。フラグメントごとに異なる layout-xml をロードするだけです。

public class FirstFragment extends Fragment{

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {


    View rootView = inflater.inflate(R.layout.<yourxmlhere>, container,
            false);
    return rootView;
}

}

フラグメントがあることはわかっていますが、アダプターはどのページ (位置) がどのフラグメントであるかを知る必要があります。これは、次の関数で決定されます。

@Override
public Fragment getItem(int position) {
    switch (position){
    case 0:
    //page 1
    return new FirstFragment();
    break;

    case 1:
    //page 2
    return new SecondFragment();
    break;
    default:
    //this page does not exists
    return null;
}

正しいページ数を設定していることを確認してください。

@Override
public int getCount() {
    //the amount of pages your adapter knows
    return <youramountofpages>;
}

これで起動して実行できるようになります。

編集:プレースホルダーフラグメントクラス全体を削除できます。もう必要ありません。

于 2015-02-12T00:12:54.640 に答える