【问题标题】:How to load a new activity inside a fragment without losing the bottom navbar如何在片段中加载新活动而不会丢失底部导航栏
【发布时间】:2019-05-14 11:55:47
【问题描述】:

正如标题所说,当我单击图像磁贴时在“主页片段”上时,我希望加载新活动,但我希望保持导航栏可见。目前使用下面的代码,活动开始,但导航栏消失。我需要帮助编写代码,以便活动开始但导航栏保持不变。

public class HomeFragment extends Fragment {

private ImageView imageCbt;
private ImageView imageDistorted;

@Nullable
@Override
public android.view.View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    return inflater.inflate(R.layout.fragment_home, container, false);

}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onActivityCreated(savedInstanceState);

    // get the button view
    imageCbt = getView().findViewById(R.id.whatIsCbt);
    imageDistorted = getView().findViewById(R.id.distortedThinking);

    // set a onclick listener for when the button gets clicked
    imageCbt.setOnClickListener(new View.OnClickListener() {
        // Start new list activity
        public void onClick(View v) {
            Intent mainIntent = new Intent(getActivity(),
                    IntroActivity2.class);
            startActivity(mainIntent);
        }
    });

    imageDistorted.setOnClickListener(new View.OnClickListener() {
        // Start new list activity
        public void onClick(View v) {
            Intent mainIntent = new Intent(getActivity(),
                    TwelveTypesDistortedThinkingActivity.class);
            startActivity(mainIntent);



        }
    });

}

【问题讨论】:

  • 也许你不需要开始一个新的活动,而是将当前的片段替换为另一个。然后导航栏不会改变,你想要的内容就会可见。
  • 我是一个完全的初学者,你能告诉我如何做到这一点的代码吗?
  • 请参考片段:developer.android.com/guide/components/fragments 以获得示例代码和更好的理解

标签: java android


【解决方案1】:

要更改当前片段,您可以将点击侦听器中的代码替换为这个(Replacing a fragment with another fragment inside activity group):

Fragment newFragment = new ExampleFragment();
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();

// Replace whatever is in the fragment_container view with this fragment,
// and add the transaction to the back stack if needed
transaction.replace(R.id.fragment_container, newFragment);
transaction.addToBackStack(null);

// Commit the transaction
transaction.commit();

然后您需要将您的活动转换为片段(可能需要进行一些重构)并在 onClickListener 方法中更改当前片段。

PS:当您使用导航栏时,片段是应用程序架构中的最佳实践。

【讨论】:

  • 片段 newFragment = new Fragment(); FragmentTransaction 事务 = getSupportFragmentManager().beginTransaction(); // 用这个片段替换 fragment_container 视图中的任何内容, // 如果需要,将事务添加到后台堆栈 transaction.replace(R.id.fragment_container, newFragment); transaction.addToBackStack(null); // 提交事务 transaction.commit();
  • FragmentTransaction 和 getSupportFragmentManager() 显示为红色,并显示错误消息“无法解析”
  • 如果您在 Fragment 中,您可以在使用 getActivity() 调用 onActivityCreated 后访问该活动。然后 getActivity().getFragmentManager() 将起作用。对于 FragmentTransaction,您可能缺少库或导入。
  • 感谢您的帮助,这很有帮助
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-03-07
  • 2019-07-03
  • 1970-01-01
  • 2022-08-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多