android - 在片段内单击按钮更改片段
问题描述
当单击该片段上的按钮时,我试图更改该片段。这样做时,片段会更改,但按钮仍会出现在更改的片段上。我是开发新手,请有人帮我解决这个问题。
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;
}
}
片段主页.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>
新片段.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>
解决方案
您使用 navHostFragment。文档: https ://developer.android.com/guide/navigation/navigation-getting-started
您应该在导航图操作中创建,并执行以下操作:
public void onClick(View v) {
findNavController().navigate(R.id.action...)
}
推荐阅读
- java - 如何实现将结果返回到事件调度线程的方法?
- powershell - Powershell - 显示作为管理员组成员的 ACTIVE 用户
- javascript - 如何在此调用 Post javascript 中正确添加 JWT 承载?
- java - Spring Bean 依赖于非 Spring 类的初始化
- python - 使用 Python 创建文件夹结构
- javascript - Html 无法识别谷歌脚本函数返回值
- angular - typescript: strictpropertyinitialization 抱怨,即使它是在 costructor 方法中初始化的
- mockito - 嘲笑 RestTemplate 总是给空
- json - 如何在 vb.net 中通过动态修复命名元素来反序列化 Json?
- php - php date() 方法显示错误的日期