2012-12-26 37 views
0

我試圖使用一個頁面標題條,我需要在我的一個片段中放置三個seekbars。這是我的代碼:如何在頁面標題欄中添加更多視圖?

public class MainActivity extends FragmentActivity { 

/** 
* The {@link android.support.v4.view.PagerAdapter} that will provide 
* fragments for each of the sections. We use a 
* {@link android.support.v4.app.FragmentPagerAdapter} derivative, which 
* will keep every loaded fragment in memory. If this becomes too memory 
* intensive, it may be best to switch to a 
* {@link android.support.v4.app.FragmentStatePagerAdapter}. 
*/ 
SectionsPagerAdapter mSectionsPagerAdapter; 

/** 
* The {@link ViewPager} that will host the section contents. 
*/ 
ViewPager mViewPager; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

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

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

} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.activity_main, menu); 
    return true; 
} 

/** 
* 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 DummySectionFragment (defined as a static inner class 
     // below) with the page number as its lone argument. 
     Fragment fragment = null; 
     Bundle args = new Bundle(); 
     switch (position) { 
     case 0: 
      fragment = new Page1(); 
      args.putInt(Page1.ARG_SECTION_NUMBER, position + 1); 
      break; 
     case 1: 
      fragment = new Page2(); 
      args.putInt(Page2.ARG_SECTION_NUMBER, position + 1); 
      break; 
     case 2: 
      fragment = new Page3(); 
      args.putInt(Page3.ARG_SECTION_NUMBER, position + 1); 
      break; 
     } 
     fragment.setArguments(args); 
     return fragment; 
    } 

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

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

/** 
* A dummy fragment representing a section of the app, but that simply 
* displays dummy text. 
*/ 
public static class Page1 extends Fragment { 
    /** 
    * The fragment argument representing the section number for this 
    * fragment. 
    */ 
    public static final String ARG_SECTION_NUMBER = "section_number"; 

    public Page1() { 
    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
      Bundle savedInstanceState) { 
     // Create a new TextView and set its text to the fragment's section 
     // number argument value. 
     // TextView textView = new TextView(getActivity()); 
     // textView.setGravity(Gravity.CENTER); 
     // textView.setText(Integer.toString(getArguments().getInt(
     // ARG_SECTION_NUMBER))); 
     SeekBar sb1 = new SeekBar(getActivity()); 
     SeekBar sb2 = new SeekBar(getActivity()); 
     SeekBar sb3 = new SeekBar(getActivity()); 
     return sb1; 
    } 
} 

public static class Page2 extends Fragment { 
    /** 
    * The fragment argument representing the section number for this 
    * fragment. 
    */ 
    public static final String ARG_SECTION_NUMBER = "section_number"; 

    public Page2() { 
    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
      Bundle savedInstanceState) { 
     // Create a new TextView and set its text to the fragment's section 
     // number argument value. 
     TextView textView = new TextView(getActivity()); 
     textView.setGravity(Gravity.LEFT); 
     textView.setText("Second tab"); 
     return textView; 
    } 
} 

public static class Page3 extends Fragment { 
    /** 
    * The fragment argument representing the section number for this 
    * fragment. 
    */ 
    public static final String ARG_SECTION_NUMBER = "section_number"; 

    public Page3() { 
    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
      Bundle savedInstanceState) { 
     // Create a new TextView and set its text to the fragment's section 
     // number argument value. 
     TextView textView = new TextView(getActivity()); 
     textView.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.RIGHT); 
     textView.setText("third tab"); 
     return textView; 
    } 
} 
} 

這實際上工作,但我只有一個seekbar在第一頁。何可以把他們三個?謝謝!!!!

回答

1

這個代碼:

SeekBar sb1 = new SeekBar(getActivity()); 
    SeekBar sb2 = new SeekBar(getActivity()); 
    SeekBar sb3 = new SeekBar(getActivity()); 
    return sb1; 
要創建3個SeekBars但只返回第一個

。在返回聲明之後,另外兩個人正在快速收集垃圾。 在onCreateView方法中,您必須創建一個GroupView,並且該組將擁有3個SeekBars或任何您需要的佈局。最好/最簡單的方法是創建一個XML佈局,並使用在該函數上傳遞的LayoutInflater。類似於:

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
     Bundle savedInstanceState) { 
    return inflater.inflate(R.layout.frag_page1, null); 
} 
+0

非常感謝你!!!!!! – Cippo

相關問題