【问题标题】:Fragment does not show up片段不显示
【发布时间】:2021-04-08 18:11:40
【问题描述】:

我对 Fragment 不熟悉。我的 mainactivity 中有一个 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;
                }
        
        }

这是我的 fragment_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>

任何帮助表示赞赏!

【问题讨论】:

  • onReceive 被调用了吗?
  • 是的,我已经检查过了,它被调用并且我从 system.out.println 中的片段得到响应!
  • 在主布局中是否有一个占位符来承载此片段?
  • 什么意思?我是新手……给我举个例子。有一个导航片段
  • 这个布局在 mainactivity 中有一个工具栏 androidx.appcompat.widget.Toolbar>

标签: android android-fragments android-architecture-components


【解决方案1】:

在使用导航架构组件库时,请勿使用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 中执行此操作

【讨论】:

  • 没问题!希望给你最好的!
  • @stefanosn 请检查更新的答案.. 我只是没有注意到你使用了一些动画
猜你喜欢
  • 2015-10-16
  • 2016-03-02
  • 1970-01-01
  • 1970-01-01
  • 2022-08-18
相关资源
最近更新 更多