0

タブを使用するこのアクティビティがありますが、ネストされたフラグメントを 1 つのタブだけに含める方法がわかりません。

私はこれを達成したかった:

Tab 1
--Fragment A --> Fragment B

ドキュメントから

フラグメントをネストするには、フラグメントを追加する Fragment で getChildFragmentManager() を呼び出すだけです。これにより FragmentManager が返され、トップレベルのアクティビティから通常のように使用してフラグメント トランザクションを作成できます。たとえば、既存の Fragment クラス内からフラグメントを追加するコードを次に示します。

Fragment videoFragment = new VideoPlayerFragment();
FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
transaction.add(R.id.video_fragment, videoFragment).commit();

SampleTabsWithIcons

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;

import com.example.tabwithicon.R;
import com.viewpagerindicator.IconPagerAdapter;
import com.viewpagerindicator.TabPageIndicator;

public class SampleTabsWithIcons extends FragmentActivity {
    private static final String[] CONTENT = new String[] { "Calendar", "Camera", "Alarms", "Location" };
    private static final int[] ICONS = new int[] {
            R.drawable.perm_group_calendar,
            R.drawable.perm_group_camera,
            R.drawable.perm_group_device_alarms,
            R.drawable.perm_group_location,
    };

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sample_tabs_with_icons);

        FragmentPagerAdapter adapter = new GoogleMusicAdapter(getSupportFragmentManager());

        ViewPager pager = (ViewPager)findViewById(R.id.pager);
        pager.setAdapter(adapter);

        TabPageIndicator indicator = (TabPageIndicator)findViewById(R.id.indicator);
        indicator.setViewPager(pager);
    }

    class GoogleMusicAdapter extends FragmentPagerAdapter implements IconPagerAdapter {
        public GoogleMusicAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            Toast.makeText(getBaseContext(), "Fragments: "+CONTENT[position % CONTENT.length].toString(), 
                    Toast.LENGTH_LONG).show();
            return TestFragment.newInstance(CONTENT[position % CONTENT.length]);
        }

        @Override
        public CharSequence getPageTitle(int position) {
            Toast.makeText(getBaseContext(), "Fragments Title: "+CONTENT[position % CONTENT.length].toString(), 
                    Toast.LENGTH_LONG).show();
            return CONTENT[position % CONTENT.length].toUpperCase();
        }

        @Override public int getIconResId(int index) {

          return ICONS[index];
        }

      @Override
        public int getCount() {
          return CONTENT.length;
        }
    }
}

テストフラグメント

public final class TestFragment extends Fragment {
    private static final String KEY_CONTENT = "TestFragment:Content";

    public static TestFragment newInstance(String content) {
        TestFragment fragment = new TestFragment();

        StringBuilder builder = new StringBuilder();
        for (int i = 0; i < 30; i++) {
            builder.append(content).append(" ");
        }
        builder.deleteCharAt(builder.length() - 1);
        fragment.mContent = builder.toString();

        return fragment;
    }

    private String mContent = "???";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if ((savedInstanceState != null) && savedInstanceState.containsKey(KEY_CONTENT)) {
            mContent = savedInstanceState.getString(KEY_CONTENT);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        TextView text = new TextView(getActivity());
        text.setGravity(Gravity.CENTER);
        text.setText(mContent);
        text.setTextSize(20 * getResources().getDisplayMetrics().density);
        text.setPadding(20, 20, 20, 20);


        inflater = getLayoutInflater(savedInstanceState);

        LinearLayout layout = (LinearLayout) inflater.inflate(R.layout.activity_inside_activity, null);

        //LinearLayout layout = new LinearLayout(getActivity());
        layout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
        layout.setGravity(Gravity.CENTER);
        layout.addView(text);

        return layout;
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putString(KEY_CONTENT, mContent);
    }
}

activity_sample_tabs_with_icons

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <com.viewpagerindicator.TabPageIndicator
        android:id="@+id/indicator"
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        />
    <android.support.v4.view.ViewPager
        android:id="@+id/pager"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        />
</LinearLayout>

ログキャット

07-12 14:33:30.685: W/IInputConnectionWrapper(18607): beginBatchEdit on inactive InputConnection
07-12 14:33:30.685: W/IInputConnectionWrapper(18607): endBatchEdit on inactive InputConnection
07-12 14:33:31.984: W/IInputConnectionWrapper(18607): beginBatchEdit on inactive InputConnection
07-12 14:33:31.984: W/IInputConnectionWrapper(18607): endBatchEdit on inactive InputConnection
07-12 14:33:33.279: W/IInputConnectionWrapper(18607): beginBatchEdit on inactive InputConnection
07-12 14:33:33.280: W/IInputConnectionWrapper(18607): endBatchEdit on inactive InputConnection
07-12 14:33:34.707: W/IInputConnectionWrapper(18607): beginBatchEdit on inactive InputConnection
07-12 14:33:34.709: W/IInputConnectionWrapper(18607): endBatchEdit on inactive InputConnection
07-12 14:33:34.710: W/IInputConnectionWrapper(18607): beginBatchEdit on inactive InputConnection
07-12 14:33:34.711: W/IInputConnectionWrapper(18607): endBatchEdit on inactive InputConnection
07-12 14:33:34.806: W/IInputConnectionWrapper(18607): beginBatchEdit on inactive InputConnection
07-12 14:33:34.807: W/IInputConnectionWrapper(18607): endBatchEdit on inactive InputConnection
07-12 14:33:34.830: W/IInputConnectionWrapper(18607): beginBatchEdit on inactive InputConnection
07-12 14:33:34.832: W/IInputConnectionWrapper(18607): endBatchEdit on inactive InputConnection
07-12 14:33:36.579: W/IInputConnectionWrapper(18607): beginBatchEdit on inactive InputConnection
07-12 14:33:36.580: W/IInputConnectionWrapper(18607): endBatchEdit on inactive InputConnection
07-12 14:33:36.583: W/IInputConnectionWrapper(18607): beginBatchEdit on inactive InputConnection
07-12 14:33:36.584: W/IInputConnectionWrapper(18607): endBatchEdit on inactive InputConnection
07-12 14:33:36.704: W/IInputConnectionWrapper(18607): beginBatchEdit on inactive InputConnection
07-12 14:33:36.709: W/IInputConnectionWrapper(18607): endBatchEdit on inactive InputConnection
07-12 14:33:36.797: W/IInputConnectionWrapper(18607): beginBatchEdit on inactive InputConnection
07-12 14:33:36.798: W/IInputConnectionWrapper(18607): endBatchEdit on inactive InputConnection
07-12 14:33:37.071: W/IInputConnectionWrapper(18607): beginBatchEdit on inactive InputConnection
07-12 14:33:37.071: W/IInputConnectionWrapper(18607): endBatchEdit on inactive InputConnection
07-12 14:37:21.166: W/IInputConnectionWrapper(19086): beginBatchEdit on inactive InputConnection
4

1 に答える 1