【问题标题】:Swiping to new page in ViewPager causes old page to lose state在 ViewPager 中滑动到新页面会导致旧页面丢失状态
【发布时间】:2013-07-09 16:04:39
【问题描述】:

我的浏览器中有 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, 0);
                activityDescription.setTextSize(30);
                textLabels.addView(activityDescription);

                TextView timeLeftForThisActivity = new TextView(collection.getContext());
                timeLeftForThisActivity.setText("00:00");
                timeLeftForThisActivity.setPadding(200, 0, 0, 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, 0, 0);
                    elapsedTimeStatic.setTextSize(20);

                    TextView elapsedTimeDynamic = new TextView(collection.getContext());
                    elapsedTimeDynamic.setText("00:00");
                    elapsedTimeDynamic.setPadding(0, 0, 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;
}

【问题讨论】:

    标签: android android-viewpager


    【解决方案1】:

    它会丢失状态,因为默认情况下 viewpager 只保存上一页和下一页(屏幕外页面限制为 1)。如果滚动到最后一页,则第一页将被销毁。

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

    【讨论】:

    【解决方案2】:

    Viewpager 根据当前位置工作。当前位置的前一个位置和下一个位置取决于您的 android 应用程序有多少个选项卡或片段。查看寻呼机只保留下一页和上一页的内存。所以你必须增加屏幕外页面限制的数量以适应要刷的屏幕数量。你可以通过添加来做到这一点

      viewPager.setOffscreenPageLimit(2); to your code. 
    

    我希望这有效。提前谢谢你。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-02-09
      • 2017-10-30
      • 2018-11-17
      • 2012-10-25
      • 2020-01-20
      • 1970-01-01
      • 2014-09-20
      • 1970-01-01
      相关资源
      最近更新 更多