2013-11-09 2 views
2

안녕하세요, Android의 새로운 기능입니다. 도움을 요청하고 싶습니다. 임 안드로이드 스튜디오에서 새 프로젝트를 만드는 중이며 탐색 창 작업 막대 탭을 뷰 페이지와 함께 사용합니다. Android Studio에서이 코드를 생성합니다. 하나의 액티비티 atc로 작업하는 것을 알고 있지만, 이제 탭으로 스 와이프 레이아웃을 사용하여 작업을 배우고 싶습니다. 내 질문은 :이 내부의 다른 항목으로 더 많은 레이아웃을 수행 할 수 있습니까? 예를 들어, 이제 모든 조각 하나에 textview가 있고, 내가 스 와이프하면 모든 페이지에 텍스트가 표시됩니다. 하지만 tab2에 textview가있는 페이지 (tab1) 레이아웃 예가 필요합니다. tab3에 레이아웃 edittext가 필요합니다. 이미지가있는 레이아웃이 필요합니다. 이걸로 가능합니까? 텍스트를 바꿀 수 있기 때문에 모든 탭에서 동일한 레이아웃을 사용할 수 있습니다.페이지 레이아웃이 다른보기 표시 줄이있는 작업 표시 줄

public class MainActivity extends ActionBarActivity implements ActionBar.TabListener { 

SectionsPagerAdapter mSectionsPagerAdapter; 
ViewPager mViewPager; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    final ActionBar actionBar = getSupportActionBar(); 
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); 
    mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager()); 
    mViewPager = (ViewPager) findViewById(R.id.pager); 
    mViewPager.setAdapter(mSectionsPagerAdapter); 

    mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() { 
     @Override 
     public void onPageSelected(int position) { 
      actionBar.setSelectedNavigationItem(position); 
     } 
    }); 

    for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) { 
     actionBar.addTab(
       actionBar.newTab() 
         .setText(mSectionsPagerAdapter.getPageTitle(i)) 
         .setTabListener(this)); 
    } 
} 


@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    getMenuInflater().inflate(R.menu.main, menu); 
    return true; 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    int id = item.getItemId(); 
    if (id == R.id.action_settings) { 
     return true; 
    } 
    return super.onOptionsItemSelected(item); 
} 

@Override 
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) { 
    // When the given tab is selected, switch to the corresponding page in 
    // the ViewPager. 
    mViewPager.setCurrentItem(tab.getPosition()); 
} 

@Override 
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) { 
} 

@Override 
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) { 
} 

public class SectionsPagerAdapter extends FragmentPagerAdapter { 

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

    @Override 
    public Fragment getItem(int position) { 
     return PlaceholderFragment.newInstance(position + 1); 
    } 

    @Override 
    public int getCount() { 
     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; 
    } 
} 
public static class PlaceholderFragment extends Fragment { 
    private static final String ARG_SECTION_NUMBER = "section_number"; 
    public static PlaceholderFragment newInstance(int sectionNumber) { 
     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) { 
     View rootView = inflater.inflate(R.layout.fragment_main, container, false); 
     TextView textView = (TextView) rootView.findViewById(R.id.section_label); 
     textView.setText(Integer.toString(getArguments().getInt(ARG_SECTION_NUMBER))); 
     return rootView; 
    } 
} 

}는 3 개 별도의 조각 클래스를 정의 할 수 있습니다이 경우

+0

이렇게하면 도움이 될 것입니다. http://www.gadgetsaint.com/android/create-viewpager-tabs-android/#.WPuppVN97BI – ASP

답변

7

. 그런 다음 당신은 당신의 SectionsPagerAdaptergetItem에 적절하게 반환 할 수 있습니다

@Override 
public Fragment getItem(int position) { 
    switch (position) { 
     case 0: 
      return new YourFragmentClass1(); 
     case 1: 
      return new YourFragmentClass2(); 
     case 2: 
      return new YourFragmentClass3(); 
    } 
} 
+0

SectionPagerAdapter에서 getItem의 코드를 공유하고 싶습니까? – CodeGuru

1

는 인 Szymon의 대답에 정교한.

1 단계는 내부 클래스 PlaceHolderFragment 2 단계 삭제 : 해당 레이아웃으로, 당신의 조각 클래스를 만듭니다. 예를 들어, ImageViewFragment (물론 조각을 확장) 및 image_view_fragment_layout입니다. 3 단계ImageViewFragmentonCreateView 메서드가 image_view_fragment_layout 인 것으로 나타났습니다.

public ImageViewFragment extends Fragment { 
    ... 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
     Bundle savedInstanceState) { 
    View rootView = inflater.inflate(R.layout.image_view_fragment_layout, 
                container, false); 
    return rootView; 
    } 

추가 할 탭 수만큼 다음 단계를 따르십시오.

그런 다음 getItem() 방법은 Szymon의 대답을 따르십시오. 첫 번째로 표시 할 탭이 0 위치에있는 식으로 계속 반복됩니다. 당신의 getCount 방법에

하나 중요한 것은 더 : 수를 추가 한 탭의 수를 반영해야 반환

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

.