简体   繁体   中英

How to change the displayed Bitmap in a ViewPager displaying ImageViews

I use a ViewPager for image sliding. On a menu button click I want to replace the displaced Bitmap with a new processed Bitmap.

I tried to change the ImageView at the Activity. But it changes the first ImageView Bitmap, not the one displayed.

public void replaceImage(Bitmap newBitmap){
    ImageView imageView = (ImageView) findViewById(R.id.image);
    imageView.setBitmap(newBitmap));
}

I think I have to change the picture at the adapter. Does the FragmentPagerAdapter give me the functionality I need?

my code if needed: https://gist.github.com/anonymous/e965a624b54b15a65f2c

The custom PagerAdapter you are using will work fine. You can either change data set, the String[] image array in the adapter, or make changes in the instantiateItem() method. That is where the ImageView is created for each image. Remember to call notifyDataSetChanged() on your adapter when you have made a change, or the ViewPager won't reflect the changes.

I can change the current displayed Image view with this code.

on the instantiateItem() method of the adapter I give every ImageView a id with

imageView.setId(position);

in my setImage() function I can find the imageView with this code and can change the bitmap.

public void setFilterImage(){
    ImageView currentImageView = (ImageView) findViewById(pager.getCurrentItem());
    currentImageView.setImageDrawable( getResources().getDrawable( myNewBitmap ));
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM