Я пытаюсь изменить фрагмент, когда на этот фрагмент нажимают кнопку. При этом фрагмент изменяется, но кнопка все еще появляется на измененном фрагменте. Я новичок в разработке, кто-то, пожалуйста, помогите мне решить эту проблему.
HomeFragment. java
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProviders;
import com.vinodsharma.hcms_attendance_app.R;
import com.vinodsharma.hcms_attendance_app.databinding.FragmentHomeBinding;
import com.vinodsharma.hcms_attendance_app.fragments.NewFragment;
public class HomeFragment extends Fragment {
private HomeViewModel homeViewModel;
FragmentHomeBinding binding;
public View onCreateView(@NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
// homeViewModel =
// ViewModelProviders.of(this).get(HomeViewModel.class);
// View root = inflater.inflate(R.layout.fragment_home, container, false);
// final TextView textView = root.findViewById(R.id.text_home);
// homeViewModel.getText().observe(getViewLifecycleOwner(), new Observer<String>() {
// @Override
// public void onChanged(@Nullable String s) {
// textView.setText(s);
// }
// });
// return root;
binding=FragmentHomeBinding.inflate(getLayoutInflater());
return binding.getRoot();
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
binding.btnAttendance.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Fragment NewFragment=new NewFragment();
FragmentManager manager=getActivity().getSupportFragmentManager();
FragmentTransaction fragmentTransaction= manager.beginTransaction();
fragmentTransaction.replace(R.id.nav_host_fragment,NewFragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
});
}
//*****To avoid memory leaks****8
@Override
public void onDestroyView() {
super.onDestroyView();
binding=null;
}
}
frag_home. xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
tools:context=".ui.home.HomeFragment">
<Button
android:id="@+id/btn_goto_new"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:background="#00f"
android:gravity="center"
android:textColor="#ffff"
android:textSize="20sp"
android:text="Go To New Fragment"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
NewFragment. java
public class NewFragment extends Fragment {
public NewFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_new, container, false);
}
}
фрагмент_нов. xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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"
tools:context=".fragments.NewFragment"
>
<!-- TODO: Update blank fragment layout -->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_blank_fragment"
android:layout_gravity="center"
android:textColor="#ffff"
android:textSize="24sp"/>
</FrameLayout>
content_main. xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:showIn="@layout/app_bar_main">
<fragment
android:id="@+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:navGraph="@navigation/mobile_navigation" />
</androidx.constraintlayout.widget.ConstraintLayout>