2014-07-16 3 views
0

getItem(), 매번 조각을 만들 수 있습니까?FragmentPagerAdapter를 올바르게 사용하고 있습니까?

그리고 FragmentPagerAdapter와 함께 fragmentActivity를 만드는 좋은 예는 무엇입니까?

나는

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 = new DummySectionFragment(); 
      Bundle args = new Bundle(); 
      args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, position + 1); 
      fragment.setArguments(args); 

      switch(position) 
      { 
      case 0: 
       Fragment fragment_tab1 = new MainActivity_Fragment_Tab_02(getApplicationContext()); 
       fragment_tab1.setRetainInstance(true); 

       return fragment_tab1; 
      case 1: 
       Fragment fragment_tab2 = new MainActivity_Fragment_Tab_01(getApplicationContext()); 
       fragment_tab2.setRetainInstance(true); 
       return fragment_tab2; 
      case 2: 
       Fragment fragment_tab3 = new MainActivity_Fragment_Tab_03(getApplicationContext()); 
       fragment_tab3.setRetainInstance(true); 
       return fragment_tab3; 

      } 
      return fragment; 
     } 

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

답변

1

그것은 괜찮아 보인다, 예상대로 작동하지 않습니다 ... 질문 fooly 요청에 대한 미안 해요? getItem()은 뷰 페이지에서 한 번 호출되며, 그 후 조각을 메모리에 보관하여 한 번 더 호출 할 필요가 없습니다.

 Fragment fragment = new DummySectionFragment(); 
     Bundle args = new Bundle(); 
     args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, position + 1); 
     fragment.setArguments(args); 

 default: 

스위치의 경우 지난

에, 코드를보다 효율적으로하기 : 내가 바꿀 것

것은 이동하는 것입니다. 정적 수를 가지고 당신이 이러한 활동에 탭으로 변경되지 않습니다 것을 알고 있다면 http://developer.android.com/training/implementing-navigation/lateral.html

+0

답변을위한 Thx는 Zezeq !! – HoJunLee

+0

Np! 당신이 대답에 만족한다면 그것을 upvote 및/또는 허용 대답으로 표시하십시오 – Zezeq

1

, 당신은 FragmentStatePagerAdapter 대신 FragmentPagerAdapter을 사용해야합니다

여기 좋은 사례가 더 많은 것을 알고.

FragmentStatePagerAdapter은 조각을 볼 때 메모리에 조각을 보관할 것이므로 필요할 때보기 계층이 파괴 될 수 있습니다.

FragmentPagerAdapter은 동적 콘텐츠 용이며 스 와이프하여 다른 조각을 가져올 때마다 조각이 삭제되지만 상태가 저장됩니다.

관련 문제