保持されているフラグメントでいくつかのビューを動的に作成したい。これは次のようになります。保持されたフラグメント内の動的ビューの作成
public class DemoFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
...
setRetainInstance(true);
}
public void createViews(List<String> objList) {
TableLayout tableLayout = getView.findViewById(R.id.tl);
for (String str: objList) {
TextView textView = new TextView(getContext());
textView.setText(str);
TableRow tableRow = new TableRow(getContext());
tr.addView(textView);
tableLayout.addView(tableRow);
}
}
}
これらの動的に作成されたビューを保持する適切な方法は何ですか?これらのビューはフラグメントのデータメンバーであるべきですか?たとえば、そのような方法で?
public class DemoFragment extends Fragment {
TableLayout tableLayout;
TextView textView;
TableRow tableRow;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
...
setRetainInstance(true);
}
public void createViews(List<String> objList) {
TableLayout tableLayout = getView.findViewById(R.id.tl);
for (String str: objList) {
TextView textView = new TextView(getContext());
textView.setText(str);
tableRow = new TableRow(getContext());
tr.addView(textView);
tableLayout.addView(tableRow);
}
}
}
やデータは、List<String> objList
のように、フラグメントのデータメンバでなければなりませんし、我々はcreateViews
方法でonResume
を呼び出す必要がありますか?例えば:
public class DemoFragment extends Fragment {
List<String> objList;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
...
setRetainInstance(true);
}
@Override
public void onResume() {
super.onResume();
createViews(objList);
}
public void createViews(List<String> objList) {
TableLayout tableLayout = getView.findViewById(R.id.tl);
for (String str: objList) {
TextView textView = new TextView(getContext());
textView.setText(str);
TableRow tableRow = new TableRow(getContext());
tr.addView(textView);
tableLayout.addView(tableRow);
}
}
}
設定が変更された後にこれらのビューを表示するにはどうすればいいですか?