简体   繁体   English

如何在方向更改时保留幻灯片的特定文本?

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

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 中,我是这样做的:

onCreate()
{  ...

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

            if (obj1 != null) {

                bitmaps = (Bitmap[]) obj1.get(0);
                boolean isText = (Boolean) obj1.get(1);
                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:您需要在您的活动中覆盖“onConfigurationChanged”function 并使活动能够处理清单中的更改。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
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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