2015-06-28 121 views
1

https://github.com/chrisbanes/PhotoViewHackyView尋呼機沒有顯示

我使用這個庫,以顯示viewpager一些照片(HackyViewPager)

它工作正常顯示圖片

但是當我想正確的位置顯示正確的照片的名稱顯示它顯示錯誤的位置名稱...

我想知道這個錯誤

 mViewPager.setAdapter(new SamplePagerAdapter()); 

public class SamplePagerAdapter extends PagerAdapter { 
    final String[][] sDrawables = {{"Chrysanthemum.jpg","0"},{"Desert.jpg","1"},{"Hydrangeas.jpg","2"},{"Jellyfish.jpg","3"}, {"Koala.jpg","4"},{"Lighthouse.jpg","5"},{"Penguins.jpg","6"},{"Tulips.jpg","7"} }; 

    @Override 
    public int getCount() { 
     return sDrawables.length; 
    } 

    @Override 
    public View instantiateItem(ViewGroup container,int position) { 

     PhotoView photoView = new PhotoView(container.getContext()); 

     Log.i("xxx position ",position + " " + sDrawables[position][1]); 

     tvImageName.setText(sDrawables[position][1]); 
     Picasso.with(ViewPagerActivity.this) 
       .load("http://myserver/uploads/" + sDrawables[position][0]) 
       .placeholder(R.drawable.placeholder) 
       .error(R.drawable.error) 
       .into(photoView); 

     container.addView(photoView, LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT); 

     return photoView; 
    } 

    @Override 
    public void destroyItem(ViewGroup container, int position, Object object) { 
     container.removeView((View) object); 
    } 

    @Override 
    public boolean isViewFromObject(View view, Object object) { 
     return view == object; 
    } 

} 

你能幫我找到爲什麼這個工作是這樣嗎?

回答

0

終於讓我找到自己的答案

我們要添加一個監聽器(setOnPageChangeListener)爲HackyViewPager而不是添加適配器內部代碼。

mViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() { 
     @Override 
     public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { 

     } 

     @Override 
     public void onPageSelected(int position) { 
      Log.i("xxx position ",position + " " + sDrawables[position][1]); 
      tvImageName.setText(sDrawables[position][1]); 
     } 

     @Override 
     public void onPageScrollStateChanged(int state) { 

     } 
    });