私は現在、互いに 2 つのフラグメントを持つアプリに取り組んでいます。下のフラグメントはナビゲーション ホイールであるため、変更されることはありませんが、ユーザーが下のフラグメントのモードを変更すると、上のフラグメントが変化します。私が抱えている問題は、ホイールを使用してモードを変更すると、新しいビューが階層に追加されますが、古いビューは削除されず、新しく追加されたビューはどこにも定義していない FrameLayout にラップされます。
これは、私のアクティビティの onCreate にあるコードです
RandomActivity.java
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.root);
fragmentManager = getSupportFragmentManager();
fragmentTransaction = fragmentManager.beginTransaction();
modeFragment = new LottoModeFragment();
wheelFragment = new WheelFragment();
fragmentTransaction.add(R.id.mode_view, modeFragment);
fragmentTransaction.add(R.id.wheel_view, wheelFragment);
fragmentTransaction.commit();
}
これは、フラグメントを切り替えるために使用するコードです
RandomActivity.java
private void switchFragments(ModeFragment fragment){
fragmentManager = getSupportFragmentManager();
fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.mode_view, fragment);
fragmentTransaction.commit();
}
これは、2 つのフラグメントの両方のプレースホルダーが宣言されているルート ビューです。
root.xml
<org.random.wheel.RootView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/root"
android:contentDescription="@string/root_desc"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="@color/root_background">
<FrameLayout
android:id="@+id/mode_view"
android:contentDescription="@string/mode_view"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<FrameLayout
android:id="@+id/wheel_view"
android:contentDescription="@string/wheel_view"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</org.random.wheel.RootView>
私のフラグメントでは(多くの異なるモードで終了したいのですが、現在は2つしかありません)onCreateViewでこれを行います
LottoModeFragment
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
modeView = (LottoModeView) inflater.inflate(R.layout.lotto_mode, container, false);
return modeView;
}
何が起こっているかをよりよく説明するために、hierachyviewer のこれらのスクリーンショットを見てください。
これは明らかに面倒であり、本来の動作方法ではありません。追加されたビューの親である冗長な FrameLayout を取り除くにはどうすればよいですか? また、Android を追加するだけでなく、実際にあるフラグメントを別のフラグメントに置き換えるにはどうすればよいでしょうか?
同様の問題は、フラグメントが xml で宣言されている場合にのみ発生するようですが、ご覧のとおり、私のものは Java コードで作成されています。たとえば、ここ
また。サポートパッケージを使用しています。
前もって感謝します。
編集
リクエストに応じて ModeView.java を含めました。
ModeView.java
public class ModeView extends RelativeLayout{
private RandomActivity activity;
private AppDisplaySizeInterface appDisplay;
public ModeView(Context context, AttributeSet attrs) {
super(context, attrs);
activity = (RandomActivity) getContext();
}
private void applyRoundedCorners(){
int width = (int) activity.getAppDisplaySizes().getModeWidth();
int height = (int) activity.getAppDisplaySizes().getModeHeight();
Bitmap original = Conversion.drawableToBitmap(this.getBackground(), width, height);
float radius = Float.parseFloat(getResources().getString(R.string.rounded_corner_radius));
RoundedCorners rounded = new RoundedCorners(original, radius, 0);
this.setBackgroundDrawable(rounded);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
appDisplay = activity.getAppDisplaySizes();
super.onMeasure(MeasureSpec.makeMeasureSpec((int) appDisplay.getModeWidth(), MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec((int) appDisplay.getModeHeight(), MeasureSpec.EXACTLY));
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
super.onLayout(changed, l, t, r, b);
applyRoundedCorners();
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
}
}