Fragment縦画面切り替え
プロジェクトのFragmentは比較的簡単で、タイトルバーに1つのネットワークデータが表示されているRecyclerViewを除いて、Adapter Item layoutは異なるxmlファイルに対応して、それぞれlayout_port(縦画面)、layout_land(横画面)、layout(デフォルト)フォルダの下.しかし、切り替えたところ、Adapter Item layoutは変化していないことが分かった.
切り替え時にonConfigurationChanged()関数がコールバックされるため、この関数で方向を設定し、ページを再描画する必要があります.
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_gift_record, container, false);
view.findViewById(R.id.btn_back).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
gotoExchangeGiftFragment();
}
});
recyclerView = view.findViewById(R.id.record);
final GiftRecordAdapter adapter = new GiftRecordAdapter();
recyclerView.setAdapter(adapter);
layoutManager = new LinearLayoutManager(getActivity());
recyclerView.setLayoutManager(layoutManager);
int mCurrentOrientation = getResources().getConfiguration().orientation;
if ( mCurrentOrientation == Configuration.ORIENTATION_PORTRAIT ){
layoutManager.setOrientation(OrientationHelper.VERTICAL);
}else {
layoutManager.setOrientation(OrientationHelper.HORIZONTAL);
}
TYGameSDKManager.getInstance().giftRecord(new Listener>() {
@Override
public void success(List res) {
Log.d("test", "gift record success");
adapter.setData(res);
}
@Override
public void fail(String reason) {
Log.d("test", "gift record fail");
}
});
return view;
}
切り替え時にonConfigurationChanged()関数がコールバックされるため、この関数で方向を設定し、ページを再描画する必要があります.
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
int mCurrentOrientation = newConfig.orientation;
if ( mCurrentOrientation == Configuration.ORIENTATION_PORTRAIT ){
layoutManager.setOrientation(OrientationHelper.VERTICAL);
}else {
layoutManager.setOrientation(OrientationHelper.HORIZONTAL);
}
recyclerView.removeAllViews();
recyclerView.getAdapter().notifyDataSetChanged();
}