Android片段空白

时间:2017-03-22 08:54:22

标签: android fragment

我有一个包含RelativeLayout和ImageView的片段。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/pagelayout"
android:layout_below="@layout/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="@color/aqua_blue"
tools:context="MainActivityFragment">

<ImageView
    android:id="@+id/pdfImage"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/white"
    android:adjustViewBounds="true"
    android:scaleType="fitStart"/>

我将pdfpage呈现为图像并在onViewCreated的图像视图中显示如下。

 pageLayout = (RelativeLayout)  rootView.findViewById(R.id.pagelayout);
    //Retain view references.
    mImageView = (ImageView) rootView.findViewById(R.id.pdfImage);        

    // Show the first page by default.
    mCurrentPageNum = pdfRenderer.getmCurrentPageNum();
    if (mCurrentPageNum == 0) {
        mCurrentPageNum = 1;
        pdfRenderer.setmCurrentPageNum(mCurrentPageNum);
    }
    showPage(mCurrentPageNum, true, this.activity);

    //get screen size
    DisplayMetrics metrics = context.getResources().getDisplayMetrics();
    scrwidth = metrics.widthPixels;
    scrheight = pdfRenderer.getPDFImage().getHeight(); 

    pageControlsList = pdfRenderer.GetPageControls();
    if (pageControlsList != null) {
        if (pageControlsList.size() > 0) {
            generateControls();
            showPage(mCurrentPageNum, true, this.activity);
        }
    }

当用户在页面上滑动时,我会导航到下一页或上一页。为此,我调用以Mainactivity中的片段编写的以下函数。

public void Readpage(int index, boolean PdfReload, Activity activity)
{
    //context=activity;
    //pageLayout = (RelativeLayout)  rootView.findViewById(R.id.pagelayout);
    //mImageView = (ImageView) rootView.findViewById(R.id.pdfImage);
    showPage(index, true, activity);
    pageControlsList = pdfRenderer.GetPageControls();
    if (pageControlsList != null) {
        generateControls();            
    }
}

如果页面中有任何控件并动态生成,则调用Generatecontrols方法。代码如下(仅给出一个样本控件)。

 public void generateControls() {
    //iterate loop to create each control
        for (Map.Entry<Integer, PageElement> ctrl : pageControlsList.entrySet()) {
            Integer ctrlKey = ctrl.getKey();
            PageElement pageField = ctrl.getValue();
            String name = pageField.getName();
            int type = pageField.getType();


            int pdfw = (int) pdfRenderer.getPage_width();
            int pdfh = (int) pdfRenderer.getPage_height();

            //get dimensions of the control
            int left = (int) pageField.getLeft();
            int top = (int) pageField.getTop();
            int right = (int) pageField.getRight();
            int bottom = (int) pageField.getBottom();
            int width = (int) pageField.getWidth();
            int height = (int) pageField.getHeight();

            //calculate dimensions w.r.t. screen size
            int ctrlW = scrwidth * width / pdfw;
            int ctrlH = scrheight * height / pdfh;
            int ctrlLeft = (int) (scrwidth * left) / pdfw;
            int ctrlTop = (int) (((scrheight * (pdfh - top)) / pdfh));
            int ctrlRight = (int) (scrwidth * right) / pdfw;
            int ctrlBottom = (int) (scrheight * bottom / pdfh);

            //set dimensions of the control with layout parameters
            RelativeLayout.LayoutParams ctrllp = new RelativeLayout.LayoutParams(ctrlW, ctrlH);
            ctrllp.setMargins(ctrlLeft, ctrlTop, 0, 0);


            //generate controls based on the type of control
            if (type == 2) { //checkbox
                CheckBox myChkBox = new CheckBox(context);
                myChkBox.setId(ctrlKey);
                myChkBox.setFocusable(false);
                myChkBox.setEnabled(false);
                if (pageField.ExportValue().contains("Off"))
                    myChkBox.setChecked(false);
                else
                    myChkBox.setChecked(true);

                pageLayout.setLayoutParams(ctrllp);
                pageLayout.addView(myChkBox, ctrllp);
}

即使第一页包含任何控件,它也能正常工作。一旦我滑动,如果任何其他页面包含控件,那么我只能看到一个空白片段。如果没有控制,那么我可以看到一切正常。我尝试了很多方法,但没有一个适合我。请帮忙。

1 个答案:

答案 0 :(得分:0)

经过一些R&amp; D我在代码中发现了问题。只需在主要活动而不是片段中编写代码。渲染活动中的控件但不是片段。

 //to display page controls during appload
    pageControlsList = pdfRenderer.GetPageControls();
    if (pageControlsList != null) {
        if (pageControlsList.size() > 0) {
            generateControls();
            //fillDefaultDocControls();
            mfrg.showPage(pdfRenderer.getmCurrentPageNum(), true, this);
        }
    }

只需生成控件并调用片段即可显示页面。