我的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, 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;
}
解决方法:
它失去了状态,因为viewpager默认只保存上一页和下一页(屏幕外页面限制为1).如果滚动到最后一页,则第一页将被销毁.
你可以使用setoffscreenPageLimit(2)增加内存中保存的页数;
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。