2016-07-30 161 views
-1
多次

我使用我的活動與我附上一些碎片進入,當第一個片段連接成的片段稱爲多片段呼籲ViewPager

我簡單的代碼來測試簡單ViewPager

import android.os.Bundle; 
import android.support.v4.app.Fragment; 
import android.support.v4.app.FragmentActivity; 
import android.support.v4.app.FragmentManager; 
import android.support.v4.app.FragmentStatePagerAdapter; 
import android.support.v4.view.PagerAdapter; 
import android.support.v4.view.ViewPager; 
import android.util.Log; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.Toast; 


public class MainActivity extends FragmentActivity { 
    private static final int NUM_PAGES = 3; 
    private ViewPager pager; 
    private PagerAdapter adapter; 

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

     // Instantiate Pager & Adapter 
     pager = (ViewPager) findViewById(R.id.viewpager); 
     adapter = new ScreenSlidePagerAdapter(getSupportFragmentManager()); 
     pager.setAdapter(adapter); 
    } 

    @Override 
    public void onBackPressed() { 
     if(pager.getCurrentItem() == 0) { 
      super.onBackPressed(); 
     } 
     else { 
      pager.setCurrentItem(pager.getCurrentItem() -1); 
     } 
    } 

    // PagerAdapter SubClass 
    private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter { 

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

     @Override 
     public Fragment getItem(int position) { 
      Fragment fragment; 
      switch(position) { 
       case 0: 
        fragment = new TutorialPage1(); 
        break; 
       case 1: 
        fragment = new TutorialPage1(); 
        break; 
       case 2: 
        fragment = new TutorialPage1(); 
        break; 
       default: 
        fragment = new TutorialPage1(); 
        break; 
      } 
      return fragment; 
     } 

     @Override 
     public int getItemPosition(Object object) { 
      return POSITION_NONE; 
     } 

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

    } /* EOC */ 

    public static class TutorialPage1 extends Fragment { 

     private static int pageIndex; 
     private static final String TAG = "TAG"; 

     public TutorialPage1() { 
     } 

     public void TutorialPage1(int id) { 
      pageIndex = id; 
      Log.v(TAG, "Page Index is " + pageIndex); // this logs twice 
     } 

     @Override 
     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 

      Log.e(TAG, "Res Index is "); 
      ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.myfragment_layout, container, false); 
      Toast.makeText(getActivity(), "----->", Toast.LENGTH_SHORT).show(); 
      return rootView; 
     } 
    } 
} /* EOC */ 

回答

0

思考:

#1

在你的getItem()調用你有TutorialPage1()帶來呼籲每種情況下...

#2

ViewPagers做喚醒他們兩邊的碎片,並將它們存儲在內存中,以允許片段之間快速切換,所以這可能是你所看到的...但你無法驗證,除非你修復#1

+0

是的,它只是測試,改成其他班後,問題不解決先生 –

+0

哦,我是非常抱歉。請讓我再次測試 –