开发者

Android - How to "invalidate" a Fragment in a FragmentPagerAdapter?

开发者 https://www.devze.com 2023-03-30 21:20 出处:网络
I have an Android FragmentPagerAdapter that uses BitmapFragment (below), and it works great. Now I\'m trying to switch out the Images on the fly. I\'ve tried changing the ImageView but nothing changes

I have an Android FragmentPagerAdapter that uses BitmapFragment (below), and it works great. Now I'm trying to switch out the Images on the fly. I've tried changing the ImageView but nothing changes. And I've tried using a FragmentTransaction.replace to swap it out, but I get an Invalid Argument exception. I know getImageViewAtPage() works and "bitmap" is a valid Bitmap.

Thanks!

private class BitmapFragment extends Fragment {
    private int mPosition = 0;
    private ImageView mImageView;

    private BitmapFragment(int num){
        super();
        mPosition = num;
    }

    public ImageView getImageView(){
        return mImageView;
    }

    /**
     * The Fragment's UI is just a Bitmap
     */
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_bitmap, container, false);
        try{
            mImageView = (ImageView) v.findViewById(R.id.bitmap);
            mImageView.setImageBitmap(bitmap);   
        }
        catch(Exception e){
            e.printStackTrace();
        }
        return v;
    }
}

        private void changeImage() {
            /*

            BitmapFragment bf = new BitmapFragment(mCurrentPageIndex);
            FragmentTransaction ft = mReaderViewHandler.getSupportFra开发者_运维技巧gmentManager().beginTransaction();
            ft.replace(R.layout.fragment_bitmap, bf);
            ft.commit();
    */
            ImageView v = getImageViewAtPage(mCurrentPageIndex);
            try {
                v.setImageBitmap(bitmap);
            } catch (Exception e) {

                e.printStackTrace();
            }
            v.invalidate();
        }


Please try to do mImageView.setImageBitmap() inside your changeImage() method instead of using v = getImageViewAtPage and doing v.setImageBitmap().

0

精彩评论

暂无评论...
验证码 换一张
取 消