roomfragmet 加载fragment 切换为FragmentContainerView。

This commit is contained in:
2025-12-09 10:38:26 +08:00
parent 338d704904
commit aed51479a4
2 changed files with 38 additions and 58 deletions

View File

@@ -1,20 +1,15 @@
package com.xscm.modulemain.activity.room.fragment;
import android.Manifest;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.ForegroundColorSpan;
import android.util.Log;
import android.view.View;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
@@ -22,15 +17,13 @@ import androidx.fragment.app.FragmentTransaction;
import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.TimeUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.xscm.modulemain.R;
import com.xscm.modulemain.TransparentActivity;
import com.xscm.modulemain.activity.room.activity.RoomActivity;
import com.xscm.modulemain.databinding.FragmentRoomBinding;
import com.xscm.modulemain.dialog.RoomUserInfoFragment;
import com.xscm.modulemain.dialog.WheatFeedingDialogFragment;
import com.xscm.modulemain.activity.room.contacts.RoomContacts;
import com.xscm.modulemain.activity.room.presenter.RoomPresenter;
import com.xscm.modulemain.databinding.FragmentRoomBinding;
import com.xscm.modulemain.dialog.WheatFeedingDialogFragment;
import com.xscm.moduleutil.base.BaseMvpFragment;
import com.xscm.moduleutil.base.CommonAppContext;
import com.xscm.moduleutil.bean.RedPacketInfo;
@@ -45,10 +38,8 @@ import com.xscm.moduleutil.bean.room.RoomBean;
import com.xscm.moduleutil.bean.room.RoomInfoResp;
import com.xscm.moduleutil.bean.room.RoomOnline;
import com.xscm.moduleutil.bean.room.RoomPitBean;
import com.xscm.moduleutil.dialog.ConfirmDialog;
import com.xscm.moduleutil.enumType.QXRoomSeatViewType;
import com.xscm.moduleutil.enumType.RoomType;
import com.xscm.moduleutil.listener.MessageListenerSingleton;
import com.xscm.moduleutil.rtc.AgoraManager;
import com.xscm.moduleutil.utils.SpUtil;
@@ -56,10 +47,8 @@ import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import java.nio.charset.StandardCharsets;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.TimeUnit;
import io.reactivex.Observable;
@@ -111,11 +100,6 @@ public class RoomFragment extends BaseMvpFragment<RoomPresenter, FragmentRoomBin
private RoomMentorShipFragment mentorShipFragment;//签约
public QXRoomSeatViewType qxRoomSeatViewType;
FragmentManager fragmentManager = null;
FragmentTransaction fragmentTransaction = null;
private void setRoomSeatViewType() {
if (mRoomInfoResp == null || mRoomInfoResp.getRoom_info() == null) {
return;
@@ -149,10 +133,6 @@ public class RoomFragment extends BaseMvpFragment<RoomPresenter, FragmentRoomBin
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
fragmentManager = getChildFragmentManager();
fragmentTransaction = fragmentManager.beginTransaction();
setRoomSeatViewType();
upRoomInfo(mRoomInfoResp);
}
@@ -397,14 +377,12 @@ public class RoomFragment extends BaseMvpFragment<RoomPresenter, FragmentRoomBin
friendshipRoomFragment = FriendshipRoomFragment.newInstance(mRoomInfoResp);
} else friendshipRoomFragment.roomInfoUpdate(mRoomInfoResp);
newFragment = friendshipRoomFragment;
} else if (roomType == RoomType.AUCTION) {
mRoomActivity.isInAuctionTopBtVisible(true);
if (roomAuctionFragment == null)
roomAuctionFragment = RoomAuctionFragment.newInstance(mRoomInfoResp);
else roomAuctionFragment.roomInfoUpdate(mRoomInfoResp);
newFragment = roomAuctionFragment;
} else if (roomType == RoomType.DATING) {
newFragment = switch (labelId) {
case "2" -> {
@@ -434,7 +412,6 @@ public class RoomFragment extends BaseMvpFragment<RoomPresenter, FragmentRoomBin
roomJukeboxFragment = RoomJukeboxFragment.newInstance(mRoomInfoResp);
else roomJukeboxFragment.roomInfoUpdate(mRoomInfoResp);
newFragment = roomJukeboxFragment;
} else if (roomType == RoomType.SIGN_CONTRACT) {
if (mentorShipFragment == null) {
mentorShipFragment = RoomMentorShipFragment.Companion.newInstance(mRoomInfoResp);
@@ -443,7 +420,8 @@ public class RoomFragment extends BaseMvpFragment<RoomPresenter, FragmentRoomBin
}
if (newFragment != null) {
switchFragment(newFragment, mBinding.roomItem.getId(), false);
// switchFragment(newFragment, mBinding.roomItem.getId(), false);
performFragmentReplacement(newFragment);
} else {
LogUtils.e("newFragment==null");
}
@@ -467,7 +445,6 @@ public class RoomFragment extends BaseMvpFragment<RoomPresenter, FragmentRoomBin
}
}
private Fragment currentFragment = null;
private boolean isReplacing = false;
@@ -536,47 +513,50 @@ public class RoomFragment extends BaseMvpFragment<RoomPresenter, FragmentRoomBin
);
return;
}
startActivity(new Intent(ActivityUtils.getTopActivity(), TransparentActivity.class));
}
/**
* 安全替换子 Fragment
*/
private void performFragmentReplacement(@NonNull Fragment newFragment, int containerId) {
if (isReplacing) return;
isReplacing = true;
View container = getView().findViewById(containerId);
if (container == null) {
isReplacing = false;
return;
private void performFragmentReplacement(Fragment newFragment) {
if (getChildFragmentManager().isDestroyed()) {
return; // 避免状态销毁后操作导致崩溃
}
if (!isAdded()) {
isReplacing = false;
FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
// 动画(可选,不想动画可以删掉)
transaction.setCustomAnimations(
android.R.anim.slide_out_right,
android.R.anim.slide_in_left
);
// 第一次添加
if (currentFragment == null) {
transaction.add(mBinding.roomItem.getId(), newFragment, newFragment.getClass().getSimpleName());
transaction.commitAllowingStateLoss();
currentFragment = newFragment;
return;
}
FragmentTransaction ft = getChildFragmentManager().beginTransaction();
// 已添加过,则直接 show
Fragment existed = getChildFragmentManager().findFragmentByTag(newFragment.getClass().getSimpleName());
if (existed != null) {
// 隐藏当前
transaction.hide(currentFragment);
// 1⃣ 如果新 Fragment 没添加过,先 add
if (!newFragment.isAdded()) {
ft.add(containerId, newFragment, newFragment.getClass().getSimpleName());
// 显示已有 fragment
transaction.show(existed);
transaction.commitAllowingStateLoss();
currentFragment = existed;
return;
}
// 2⃣ 显示新 Fragment
ft.show(newFragment);
// 没添加过则 add + hide
transaction.hide(currentFragment);
transaction.add(mBinding.roomItem.getId(), newFragment, newFragment.getClass().getSimpleName());
transaction.commitAllowingStateLoss();
// 3⃣ 隐藏旧 Fragment
if (currentFragment != null && currentFragment != newFragment) {
ft.hide(currentFragment);
}
ft.commitAllowingStateLoss(); // 使用异步提交,避免阻塞 UI
// 4⃣ 更新引用
currentFragment = newFragment;
isReplacing = false;
}
@@ -648,7 +628,7 @@ public class RoomFragment extends BaseMvpFragment<RoomPresenter, FragmentRoomBin
args.putString("roomId", roomId);
args.putInt("displayMode", displayMode);
wheatFeedingDialogFragment.setArguments(args);
wheatFeedingDialogFragment.show(fragmentManager, "WheatFeedingDialogFragment");
wheatFeedingDialogFragment.show(getChildFragmentManager(), "WheatFeedingDialogFragment");
}

View File

@@ -12,7 +12,7 @@
android:clipChildren="false"
android:clipToPadding="false">
<FrameLayout
<androidx.fragment.app.FragmentContainerView
android:id="@+id/room_item"
android:layout_width="match_parent"
android:layout_height="match_parent"