Android   发布时间:2022-04-28  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了android – 在ViewPager中滑动到新页面会导致旧页面丢失状态大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我的viewpager中有3页.每个页面都有一个按钮,最初设置为一个开始按钮.按下开始按钮后,它将变为暂停按钮.我的问题是,如果我在第一页并单击开始(将其更改为暂停),然后滚动到最后一页并返回到第一页,则按钮会再次开始.它已经失去了被压迫的状态.这只发生在滚动两页之后.如果只滚动一页然后再返回,它不会丢失状态.

public Object instantiateItem(ViewGroup collection,int position) {

        RelativeLayout wholeView = new RelativeLayout(collection.getContext());

            // images
            final ImageView workoutWidget = new ImageView(collection.getContext());
            workoutWidget.setImageResource(R.drawable.workout_widget_master);

            final ImageButton resetButton = new ImageButton(collection.getContext());
            resetButton.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.reset_button_master));
            RelativeLayout.LayoutParams resetButtonParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
            resetButtonParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
            resetButtonParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
            resetButtonParams.height = 150;
            resetButtonParams.width = 150;
            resetButton.setLayoutParams(resetButtonParams);
            resetButton.setVisibility(View.GONE);

            final ImageButton startButton = new ImageButton(collection.getContext());
            startButton.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.start_button_master));
            startButton.setTag(context.getString(R.string.wtrStartButtonTag));
            RelativeLayout.LayoutParams startButtonParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
            startButtonParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
            startButtonParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
            startButtonParams.height = 150;
            startButtonParams.width = 150;
            startButton.setLayoutParams(startButtonParams);
            startButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    startButton.setAlpha(1.0f);

                    if (startButton.getTag() == context.getString(R.string.wtrStartButtonTag)) {
                        startButton.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.pause_button_master));
                        startButton.setTag(context.getString(R.string.wtrPauseButtonTag));
                        resetButton.setVisibility(View.VISIBLE);
                    }
                    else if (startButton.getTag() == context.getString(R.string.wtrPauseButtonTag)) {
                        startButton.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.start_button_master));
                        startButton.setTag(context.getString(R.string.wtrStartButtonTag));
                    }
                }
            });

            // text labels view
            LinearLayout textLabels = new LinearLayout(collection.getContext());
            textLabels.setOrientation(LinearLayout.VERTICAL);

                TextView activityDescription = new TextView(collection.getContext());
                activityDescription.setText("warm-up");
                activityDescription.setPadding(200,200,0);
                activityDescription.setTextSize(30);
                textLabels.addView(activityDescription);

                TextView timeLeftForThisActivity = new TextView(collection.getContext());
                timeLeftForThisActivity.setText("00:00");
                timeLeftForThisActivity.setPadding(200,0);
                timeLeftForThisActivity.setTextSize(60);
                textLabels.addView(timeLeftForThisActivity);

                LinearLayout elapsedLabels = new LinearLayout(collection.getContext());
                elapsedLabels.setOrientation(LinearLayout.HORIZONTAL);

                    TextView elapsedTimeStatic = new TextView(collection.getContext());
                    elapsedTimeStatic.setText("Elapsed Time: ");
                    elapsedTimeStatic.setPadding(200,0);
                    elapsedTimeStatic.setTextSize(20);

                    TextView elapsedTimeDynamic = new TextView(collection.getContext());
                    elapsedTimeDynamic.setText("00:00");
                    elapsedTimeDynamic.setPadding(0,0);
                    elapsedTimeDynamic.setTextSize(20);

                elapsedLabels.addView(elapsedTimeStatic);
                elapsedLabels.addView(elapsedTimeDynamic);

            textLabels.addView(elapsedLabels);

        // adding images and text to overall view
        wholeView.addView(workoutWidget);
        wholeView.addView(startButton);
        wholeView.addView(resetButton);
        wholeView.addView(textLabels);

    collection.addView(wholeView,0);

    return wholeView;
}

解决方法

它失去了状态,因为viewpager认只保存上一页下一页(屏幕外页面限制为1).如果滚动到最后一页,则第一页将被销毁.

你可以使用setOffscreenPageLimit(2)增加内存中保存的页数;

大佬总结

以上是大佬教程为你收集整理的android – 在ViewPager中滑动到新页面会导致旧页面丢失状态全部内容,希望文章能够帮你解决android – 在ViewPager中滑动到新页面会导致旧页面丢失状态所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: