gpt4 book ai didi

android - 检查邻居 Android fragment 的存在

转载 作者:行者123 更新时间:2023-11-29 22:47:12 24 4
gpt4 key购买 nike

我想检查来自 Android fragment 代码的邻居 fragment 是否存在。我试过了,但两者都显示 false,即使还有另一个 ID 为 R.id.blank2 的 fragment 。

public class BlankFragment extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
...
Fragment f = getActivity().getSupportFragmentManager().findFragmentById(R.id.blank2);
hasNeighbor = f != null;
Log.i("vac", "OnCreateView: " + hasNeighbor);

f = getFragmentManager().findFragmentById(R.id.blank2);
hasNeighbor = f != null;
Log.i("vac", "OnCreateView: " + hasNeighbor);

我正在使用 android.support.v4.app.Fragment。

这是一个布局文件。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<fragment
android:name="com.vac.myapplication.BlankFragment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/blank"/>

<fragment
android:name="com.vac.myapplication.BlankFragment2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/blank2"/>

</LinearLayout>

最佳答案

getFragmentManager() 是正确的,但检查实例化的顺序(布局 xml 中的顺序)。 BlankFragment2 在您尝试查找时尚未创建。

关于android - 检查邻居 Android fragment 的存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58243728/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com