活动中的小组件布局在片段添加事务后仍显示



我对片段有问题。我动态添加片段而不删除旧片段,以便在我返回时可以调用它们。但是当我应用以下代码片段时,我发现来自活动的某些视图并没有消失,例如按钮、文本视图等。我得到的结果是重叠视图

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>

空白片段.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布局的结果仍然出现在碎片上。请帮助我结果如下所示:这里 1这里 2

在activity_reading中,首先声明片段容器视图 (dynamic_fragment_frame),因此按钮和文本视图将显示在其顶部。如果将片段容器移动到底部,片段将正确显示,因此布局应如下所示:

<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>
<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>

通过上面的布局,想象一下TextViewButton都在FragmeLayout上。所以当你在FrameLayout上加一个Fragment时,TextViewButton仍然在FragmeLayout上。若要隐藏这些视图,应在添加Fragment时使用TextView.setVisibility(View.GONE)Button.setVisibility(View.GONE)

最新更新