-1

我才能從服務器加載圖像使用viewpager,每個片斷應該基於適配器片段的當前位置加載圖像。因此,例如位置0將加載圖像0位置1個負載圖像1等。用於過去兩天我正在與得到正確的片段位置struggeling,在總的I具有從但是我已經添加到代碼我可以看到打印輸出3個片段只有位置0和2,因此圖像在位置1沒有得到正確的位置上,從viewpager

複製的主要問題是如何解決這個問題?我想獲得正確的位置,並基於該正確的圖像。基於幾種解決方案下面是viewpager代碼和適配器代碼,它被修改過數次但他們都不似乎工作

public class LoadCarFullSizeImage extends AppCompatActivity{ 

private ViewPager viewPager; 
protected String imagePath; 


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

    imagePath=getIntent().getStringExtra("imagePath"); 

    viewPager=(ViewPager)findViewById(R.id.vp_carViewPager); 

    CirclePageIndicator titleIndicator = (CirclePageIndicator)findViewById(R.id.titles); 


    PagerAdapter mPagerAdapter = new ScreenSlidePagerAdapter(getSupportFragmentManager(),imagePath); 
    viewPager.setAdapter(mPagerAdapter); 

    titleIndicator.setViewPager(viewPager); 


} 




@Override 
public void onBackPressed() { 
    if (viewPager.getCurrentItem() == 0) { 
     // If the user is currently looking at the first step, allow the system to handle the 
     // Back button. This calls finish() on this activity and pops the back stack. 
     super.onBackPressed(); 
    } else { 
     // Otherwise, select the previous step. 
     viewPager.setCurrentItem(viewPager.getCurrentItem() - 1); 
    } 
} 



private static class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter{ 

    private String imgPath; 

    public ScreenSlidePagerAdapter(android.support.v4.app.FragmentManager fm, String _imagePath) { 
     super(fm); 
     this.imgPath=_imagePath; 
    } 


    @Override 
    public Fragment getItem(int position) { 
     switch (position) { 
      case 0: // Fragment # 0 - This will show FirstFragment 
       return CarImageFragment.newInstance(0, imgPath); 
      case 1: // Fragment # 0 - This will show FirstFragment different title 
       return CarImageFragment.newInstance(1, imgPath); 
      case 2: // Fragment # 1 - This will show SecondFragment 
       return CarImageFragment.newInstance(2, imgPath); 
      default: 
       return null; 
     } 

    } 


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

}

回答

0

由於您使用的指標..你必須getthe電流從中頁...並使用它太... 當使用指針停止使用尋呼機或尋呼機監聽器中當前網頁...任何監聽器應設置爲指標。

titleIndicator.getCurrentItem()==0... 

titleIndicator.setCurrent.....