片段添加事务后,“活动”中的窗口小部件布局仍然出现

时间:2018-12-26 04:40:39

标签: java android android-fragments

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

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>

BlankFragment.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布局的结果仍然出现在片段上。请帮我 结果如下图所示: here 1 here 2

2 个答案:

答案 0 :(得分: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>

答案 1 :(得分:1)

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

使用上面的布局,想象TextViewButtonFragmeLayout上方。因此,当您将Fragment添加到FrameLayout时,TextViewButton仍位于FragmeLayout上方。要隐藏这些视图,在添加TextView.setVisibility(View.GONE)

时应使用Button.setVisibility(View.GONE)Fragment