HackyView寻呼机未显示正确的位置

时间:2015-06-28 16:50:47

标签: android android-viewpager picasso

https://github.com/chrisbanes/PhotoView

我正在使用此库在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;
    }

}

请你帮我找一下为什么这样的工作?

1 个答案:

答案 0 :(得分:0)

最后我自己找到了答案

我们必须为HackyViewPager添加一个监听器(setOnPageChangeListener),而不是在Adapter中添加代码。

 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) {

        }
    });