この質問が以前に尋ねられたことは知っていますが、これまでに与えられた回答はどれも役に立ちません。
FragmentStatePagerAdapter からのフラグメント (android.support.v4.app.Fragment) が取り込まれたビューページャーがあります。これらのフラグメントの一部には、現在選択されているビューを追跡するなど、方向が変更されたときに保持する必要があるロジックが含まれています。
ただし、問題のデータを onSaveInstanceState に保存しても、savedInstanceState は常に null です。データを静的変数に保存することでこれを解決できます(各フラグメントのインスタンスが1つしかないため、これでうまくいきます)が、これは非常に醜い解決策であることがわかり、これを行う適切な方法が必要です。
これは、回転時に状態を保持しないフラグメントの 1 つです。
public class PriceSelectFragment extends Fragment {
private TableRow mSelected;
private int mSelectedPos = 0;
// newInstance constructor for creating fragment with arguments
public static PriceSelectFragment newInstance() {
PriceSelectFragment fragmentFirst = new PriceSelectFragment();
return fragmentFirst;
}
public PriceSelectFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_price_select, container, false);
TableLayout mTable = (TableLayout)view.findViewById(R.id.price_table);
List<PriceGroup> mPriceGroups = ((MainActivity) getActivity()).getPriceGroups();
int i = 0;
for (final PriceGroup group : mPriceGroups) {
//Create row from layout and access child TextViews
TableRow r = (TableRow)inflater.inflate( R.layout.price_group, mTable, false);
TextView size = (TextView)r.getChildAt(0);
TextView dimension = (TextView)r.getChildAt(1);
TextView weight = (TextView)r.getChildAt(2);
TextView price = (TextView)r.getChildAt(3);
//Populate row with PriceGroup Data
size.setText(group.sizeIndicator);
dimension.setText(String.format("%2.0fx%2.0fx%2.0f", group.length, group.width, group.height));
weight.setText(Float.toString(group.weight));
price.setText(Integer.toString(group.price));
//Alternate background color every other row
if (i % 2 == 0) {
r.setBackgroundDrawable(getResources().getDrawable(R.drawable.price_selector_1));
}
else {
r.setBackgroundDrawable(getResources().getDrawable(R.drawable.price_selector_2));
}
mTable.addView(r); // Add to table
r.setTag(i);
r.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
selectRow((TableRow) v);
}
});
i++;
}
mSelected = (TableRow)view.findViewWithTag(mSelectedPos);
selectRow(mSelected);
return view;
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt("selected", mSelectedPos);
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
if (savedInstanceState != null) {
mSelectedPos = savedInstanceState.getInt("selected");
}
}
private void selectRow(TableRow row) {
if ((int) mSelected.getTag() % 2 == 0) {
mSelected.setBackgroundDrawable(getResources().getDrawable(R.drawable.price_selector_1));
}
else {
mSelected.setBackgroundDrawable(getResources().getDrawable(R.drawable.price_selector_2));
}
mSelected = row;
mSelectedPos = (int) mSelected.getTag();
mSelected.setBackgroundColor(getResources().getColor(R.color.light_blue));
}
}
状態を静的変数に保存せずにこれを解決するにはどうすればよいですか?
編集
すべてのフラグメントはプログラムで作成されているため、ID がないことを指摘する必要があります。それが問題である可能性があることを読みましたが、それを解決する方法もわかりません。
また、私のアプリケーションは次のように構成されています。
- NavigationDrawer を使用した MainActivity
- フラグメント1
- ViewPager
- サブフラグメント1 - サブフラグメント5
- ViewPager
- フラグメント2
- フラグメント3
- フラグメント1
私が問題を抱えている状態のフラグメントは、サブフラグメントです。