空のLinearLayoutを宣言し、onCreateメソッドで、5回繰り返すループを持つ関数を呼び出して、別のレイアウトを膨らませ、LinearLayoutに追加しました。
private void setupList() {
LayoutInflater layoutInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout itemList = (LinearLayout) findViewById( R.id.itemList );
itemList.removeAllViews();
for ( Category category :: categories ) {
View rowView = layoutInflater.inflate(R.layout.category_row, null);
initializeRow( rowView, category.percentComplete );
itemList.addView( rowView );
}
}
次に、initializeRowメソッドで、膨らませたばかりのビューにあるTextViewとProgressBarを初期化します。
private void initializeRow( final View view, final int percentComplete ) {
TextView topicView = ((TextView) view.findViewById(R.id.topic));
topicView.setText( category.title );
ProgressBar progressBar = (ProgressBar) view.findViewById( R.id.progressBar );
progressBar.setMax( 100 );
progressBar.setProgress( percentComplete );
TextView textView = (TextView) view.findViewById( R.id.progressText );
textView.setText( percentComplete "% Complete" );
}
このアクティビティが初めて作成されると、プログレスバーとテキストビューが適切な値で表示されます。ただし、デバイスを回転させると、TextViewは適切な値で表示されますが、すべてのProgressBarには、最後のProgressBarに対応する進行状況が表示されます。onCreateが最初に呼び出されたときにこれが機能するのに、デバイスが回転した後にonCreateメソッドが呼び出されたときに機能しないのはなぜですか?
すべてのProgressBarが同じIDを持っていることに気付きました。しかし、私のコードでは、膨らませたビューのfindViewByIdを使用して、特定のProgressBarへの参照を取得しています。呼び出して各ProgressBarに一意のIDを与えることで、これを機能させることができました
progressBar.setId( progressBarIds[ position ] );
私のinitializeRowメソッドで。この動作がProgressBarのバグの結果であるのか、それとも私が理解していないlayoutInflatersまたはProgressBarsに関するルールがあるのか興味があります。