Android"正在加载数据..."片段

时间:2017-01-22 19:34:07

标签: android android-viewpager fragment

我正在开发一个应用程序,您可以在其中进行多项选择测试,其主屏幕包含在ViewPager上,并在viewpager的每个页面中显示一个测试问题的显示位置和应用程序工作得很好,但有一些我无法实现的目标:

问题和可能的答案是从sqlite内部应用程序数据库加载的,它们的文本格式化为一些html,并显示三个webview,有时特定页面需要更长时间才能完全呈现,当我说"需要更长的时间"我不是在谈论毫秒,也许在某些情况下,它们可能需要两到三秒才能完全呈现(非常罕见但是发生)所以我想以&#的形式显示消息34;加载数据......"所以用户知道它发生了什么。

我能够创建一个漂亮的闪烁文本TextView,然后我开始在" onCreateView"的开头展示它。并禁用它(使用textView.setText(""))在同一" onCreateView"的最后。方法,其中包括drawPageN()方法,其中页面的所有元素都被渲染,但我无法使其工作,因为闪烁"加载数据..."出现,但立即消失不反映真实的渲染时间,有时页面仍然没有完全渲染,消息已经消失。

我尝试过其他方法,例如" onViewCreated"禁用"加载文字"消息但没有结果。

我想过" onPreExecute"和" onPostExecute"但为此,我必须在" doItOnBackgroung"并且无法在后台线程上更新UI。

好的,我认为我的目标是"在概念"很简单,但我需要有关如何实现工作版本的帮助,让我们说,从哪里开始进度指示器以及将其关闭的位置。

提前致谢。

编辑:

我一直在尝试Soba建议使用带有onPreExecute,doInBackground和onPostExecute的AsyncTask但现在它在尝试给视图充气时抛出一个错误,它特意指向XML中的第一个WebView,所以我粘贴了一些代码和错误消息,以防它有助于更​​多。

    public static class TestSectionFragment extends Fragment { //static

        private static final String ARG_SECTION_NUMBER = "section_number";
        private WebView webViewInstructions, webViewQuestion;
        private LinearLayout ll, llTvQuestionInstructions;
        private int res;

        public TestSectionFragment() {
        }

        class asyncDrawPage extends AsyncTask<String, Integer, View> {

            LayoutInflater inflater;
            ViewGroup container;

            @Override
            protected View doInBackground(String... strConfig) {

                View rootView;
                Bundle arguments = getArguments();
                int pageNumber = arguments.getInt(ARG_SECTION_NUMBER);

                if (strTheme.equals(testMe.theme_vivid)) {
                    rootView = inflater.inflate(R.layout.activity_test_vivid, container, false);
                }else{
                    rootView = inflater.inflate(R.layout.activity_test, container, false);
                }

                String testName = test.getTestName();
                int testCategory = test.getIdCategory();
                int questionsCount = test.getQuestions().size();

                drawPageN(rootView, pageNumber, testCategory, testName, questionsCount, strTheme);

                return rootView;
            }
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

            asyncDrawPage asyncDraw = new asyncDrawPage();
            asyncDraw.inflater = inflater;
            asyncDraw.container = container;

            View rootView = null;

            try {
                rootView = asyncDraw.execute().get();
            } catch (java.lang.InterruptedException e) {
                e.printStackTrace();
            } catch (java.util.concurrent.ExecutionException e) {
                e.printStackTrace();
            }

            return rootView;
        }
}

drawPageN是填充所有视图的方法(TextViews,WebViews等)。

错误信息(顺便说一下,我不得不切掉它):

01-23 10:09:33.133 24469-24469/com.testMe.testMe W/System.err: java.util.concurrent.ExecutionException: android.view.InflateException: Binary XML file line #74: Binary XML file line #74: Error inflating class android.webkit.WebView
01-23 10:09:33.134 24469-24529/com.testMe.testMe E/testMeLogging: [Error] UncaughtExceptionHandler threade = Thread[AsyncTask #3,5,main], error java.lang.RuntimeException: An error occurred while executing doInBackground()
01-23 10:09:33.135 24469-24469/com.testMe.testMe W/System.err:     at java.util.concurrent.FutureTask.report(FutureTask.java:94)
01-23 10:09:33.135 24469-24516/com.testMe.testMe W/System.err:     at java.lang.RuntimeException: An error occurred while executing doInBackground()
01-23 10:09:33.135 24469-24516/com.testMe.testMe W/System.err:     at android.os.AsyncTask$3.done(AsyncTask.java:309)
01-23 10:09:33.135 24469-24516/com.testMe.testMe W/System.err:     at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:354)
01-23 10:09:33.135 24469-24469/com.testMe.testMe W/System.err:     at java.util.concurrent.FutureTask.get(FutureTask.java:164)
01-23 10:09:33.135 24469-24516/com.testMe.testMe W/System.err:     at java.util.concurrent.FutureTask.setException(FutureTask.java:223)
01-23 10:09:33.135 24469-24469/com.testMe.testMe W/System.err:     at android.os.AsyncTask.get(AsyncTask.java:498)
01-23 10:09:33.135 24469-24516/com.testMe.testMe W/System.err:     at java.util.concurrent.FutureTask.run(FutureTask.java:242)
01-23 10:09:33.136 24469-24469/com.testMe.testMe W/System.err:     at com.testMe.testMe.ActivityTest$TestSectionFragment.onCreateView(ActivityTest.java:1062)
01-23 10:09:33.136 24469-24516/com.testMe.testMe W/System.err:     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)
01-23 10:09:33.136 24469-24516/com.testMe.testMe W/System.err:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
01-23 10:09:33.136 24469-24516/com.testMe.testMe W/System.err:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
01-23 10:09:33.136 24469-24516/com.testMe.testMe W/System.err:     at java.lang.Thread.run(Thread.java:818)
01-23 10:09:33.136 24469-24516/com.testMe.testMe W/System.err: Caused by: android.view.InflateException: Binary XML file line #74: Binary XML file line #74: Error inflating class android.webkit.WebView
01-23 10:09:33.136 24469-24516/com.testMe.testMe W/System.err:     at android.view.LayoutInflater.inflate(LayoutInflater.java:539)
01-23 10:09:33.136 24469-24516/com.testMe.testMe W/System.err:     at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
01-23 10:09:33.136 24469-24516/com.testMe.testMe W/System.err:     at com.testMe.testMe.ActivityTest$TestSectionFragment$asyncDrawPage.doInBackground(ActivityTest.java:1009)
01-23 10:09:33.136 24469-24516/com.testMe.testMe W/System.err:     at com.testMe.testMe.ActivityTest$TestSectionFragment$asyncDrawPage.doInBackground(ActivityTest.java:996)
01-23 10:09:33.136 24469-24469/com.testMe.testMe W/System.err:     at android.support.v4.app.Fragment.performCreateView(Fragment.java:2087)
01-23 10:09:33.136 24469-24516/com.testMe.testMe W/System.err:     at android.os.AsyncTask$2.call(AsyncTask.java:295)
01-23 10:09:33.137 24469-24469/com.testMe.testMe W/System.err:     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1113)
01-23 10:09:33.137 24469-24469/com.testMe.testMe W/System.err:     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1295)
01-23 10:09:33.137 24469-24469/com.testMe.testMe W/System.err:     at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:801)
01-23 10:09:33.137 24469-24469/com.testMe.testMe W/System.err:     at android.support.v4.app.FragmentManagerImpl.execSingleAction(FragmentManager.java:1643)
01-23 10:09:33.137 24469-24469/com.testMe.testMe W/System.err:     at android.support.v4.app.BackStackRecord.commitNowAllowingStateLoss(BackStackRecord.java:679)
01-23 10:09:33.137 24469-24516/com.testMe.testMe W/System.err:     at java.util.concurrent.FutureTask.run(FutureTask.java:237)
01-23 10:09:33.137 24469-24516/com.testMe.testMe W/System.err:  ... 4 more
01-23 10:09:33.138 24469-24469/com.testMe.testMe W/System.err: Caused by: android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:143)
01-23 10:09:33.138 24469-24516/com.testMe.testMe W/System.err:  at android.view.InflateException: Binary XML file line #74: Error inflating class android.webkit.WebView
01-23 10:09:33.138 24469-24469/com.testMe.testMe W/System.err: android.support.v4.view.ViewPager.populate(ViewPager.java:1272)
01-23 10:09:33.138 24469-24469/com.testMe.testMe W/System.err:     at android.support.v4.view.ViewPager.populate(ViewPager.java:1120)
01-23 10:09:33.139 24469-24469/com.testMe.testMe W/System.err:     at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1646)
01-23 10:09:33.139 24469-24469/com.testMe.testMe W/System.err:     at android.view.View.measure(View.java:18911)
01-23 10:09:33.139 24469-24469/com.testMe.testMe W/System.err:     at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:715)
01-23 10:09:33.139 24469-24469/com.testMe.testMe W/System.err:     at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:461)
01-23 10:09:33.140 24469-24469/com.testMe.testMe W/System.err:     at android.view.View.measure(View.java:18911)
01-23 10:09:33.140 24469-24469/com.testMe.testMe W/System.err:     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5995)
01-23 10:09:33.141 24469-24469/com.testMe.testMe W/System.err:     at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
01-23 10:09:33.141 24469-24469/com.testMe.testMe W/System.err:     at android.view.View.measure(View.java:18911)
01-23 10:09:33.141 24469-24469/com.testMe.testMe W/System.err:     at android.widget.LinearLayout.measureVertical(LinearLayout.java:901)
01-23 10:09:33.141 24469-24469/com.testMe.testMe W/System.err:     at android.widget.LinearLayout.onMeasure(LinearLayout.java:630)
01-23 10:09:33.142 24469-24469/com.testMe.testMe W/System.err:     at android.view.View.measure(View.java:18911)
01-23 10:09:33.142 24469-24469/com.testMe.testMe W/System.err:     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5995)
01-23 10:09:33.142 24469-24469/com.testMe.testMe W/System.err:     at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
01-23 10:09:33.142 24469-24469/com.testMe.testMe W/System.err:     at com.android.internal.policy.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2662)
01-23 10:09:33.143 24469-24469/com.testMe.testMe W/System.err:     at android.view.View.measure(View.java:18911)
01-23 10:09:33.144 24469-24469/com.testMe.testMe W/System.err:     at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2157)
01-23 10:09:33.146 24469-24469/com.testMe.testMe W/System.err:     at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1261)
01-23 10:09:33.146 24469-24469/com.testMe.testMe W/System.err:     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1498)
01-23 10:09:33.146 24469-24469/com.testMe.testMe W/System.err:     at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1140)
01-23 10:09:33.146 24469-24469/com.testMe.testMe W/System.err:     at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6232)
01-23 10:09:33.147 24469-24469/com.testMe.testMe W/System.err:     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:858)
01-23 10:09:33.147 24469-24469/com.testMe.testMe W/System.err:     at android.view.Choreographer.doCallbacks(Choreographer.java:670)
01-23 10:09:33.147 24469-24469/com.testMe.testMe W/System.err:     at android.view.Choreographer.doFrame(Choreographer.java:606)
01-23 10:09:33.147 24469-24469/com.testMe.testMe W/System.err:     at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:844)
01-23 10:09:33.147 24469-24469/com.testMe.testMe W/System.err:     at android.os.Handler.handleCallback(Handler.java:739)
01-23 10:09:33.147 24469-24469/com.testMe.testMe W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:95)
01-23 10:09:33.147 24469-24469/com.testMe.testMe W/System.err:     at android.os.Looper.loop(Looper.java:148)
01-23 10:09:33.148 24469-24469/com.testMe.testMe W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:5551)
01-23 10:09:33.148 24469-24469/com.testMe.testMe W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
01-23 10:09:33.148 24469-24469/com.testMe.testMe W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:730)...

编辑3:根据建议添加更多方法。

@SuppressLint("SetJavaScriptEnabled")
public void drawPageN(View view, int pageNumber, int testCategory,
                      String testName, int questionsCount, String strTheme) {

    String htmlText;
    Context context = this.getActivity();

    tvFetchingData = (TextView) view.findViewById(R.id.llBody).findViewById(R.id.tvBlinking);
    tvFetchingData.setTextColor(Color.BLACK);
    tvFetchingData.setText("Loading data...");
    GeneralUtils.blinkText(tvFetchingData);

    if (test.getQuestions() != null) {

        Question question = test.getQuestions().get(pageNumber);

        if (question.getInstructions()!=null){
            webViewInstructions = (WebView) view.findViewById(R.id.llBody)
                    .findViewById(R.id.llTvQuestionInstructions)
                    .findViewById(R.id.webViewInstructions);

            llTvQuestionInstructions = (LinearLayout) view.findViewById(R.id.llBody)
                    .findViewById(R.id.llTvQuestionInstructions);

            //webViewInstructions.setWebViewClient(new WebViewClient());
            //webViewInstructions.setWebViewClient(new MYWEBCLIENT());
            webViewInstructions.setWebViewClient(new WebViewClient() {
                @Override
                public boolean shouldOverrideUrlLoading(WebView view, String url) {
                    return false;
                }
                @Override
                public void onPageFinished(WebView view, String url) {
                    if (webViewQuestion.getVisibility() != View.VISIBLE) {
                        webViewQuestion.setVisibility(View.VISIBLE);
                    }
                }
            });
            webViewInstructions.getSettings().setJavaScriptEnabled(true);
            webViewInstructions.getSettings().setDomStorageEnabled(true);
            webViewInstructions.setLayerType(View.LAYER_TYPE_NONE, null);
            webViewInstructions.setBackgroundColor(Color.argb(1, 0, 0, 0));
            //remove copy text
            webViewInstructions.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    return true;
                }
            });
            webViewInstructions.setLongClickable(false);
            webViewInstructions.setTag(true);
            TestUtils.setZoom(webViewInstructions, null, context);

            htmlText = getHtmlText(question.getInstructions());
            webViewInstructions.loadDataWithBaseURL("", htmlText, "text/html", "UTF-8", "");
        } else {
            llTvQuestionInstructions.setVisibility(View.GONE);
        }

        webViewQuestion = (WebView) view.findViewById(R.id.llBody)
                .findViewById(R.id.llTvQuestion)
                .findViewById(R.id.webViewQuestion);

        //webViewQuestion.setWebViewClient(new WebViewClient());
        //webViewQuestion.setWebViewClient(new MYWEBCLIENT());
        webViewQuestion.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                return false;
            }
            @Override
            public void onPageFinished(WebView view, String url) {
                if (webViewQuestion.getVisibility() != View.VISIBLE) {
                    webViewQuestion.setVisibility(View.VISIBLE);
                    tvFetchingData.setText("Data loaded...");
                }
            }
        });
        webViewQuestion.getSettings().setJavaScriptEnabled(true);
        webViewQuestion.getSettings().setDomStorageEnabled(true);
        webViewQuestion.setBackgroundColor(Color.argb(1, 0, 0, 0));
        //remove copy text
        webViewQuestion.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                return true;
            }
        });
        webViewQuestion.setLongClickable(false);
        TestUtils.setZoom(webViewQuestion, null, context);

        //set webview text
        htmlText = getHtmlText(question.getQuestion());
        webViewQuestion.loadDataWithBaseURL("", htmlText, "text/html", "UTF-8", "");

        //ANSWERS

        generatePossibleAnswersCheckBoxes(test.getQuestions().get(pageNumber), view, pageNumber);

        // get the image name according to category, test number and
        // question number
        // example: c1-t1-q15.png
        // String imageName = "c" + String.valueOf(testCategory) + "t"
        // + testName + "q" + String.valueOf(pageNumber + 1);

        String imageName = question.getImage();
        if (imageName!=null) {
            res = getResources().getIdentifier(question.getImage(), "drawable",
                    context.getPackageName());
            if (res != 0) {
                //set the red text indicating there is an image below
                TextView tvImgInstructions = (TextView) view.findViewById(R.id.llBody)
                        .findViewById(R.id.llTvImgInstructions)
                        .findViewById(R.id.tvImgInstructions);

                ApplicationConfiguration testMe = new ApplicationConfiguration();
                String imageInstructionsTextColor = testMe.imageInstructionsTextColor;

                tvImgInstructions.setTextColor(Color.parseColor(imageInstructionsTextColor));
                tvImgInstructions.setText(GeneralUtils.getStringResourceByName(
                        "textview_activitytest_imginstructions", context));
                TestUtils.setZoom(null, tvImgInstructions, context);

                //get dimensions of question image
                Drawable d = getResources().getDrawable(res);
                int w = d.getIntrinsicWidth();
                int h = d.getIntrinsicHeight();
                //set img width and heigth to 90% of screen size to fit in screen
                int screenWidth = GeneralUtils.getScreenWidth(context);
                int imgWidth = (int)(screenWidth * 0.98);
                int wScaleFactor = imgWidth*100/w;
                int imgHeigth = h*wScaleFactor/100;

                ImageView imgQuestion = new ImageView(context);
                imgQuestion.setPadding(0, 50, 0, 0);
                imgQuestion.setId(R.id.imgQuestionX);
                imgQuestion.setTag(imageName);
                LinearLayout.LayoutParams parms = new LinearLayout.LayoutParams(imgWidth, imgHeigth);
                imgQuestion.setLayoutParams(parms);

                //set a border for the image
                imgQuestion.setAdjustViewBounds(true);
                imgQuestion.setScaleType(ScaleType.CENTER_INSIDE);
                imgQuestion.setBackgroundColor(Color.BLACK);
                imgQuestion.setPadding(1,1,1,1);
                imgQuestion.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

                imgQuestion.setOnClickListener(new OnClickListener() {
                    public void onClick(View v) {
                        Context context = v.getContext();
                        String imgTag = (String) v.getTag();
                        Bundle bImageData = new Bundle();
                        bImageData.putString("imgTag", imgTag);
                        bImageData.putString("testName", test.getTestName());
                        Intent intent = new Intent(
                                context,
                                ActivityImgFullScreen.class);
                        intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
                        intent.putExtras(bImageData);
                        startActivity(intent);
                    }
                });
                ll = ((LinearLayout) view.findViewById(R.id.llQuestionImage));
                ll.addView(imgQuestion);
                imgQuestion.setImageResource(res);
            } else {
                //for now used in literature for poems and additional text
                //TODO: make a special field for text
                TextView tvAdditionalText = new TextView(context);
                String additionalText = question.getImage();
                tvAdditionalText.setTextColor(Color.BLACK);
                int screenWidth = GeneralUtils.getScreenWidth(context);
                int screenHeight = GeneralUtils.getScreenHeight(context);
                float textSize = GeneralUtils.getTextSize(additionalText, screenWidth, screenHeight) * 3;
                tvAdditionalText.setTextSize(textSize);
                tvAdditionalText.setText(additionalText);
                ll = ((LinearLayout) view.findViewById(R.id.llQuestionImage));
                ll.addView(tvAdditionalText);
            }
        }
    }

    if (webViewInstructions.getTag().equals(true)){
        webViewInstructions.setPadding(0, 0, 0, 0);
    }

    //add extra space so question image can be scrolled up and seen completely
    //the space added is double the end test linear layout height
    int llHeight=0;
    if (img != null) {
        llHeight = (img.getIntrinsicHeight()); // the heigth of the end test image
    }

    LinearLayout llSpacer = new LinearLayout(context);
    LinearLayout.LayoutParams parmsSpacer = new LinearLayout.LayoutParams(llHeight, llHeight);
    llSpacer.setLayoutParams(parmsSpacer);
    ll.addView(llSpacer);

    String strQuestion = GeneralUtils.getStringResourceByName("activitytest_labelview_question", context);

    String pagerTextNoArrows=strQuestion + " "
            + Integer.toString(pageNumber + 1) + " / "
            + questionsCount;

    if (strTheme.equals(testMe.theme_vivid)) {
        TextView tvQuestionNumber = (TextView) view.findViewById(R.id.tvQuestionNumber);
        tvQuestionNumber.setText(pagerTextNoArrows);
    }else{
        String pagerText = "↑   " + pagerTextNoArrows + "   ↓";

        VerticalLabelView vertical;
        int defFontSize = GeneralUtils.getPrefsByScreenDensity(this.getActivity())[5];
        ll = ((LinearLayout) view.findViewById(R.id.llVerticalPager));
        int paddingLeft = GeneralUtils.dp2px(5, context);
        int paddingBottom = GeneralUtils.dp2px(100, context);
        ll.setPadding(paddingLeft, 0, 0, paddingBottom);
        if (strTheme.equals(testMe.theme_blue)){
            ll.setBackgroundColor(Color.parseColor(testMe.btnStartColor_blue));
        }

        vertical = new VerticalLabelView(context);
        vertical.setTextColor(Color.WHITE);
        vertical.setText(pagerText);
        vertical.setTextSize(GeneralUtils.dp2px(defFontSize, this.getActivity()));

        ll.addView(vertical);
    }
}

@SuppressLint("SetJavaScriptEnabled")
private void generatePossibleAnswersCheckBoxes(final Question question,
                                               View view, final int pageNumber) {

    Context context = view.getContext();
    String htmlText="";
    int i = 0;
    final List<Answer> answers = question.getAnswers();
    nroAnswers = answers.size();

    if (ll != null) {
        ll.removeAllViews();
    }
    if (answers != null) {
        for (Answer rbAnswer : answers) {
            int cbIndex = answers.size()*pageNumber+i;
            // linear layout to hold image checkbox and checkbox text
            LinearLayout llCheckBox = new LinearLayout(context);
            llCheckBox.setOrientation(LinearLayout.HORIZONTAL); // horizontal
            llCheckBox.setGravity(Gravity.CENTER_VERTICAL);
            llCheckBox.setPadding(0, 0, 0, 5);
            //webview for answer
            final WebView webView = new WebView(context);
            webView.setVisibility(View.INVISIBLE);
            //webView.setWebViewClient(new WebViewClient());
            //webView.setWebViewClient(new MYWEBCLIENT());
            webView.setWebViewClient(new WebViewClient() {
                @Override
                public boolean shouldOverrideUrlLoading(WebView view, String url) {
                    return false;
                }
                @Override
                public void onPageFinished(WebView view, String url) {
                    if (webView.getVisibility() != View.VISIBLE) {
                        webView.setVisibility(View.VISIBLE);
                    }
                }
            });
            webView.getSettings().setJavaScriptEnabled(true);
            webView.getSettings().setDomStorageEnabled(true);
            webView.setBackgroundColor(Color.argb(1, 0, 0, 0));
            webView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    return true;
                }
            });
            webView.setLongClickable(false);

            htmlText = getHtmlText(rbAnswer.getAnswer());
            webView.loadDataWithBaseURL("", htmlText, "text/html", "UTF-8", "");
            TestUtils.setZoom(webView, null, context);

            // checkbox i
            cbAnswer[cbIndex] = new ImageView(context);
            cbAnswer[cbIndex].setTag("0@" + rbAnswer.getIdAnswer()+ "@" + pageNumber); // 0=>unchecked|1=>checked
            cbAnswer[cbIndex].setId(cbIndex);

            // determine if checkbox is checked or not according to what
            // is stored in array of already answered questions
            int answeredInCurrentPage = test.getAnsweredQuestions().get(pageNumber).getIdAnswer();
            if (rbAnswer.getIdAnswer() == answeredInCurrentPage) {
                if (voicerecognitionPreference){
                    cbAnswer[cbIndex].setImageResource(R.drawable.checkbox_checked_say);
                }else{
                    cbAnswer[cbIndex].setImageResource(R.drawable.checkbox_checked);
                }
            } else {
                if (voicerecognitionPreference){
                    cbAnswer[cbIndex].setImageResource(R.drawable.checkbox_unchecked_say);
                }else{
                    cbAnswer[cbIndex].setImageResource(R.drawable.checkbox_unchecked);
                }
            }

            int cbPadBottom = 5;
            int cbPadTop = 5;
            int cbPadRight = 15;
            int cbPadLeft = 0;
            cbAnswer[cbIndex].setPadding(cbPadLeft, cbPadTop, cbPadRight, cbPadBottom);

            // add elements to linear layout
            // create and add textview with "tooltip" say nro.
            if (voicerecognitionPreference){
                addTextViewSay(llCheckBox, i+1);
            }

            llCheckBox.addView(cbAnswer[cbIndex]);
            llCheckBox.addView(webView);

            cbAnswer[cbIndex].setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {

                    Context context = v.getContext();

                    //play click sound
                    SoundManager.playSound(context, testMe.getDefaultCheckBoxClickSound(), testMe.getSoundType()[0]);
                    ImageView cbSelected = (ImageView) v;

                    //String strRest = cbSelected.getTag().toString().substring(1);
                    //cbAnswer[cbIndex].setTag("1"+strRest);

                    int cbSelectedId = cbSelected.getId();
                    int cbSelectedTagId = Integer.parseInt(cbSelected.getTag().toString().split("@")[1]);
                    for (int j = 0; j < answers.size(); j++) {
                        int cbIndex = answers.size()*pageNumber+j;
                        boolean isSelected = cbAnswer[j].getTag().toString().substring(0, 1).equals("1");
                        if (cbIndex != cbSelectedId) {
                            // (cbAnswer[j].get
                            // .isChecked()==true &&
                            // j!=cbSelectedId)
                            if (voicerecognitionPreference){
                                cbAnswer[cbIndex].setImageResource(R.drawable.checkbox_unchecked_say);
                            }else{
                                cbAnswer[cbIndex].setImageResource(R.drawable.checkbox_unchecked);
                            }
                            String strRest = cbAnswer[cbIndex].getTag().toString().substring(1);
                            cbAnswer[cbIndex].setTag("0"+strRest);
                        } else {
                            String cbAnswerJTag = cbAnswer[cbIndex].getTag().toString();
                            String cbAnswerJState = cbAnswerJTag.split("@")[0];

                            if (cbAnswerJState.equals("1")) { // 0=>unchecked|1=>checked
                                if (voicerecognitionPreference){
                                    cbAnswer[cbIndex].setImageResource(R.drawable.checkbox_unchecked_say);
                                }else{
                                    cbAnswer[cbIndex].setImageResource(R.drawable.checkbox_unchecked);
                                }
                                cbAnswer[cbIndex].setTag("0@" + cbAnswerJTag.split("@")[1]);
                            } else {
                                if (voicerecognitionPreference){
                                    cbAnswer[cbIndex].setImageResource(R.drawable.checkbox_checked_say);
                                }else{
                                    cbAnswer[cbIndex].setImageResource(R.drawable.checkbox_checked);
                                }
                                cbAnswer[cbIndex].setTag("1@" + cbAnswerJTag.split("@")[1]);
                            }
                        }
                    }
                    test.getAnsweredQuestions().get(pageNumber).setIdAnswer(cbSelectedTagId);
                    test.getAnsweredQuestions().get(pageNumber).setIdQuestion(question.getIdQuestion());
                }
            });
            ll = ((LinearLayout) view.findViewById(R.id.cbLayout));
            ll.addView(llCheckBox);
            i++;
        }
    }
}

3 个答案:

答案 0 :(得分:0)

实际上,在UI线程上调用了AsyncTask的onPreExecute()和onPostExecute方法,因此在那里显示和关闭进度条(或者在你的情况下,闪烁的文本视图)是非常安全的。

@Override
protected void onPreExecute() {
   //Show progressbar
}

@Override
protected Void doInBackground(Void... params) {
   //Fetch data
}

@Override
protected void onPostExecute(Void aVoid) {
   //Dismiss progressbar
    }

答案 1 :(得分:0)

使用getAttachedNetworkStorages的{​​{1}}和onProgressUpdate方法:

  

onProgressUpdate(Progress ...),在调用 publishProgress(Progress ...)后在UI线程上调用。执行的时间是不确定的。此方法用于在后台计算仍在执行时显示用户界面中的任何形式的进度。例如,它可用于为进度条设置动画或在文本字段中显示日志。

Refer This Answer

答案 2 :(得分:0)

当我终于找到了解决方案时,我决定回答我自己的问题,以防它对其他人有用。

经过对drawPageN方法的大量优化(顾名思义渲染我的视图寻呼机的页面)后,我能够成功做到的是下一步:

首先,我重新组织了代码,以便现在使用 onPageFinished 按顺序加载三个webview,然后在SectionFragment的onCreateView中设置“Loading Data ...”,将TextView文本闪烁为on在最后一个WebView onPageFinished 中,我将TextView文本设置为空。

我之前已经尝试过但没有成功,但现在的关键是使用 onPageFinished 按顺序实现WebViews加载。

我希望它可以帮助其他人,并感谢您的所有努力。