gpt4 book ai didi

java - fragment 添加事务后, Activity 中的小部件布局仍然出现

转载 作者:行者123 更新时间:2023-11-29 04:13:02 25 4
gpt4 key购买 nike

我对 fragment 有疑问。我在不删除旧 fragment 的情况下动态添加 fragment ,以便在我返回时可以重新调用它们。但是当我应用以下代码 fragment 时,我发现来自该 Activity 的一些 View 并没有消失,例如 Button、TextView 等。我得到的结果是重叠 View

MainActivity.Java

public class MainActivity extends AppCompatActivity implements BlankFragment.OnFragmentInteractionListener {

private static final String TAG = "MainActivity";

private Unbinder unbinder;

private FragmentManager fragmentManager;

@BindView(R.id.dynamic_fragment_frame)
FrameLayout frameLayout;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_reading);

unbinder = ButterKnife.bind(this);

fragmentManager = this.getSupportFragmentManager();
}

public void openFragment(View view) {

BlankFragment fragment = BlankFragment.newInstance("Test 1");
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.setCustomAnimations(R.anim.enter_from_right, R.anim.exit_to_right, R.anim.enter_from_right, R.anim.exit_to_right);
transaction.addToBackStack(null);
transaction.add(R.id.dynamic_fragment_frame, fragment, "BLACK_FRAGMENT");
transaction.commit();

}

@Override
protected void onDestroy() {
unbinder.unbind();
super.onDestroy();
}


@Override
public void onFragmentInteraction(String text) {
Log.d(TAG, "onFragmentInteraction: " + text);
onBackPressed();
}
}

activity_reading.xml

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#00d9ff"
tools:context=".MainActivity">

<FrameLayout
android:id="@+id/dynamic_fragment_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"/>

<TextView
android:id="@+id/title_read_news"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Ini Judul"
android:textAppearance="@style/TextAppearance.AppCompat.Large"
android:layout_gravity="center"
android:paddingBottom="5dp"/>

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:onClick="openFragment"
android:text="Open"
android:stateListAnimator="@null"/>
</RelativeLayout>

空白 fragment .java

public class BlankFragment extends Fragment {

private static final String ARG_TEXT = "TEXT";

private Unbinder unbinder;

private String mParam1;

private OnFragmentInteractionListener mListener;

@BindView(R.id.tv_blank_fragment)
TextView tvTitle;

@BindView(R.id.back_btn)
Button backBtn;

@BindView(R.id.next_btn)
Button nextBtn;

private FragmentManager fragmentManager;

public BlankFragment() {
// Required empty public constructor
}

public static BlankFragment newInstance(String param1) {
BlankFragment fragment = new BlankFragment();
Bundle args = new Bundle();
args.putString(ARG_TEXT, param1);
fragment.setArguments(args);
return fragment;
}

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);


if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_TEXT);
}

fragmentManager = getFragmentManager();
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_blank, container, false);
}


@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
unbinder = ButterKnife.bind(this, view);

tvTitle.setText(mParam1);
backBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

String test = "test dari fragment";
sendBack(test);
}
});

nextBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {


}
});

}
public void sendBack(String sendback) {
if (mListener != null) {
mListener.onFragmentInteraction(sendback);
}
}

@Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof OnFragmentInteractionListener) {
mListener = (OnFragmentInteractionListener) context;
} else {
throw new RuntimeException(context.toString()
+ " must implement OnFragmentInteractionListener");
}
}

@Override
public void onDetach() {
super.onDetach();
mListener = null;
}
public interface OnFragmentInteractionListener {
void onFragmentInteraction(String text);
}
}

fragment_blank.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#cfcf1d"
tools:context=".BlankFragment">

<TextView
android:id="@+id/tv_blank_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/hello_blank_fragment" />

<Button
android:id="@+id/next_btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/back_btn"
android:layout_alignParentStart="true"
android:text="Create new Fragment" />

<Button
android:id="@+id/back_btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentBottom="true"
android:text="back" />

</RelativeLayout>

一切顺利,但我的 Mainactivity 布局的结果仍然出现在 fragment 上。请帮我结果如下图所示: here 1 here 2

最佳答案

在activity_reading中,首先声明了 fragment 容器 View (dynamic_fragment_frame),所以按钮和 TextView 会显示在它上面。如果将 fragment 容器移动到底部, fragment 将正确显示,因此布局应如下所示:

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#00d9ff"
tools:context=".MainActivity">

<TextView
android:id="@+id/title_read_news"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Ini Judul"
android:textAppearance="@style/TextAppearance.AppCompat.Large"
android:layout_gravity="center"
android:paddingBottom="5dp"/>

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:onClick="openFragment"
android:text="Open"
android:stateListAnimator="@null"/>

<FrameLayout
android:id="@+id/dynamic_fragment_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"/>

</RelativeLayout>

关于java - fragment 添加事务后, Activity 中的小部件布局仍然出现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53927491/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com