开发者

How can I retain the particular text of a slide on orientation change?

开发者 https://www.devze.com 2023-03-15 22:46 出处:网络
I have a slideshow in my app and some text associated it with every slide. the text and images are dynamic. How can i retain the text of a particular slide on orientation change so that after orientat

I have a slideshow in my app and some text associated it with every slide. the text and images are dynamic. How can i retain the text of a particular slide on orientation change so that after orientation change the view remains same.I basically want to know the slide number or index on which the orientation was changed.

What i am doing is as follows:

@Override
public Object onRetainNonConfigurationInstance() {

    ArrayList<Object> objList = new ArrayList<Object>();

    Bitmap bitmapList[] = null;
    String data = "";
    try {
        bitmapList = new Bitmap[slides.size()];

        Log.e("ON", "onRetainNonConfigurationInstance() ");
        if (gallery != null) {
            for (int i = 0; i < imgViews.length; i++) {
                LoaderImageView loaderImageView = imgViews[i];
                if (loaderImageView != null) {
                    Bitmap bitmap = loaderImageView.getImageBitmap();
                    data = slides.get(i).getBody();
                    //System.out.println("the body text is: " + data);
                    if (bitmap != null) {
                        bitmapList[i] = new BitmapDrawable(bitmap).getBitmap();
                    }
                }
            }
        }

        objList.add(bitmapList);
        objList.add(isDisplayingText);
        objList.add(data);

    } catch (Exception e) {
        Log.e("Exception ", "LargeSlideShow.onRetain  Message = " + e.toString());
    } catch (Error e) {
        Log.e("Error ", "LargeSlideShow.onRetain   Message = " + e.toString());
    }
    return objList;
}

and in onCreate am doing it this way:

onCreate()
{  ...

ArrayList<Object> obj1 = (ArrayList<Object>) getLastNonConfigurationInstance();

            if (obj1 != null) {

                bitmaps = (Bitmap[]) obj1.get(0);
                boolean isText = (Boolean) obj1.get(1);
      开发者_如何学C          String data = (String) obj1.get(2);

                System.out.println("The Text received in on Create is:  " + data);
                if (isText == true) {
                    int vis = disText.getVisibility();
                    if (vis == View.GONE) {

                        String formattedBody = makeFormattedBody(data);
                        webView.loadData(formattedBody, "webView/html", "utf-8");
                        disText.setVisibility(View.VISIBLE);
                        disText.startAnimation(animShow);
                        isDisplayingText = true;
                    } else if (vis == View.VISIBLE) {
                        disText.startAnimation(animHide);
                        disText.setVisibility(View.GONE);
                        isDisplayingText = false;
                    }

                }
  ... }

where am i missing the shot, please let me know. any help is appreciated.


You need to override the "onConfigurationChanged" function in your activity and enable the activity to handle the change in the manifest.xml:

    <activity android:name=".SlideView"  android:configChanges="orientation"> </activity>

Then you may setUp your View again:

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    setContentView(R.layout.oneSlide);
    setUpView(); // configure the view e.g. add the picture and the text
}
0

精彩评论

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

关注公众号