view pager一次加载三个视图,如何获取对可见视图的引用?

时间:2013-05-06 11:06:42

标签: android android-viewpager

    class MyPagerAdaptor extends PagerAdapter
{
    LayoutInflater inflater;

    public MyPagerAdaptor(){
        inflater = getLayoutInflater();
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return mIdList.size();
    }

    @Override
    public Object instantiateItem(ViewGroup viewGroup, int position) {

        // TODO Auto-generated method stub
        /*if(flag)
        {
            mPosToShare = position;
            System.out.println("mPosToShare......: "+mPosToShare);
            flag = false;
        }*/
        System.out.println("instantiateView............."+position);
        View imageLayout = inflater.inflate(R.layout.item_pager_image, viewGroup, false);
        ImageViewTouch imageView = (ImageViewTouch) imageLayout.findViewById(R.id.image);

        try 
        {
            InputStream is = mExpansionFile.getInputStream("worlatlaspronewmaps/"+title[1]+mIdList.get(position)+".jpg");
            //InputStream is1 = mExpansionFile.getInputStream("worlatlaspronewmaps/"+title[1]+mIdList.get(position-1)+".jpg");
            if(is == null)
            {
                is = mExpansionFile.getInputStream("worlatlaspronewmaps/"+title[1]+mIdList.get(position)+".JPG");
            }

            bitmap = BitmapFactory.decodeStream(is,null,bfOptions);
            System.out.println(bitmap);

            imageView.zoomTo(0.5f,50);
            imageView.setImageBitmap(bitmap);

        }  
        catch (IOException e) 
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        viewGroup.addView(imageLayout,0);
        return imageLayout;
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        // TODO Auto-generated method stub
        return (view==object);
    }

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

MyPagerAdaptor类使用位图填充viewpager作为pager,每个视图都包含一个imageview,但是当我得到位图的引用时,它会给出右边或左边位图的引用,这是不可见的。我需要可见的位图参考。

1 个答案:

答案 0 :(得分:0)

objectOfViewPager.getCurrentItem();

返回当前项目,索引为0。

编辑: 您可以像这样获得对视图的引用,

int index = objectOfViewPager.getCurrentItem();
View view = objectOfViewPager.getChildAt(index);

编辑2 :(进一步解释)

由于您只想更改位图,因此您应该执行以下操作:

int index = objectOfViewPager.getCurrentItem();
mIdList.set(index,whateverBitmapyouwant);
objectOfAdapter.notifyDataSetChanged();