2013-07-12 3 views
0

J'ai cette activité qui utilise des onglets, mais je ne suis pas sûr d'inclure des fragments imbriqués dans un seul onglet.Implémentation de fragments imbriqués avec Android avec TabViewer

Je voulais y parvenir:

Tab 1 
--Fragment A --> Fragment B 

De Docu

Pour imbriquer un fragment, appelez simplement getChildFragmentManager() sur le Fragment dans lequel vous souhaitez ajouter un fragment . Cela renvoie un FragmentManager que vous pouvez utiliser comme vous le faites normalement à partir de l'activité de haut niveau pour créer des transactions fragmentées. Par exemple, voici quelques code qui ajoute un fragment à partir d'un fragment classe existante:

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

TestFragment

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> 

LogCat

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 

Répondre

0

je me suis dit moi-même, je dois vous assurer que je suis dans la bonne page pour mettre les fragments imbriqués. En utilisant le switch, j'ai été capable d'imbriquer un fragment. Dans LaunchpadSectionFragment() étend un fragment, à l'intérieur de ce fragment appelle le getChildFragmentManager() pour commencer l'imbrication d'un autre fragment à l'intérieur LaunchpadSectionFragment.

@Override 
     public Fragment getItem(int i) { 
      switch (i) { 
      case 0: 

       return new LaunchpadSectionFragment(); 
} 

Nesting:

Fragment videoFragment = new VideoPlayerFragment(); 
Fragment videoFragment2 = new VideoPlayerFragment2(); 
FragmentTransaction transaction = getChildFragmentManager().beginTransaction(); 
transaction.add(R.id.video_fragment, videoFragment); 
transaction.add(R.id.video_fragment2, videoFragment2); 
transaction.commit() 
Questions connexes