Android - 如何在FragmentPagerAdapter中“废弃”片段?

时间:2011-08-26 19:09:13

标签: java android

我有一个使用BitmapFragment(下面)的Android FragmentPagerAdapter,效果很好。现在我正试图动态切换图像。我试过更改ImageView但没有任何变化。我已经尝试使用FragmentTransaction.replace交换它,但我得到一个无效的参数异常。我知道getImageViewAtPage()工作,“位图”是一个有效的位图。

谢谢!

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.getSupportFragmentManager().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();
        }

1 个答案:

答案 0 :(得分:0)

请尝试在mImageView.setImageBitmap()方法中changeImage()而不是使用v = getImageViewAtPage并执行v.setImageBitmap()