Android 碎片没有出现
我不熟悉碎片。我在mainactivity中有一个Android 碎片没有出现,android,android-fragments,android-architecture-components,Android,Android Fragments,Android Architecture Components,我不熟悉碎片。我在mainactivity中有一个BroadcastReceiver,我从baseadapter调用它,我用这个代码来显示一个片段,但从未出现过 BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { Fragmen
BroadcastReceiver
,我从baseadapter调用它,我用这个代码来显示一个片段,但从未出现过
BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Fragment frag = new NewviewFragment();
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.add(frag, "nav_newview");
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
ft.addToBackStack(null);
ft.commit();
}
};
这是我片段中的代码
package com.example.myapp.ui.newview;
public class NewviewFragment extends Fragment {
public View onCreateView(@NonNull LayoutInflater inflater,
ViewGroup container,Bundle savedInstanceState) {
newviewViewModel =
new ViewModelProvider(this).get(NewviewViewModel.class);
root = inflater.inflate(R.layout.fragment_newview, container, false);
newviewViewModel.getText().observe(getViewLifecycleOwner(), new Observer<String>() {
@Override
public void onChanged(@Nullable String s) {
// textView.setText(s);
}
});
return root;
}
}
package com.example.myapp.ui.newview;
公共类NewviewFragment扩展了片段{
公共视图onCreateView(@NonNull layoutiner充气机,
视图组容器,捆绑包savedInstanceState){
NewViewModel=
新建ViewModelProvider(this.get)(newViewModel.class);
root=充气机。充气(R.layout.fragment\u newview,container,false);
NewViewModel.getText().observe(getViewLifecycleOwner(),new Observer()){
@凌驾
公共void onChanged(@Nullable String s){
//textView.setText;
}
});
返回根;
}
}
这是我的片段_newview.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.newview.NewviewFragment">
<WebView
android:id="@+id/webview1"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
感谢您的帮助 在使用导航体系结构组件库时,不应使用
FragmentTransaction
手动进行片段事务
您只需通过NavController
BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
NavController controller = Navigation.findNavController(MainActivity.this, R.id.nav_host_fragment); // R.id.nav_host_fragment is the id of the placeholder fragment in content_main.xml
controller.navigate(R.id.nav_newview);
}
};
我假设您的主要活动名为MainActivity
&此活动中承载了BroadcastReceiver
更新:
我没有注意到您制作了一个淡入淡出的动画,带有导航组件,以便制作一些动画:
navController.navigate(R.id.secondFragment, null, getNavOptions());
NavOptions getNavOptions() {
return new NavOptions.Builder()
.setEnterAnim(R.anim.fragment_open_enter)
.setExitAnim(R.anim.nav_default_exit_anim)
.setPopEnterAnim(R.anim.nav_default_pop_enter_anim)
.setPopExitAnim(R.anim.nav_default_pop_exit_anim)
.build();
}
并在res/anim
您也可以在mobile_navigation.xml中执行此操作。您是否收到了呼叫?是的,我已经检查了它,它被调用了,我从system.out.println中的片段中得到了响应!在主布局中是否有要承载此片段的占位符?这是什么意思?我是新来的。。。给我举个例子。在mainactivity中有一个导航片段和带有工具栏的布局没有问题!祝你一切顺利@stefanosn请检查更新的答案。。我只是没注意到你用了一些动画