0

나는 ABS와 ViewPagerIndicatior를 사용하고있다. 사용자 지정 어댑터로 채우는 내보기 호출기에서 사용자 지정 목록보기가있는 세 조각이 있습니다.SlidingFragmentActivity : 다른 조각을 가진 뷰 페이저 인스턴스를 저장하는 방법

public class MyActivity extends SlidingFragmentActivity implements  ViewPager.OnPageChangeListener{ 

protected SliderLeft sliderLeft; 
protected SliderRight sliderRight; 
ActionBar mActionBar; 
ViewPager mViewPager; 
TitlePageIndicator mPageIndicator; 
FragmentPagerAdapter mPagerAdapter; 
SubMenu subMenu1; 
private Menu mainMenu; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    // set the Behind View 
    setBehindContentView(R.layout.slider_menu_frame); 

    // customize the SlidingMenu 
    SlidingMenu sm = getSlidingMenu(); 
    sm.setShadowWidthRes(R.dimen.shadow_width); 
    sm.setShadowDrawable(R.drawable.shadowleft); 

    DisplayMetrics metrics = new DisplayMetrics(); 
    getWindowManager().getDefaultDisplay().getMetrics(metrics); 

    //int heightOfScreen = metrics.heightPixels; 
    int width = metrics.widthPixels; 

    sm.setBehindWidth(width/2); //sm.setBehindOffsetRes(R.dimen.slidingmenu_offset); 
    setSlidingActionBarEnabled(true); 
    getSlidingMenu().setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN); 
    mActionBar = getSupportActionBar(); 

    // set viewPager and viewPagerIndicator 
    mViewPager = (ViewPager) findViewById(R.id.pager); 
    mPagerAdapter = new FragmentPagerAdapter(getSupportFragmentManager()); 
    mViewPager.setOffscreenPageLimit(3); 
    mViewPager.setAdapter(mPagerAdapter); 
    mViewPager.setOnPageChangeListener(this); 

    mPageIndicator = (TitlePageIndicator) findViewById(R.id.indicator); 
    mPageIndicator.setOnPageChangeListener(this); 
    mPageIndicator.setViewPager(mViewPager); 
    mPageIndicator.setCurrentItem(1); 

} 


} 

내 응용 프로그램의 UI는 야후 응답 응용 프로그램과 동일한 역할 : I 포트 또는 그 구절에 땅에서 방향을 변경할 때 방향 변경 및 뷰 다시

주요 활동을 다시 할 때 지금, onActivityCreated 다시 호출합니다.

뷰 페이저 어댑터는 다음 뷰 페이저 내부

public class FragmentPagerAdapter extends FragmentStatePagerAdapter { 

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

@Override 
public Fragment getItem(int i) { 
    Fragment fragment = null; 
    switch (i){ 
     case 0: 
      Fragment_Review fragment_review = new Fragment_Review(); 
      Bundle args0 = new Bundle(); 
      args0.putInt(Fragment_Activities.ARG_OBJECT, i); 
      fragment_review.setArguments(args0); 
      fragment = fragment_review; 
      break; 
     case 1: 
      Fragment fragment_activities = new Fragment_Activities(); 
      Bundle args1 = new Bundle(); 
      args1.putInt(Fragment_Activities.ARG_OBJECT, i); 
      fragment_activities.setArguments(args1); 
      fragment = fragment_activities; 
      break; 
     case 2: 
      Fragment_Answer fragment_answerit = new Fragment_Answer(); 
      Bundle args2 = new Bundle(); 
      args2.putInt(Fragment_Activities.ARG_OBJECT, i); 
      fragment_answerit.setArguments(args2); 
      fragment = fragment_answerit; 
      break; 
    } 
    return fragment; 
} 

@Override 
public int getCount() { 
    return 3; 
} 

@Override 
public CharSequence getPageTitle(int position) { 

    CharSequence pageTitle = ""; 

    switch (position){ 
     case 0: 
      pageTitle = "tiile"; 
      break; 
     case 1: 
      pageTitle = "title"; 
      break; 
     case 2: 
      pageTitle = "tille"; 
      break; 
    } 

    return pageTitle; 
} 

} 

모든 단편 SherlockFragment에서 연장된다.

public class Fragment_Review extends SherlockFragment implements PullToRefreshBase.OnRefreshListener<ListView> { 

private List<Data> allData; 
private CustomListAdapter customListAdapter; 
private PullToRefreshListView mPullRefreshListView; 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    View v = inflater.inflate(R.layout.activity_ptr_list_fragment, container, false); 
    mPullRefreshListView = (PullToRefreshListView) v.findViewById(R.id.pull_to_refresh_listview); 
    progressBar = (ProgressBar) v.findViewById(R.id.progressBar); 
    return v; 
} 

@Override 
public void onActivityCreated(Bundle savedInstanceState) { 
    super.onActivityCreated(savedInstanceState); 
    allData = new ArrayList<Data>(); 
    mPullRefreshListView.setMode(PullToRefreshBase.Mode.PULL_UP_TO_REFRESH); 
    customListAdapter = new CustomListAdapter(getActivity(), allData); 
    mPullRefreshListView.setOnRefreshListener(this); 
    mPullRefreshListView.setAdapter(lazyQuestionListAdapter); 
    new GetData().execute(getString(R.string.url) + "get_data.php"); 
} 
} 

서버로부터 데이터를 수신하면 어댑터가 업데이트됩니다.

방향 변경시 모든 조각의 인스턴스를 저장하는 방법을 알려주세요.

답변

1
(프래그먼트)이 추천

:

@Override 
public void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 
    setRetainInstance(true); 
    allData = new ArrayList<Data>(); 
    customListAdapter = new CustomListAdapter(getActivity(), allData); 
} 
@Override 
public void onActivityCreated(Bundle savedInstanceState) 
{ 
    super.onActivityCreated(savedInstanceState); 
    mPullRefreshListView.setMode(PullToRefreshBase.Mode.PULL_UP_TO_REFRESH); 
    mPullRefreshListView.setOnRefreshListener(this); 
    mPullRefreshListView.setAdapter(lazyQuestionListAdapter); 

    //ADD condition for load data from network (like only if adapter.isEmpty()) 
    new GetData().execute(getString(R.string.url) + "get_data.php"); 
} 
+0

THX하지만보기 다시 –

+0

예 재현하지만 보통이다. 이것을 피하려면, manifest에이 활동을 위해 android : configChanges = "orientation | screenSize"를 추가해야합니다 ... – Pauland

+0

고맙습니다. 내 문제가 해결되었습니다. 답을 확인했습니다. –