我正在使用ProgressBar
来显示我的应用程序中完成的问题的百分比。
但是,我遇到一个奇怪的问题,当再次调用活动的onCreate
方法(旋转,调整大小..)时,ProgressBar
的进度显示变得很奇怪。
我调试了代码,但是一切都很好,setMax()和setProgress()的值也正确。
首次加载:
调整大小屏幕:
我的onCreate
方法:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_practice_type_list);
LayoutInflater inflater = (LayoutInflater) this.getSystemService(this.LAYOUT_INFLATER_SERVICE);
Bundle b = getIntent().getExtras();
int practiceType = b.getInt("practiceType");
int level = b.getInt("level");
DatabaseAccess databaseAccess = DatabaseAccess.getInstance(this);
databaseAccess.open();
ArrayList<BookInfoByTypeModel> bookInfo = databaseAccess.getBookInfoByType(level, practiceType);
databaseAccess.close();
ScrollView scrollView = (ScrollView)findViewById(R.id.practice_type_layout_scrollview);
LinearLayout topParent = new LinearLayout(this);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
params.gravity = Gravity.CENTER;
topParent.setLayoutParams(params);
topParent.setOrientation(LinearLayout.VERTICAL);
View viewTypeItem;
TextView tvTypeThumbnail;
TextView tvTypeName;
TextView tvTypeCount;
ProgressBar pbTypeProgressPercent;
TextView tvTypeProgressText;
for (int i = 0; i < bookInfo.size(); i++) {
BookInfoByTypeModel book = bookInfo.get(i);
viewTypeItem = inflater.inflate(R.layout.type_item, scrollView, false);
tvTypeThumbnail = (TextView) viewTypeItem.findViewById(R.id.type_thumbnail);
tvTypeName = (TextView) viewTypeItem.findViewById(R.id.type_name);
tvTypeCount = (TextView) viewTypeItem.findViewById(R.id.type_count);
pbTypeProgressPercent = (ProgressBar) viewTypeItem.findViewById(R.id.type_progress_percent);
tvTypeProgressText = (TextView) viewTypeItem.findViewById(R.id.type_progress_text);
switch (book.getType()){
case 0:
tvTypeThumbnail.setText("字");
tvTypeName.setText("漢字・語彙");
break;
case 1:
tvTypeThumbnail.setText("漢");
tvTypeName.setText("漢字");
break;
case 2:
tvTypeThumbnail.setText("語");
tvTypeName.setText("語彙");
break;
case 3:
tvTypeThumbnail.setText("文");
tvTypeName.setText("文法");
break;
case 4:
tvTypeThumbnail.setText("読");
tvTypeName.setText("読解");
break;
case 5:
tvTypeThumbnail.setText("聴");
tvTypeName.setText("聴解");
break;
case 6:
tvTypeThumbnail.setText("総");
tvTypeName.setText("総合");
break;
}
tvTypeCount.setText("問題集数:" + book.getBookCount());
tvTypeProgressText.setText(book.getProgressCount() + "/" + book.getBookCount());
pbTypeProgressPercent.setMax(book.getBookCount());
pbTypeProgressPercent.setProgress(book.getProgressCount());
topParent.addView(viewTypeItem);
}
scrollView.addView(topParent);
}
我在这里做什么错了?
答案 0 :(得分:1)
您正在将具有相同ID的多个视图扩展到布局中。通常,视图会自动恢复其状态,但是在那种情况下,状态会被布局中具有给定ID的“最后一个”视图覆盖,因此所有视图(具有该特定ID)最终都会从中恢复,从而导致保存状态损坏。 / p>
在onRestoreInstanceState
之后调用的(超级)onCreate
方法中正在发生状态恢复,这就是为什么在那里设置的值会丢失的原因。
为防止此行为,请在要添加到布局的视图上禁用setSaveEnabled(false)
的状态保存。