【问题标题】:FragmentTransaction adds content to the wrong LinearLayoutFragmentTransaction 将内容添加到错误的 LinearLayout
【发布时间】:2018-02-08 20:58:31
【问题描述】:

我有一个搜索界面(带有一个 android 搜索小部件)。当我检索结果时,每个结果都被包装成一个folding-cell。加载折叠单元格后,我用一个简单的 TextView 填充单元格标题。然后,我用 TextView 填充内容(有效)。除了 TextView,我的单元格内容还包含一个 LinearLayout。我使用这个布局来添加一个片段,使用一个 FragmentTransaction。
我的问题是每个片段应该填充一个不同的折叠单元,但只有它们都添加到第一个折叠单元(所以我猜 FragmentTransaction 管理器正在将所有片段添加到同一个 LinearLayout,第一个折叠的一个 -单元格)。

这是第一个和第二个折叠单元的屏幕,展开:

这是搜索结果的代码:

             try {
                JSONArray jsonarr = new JSONArray(response);

                for (int i = 0; i < jsonarr.length(); i++) {
                    JSONObject row = jsonarr.getJSONObject(i);
                    Bundle args = new Bundle();
                    args.putString("email", row.getString("email"));
                    args.putString("name", row.getString("nom_organisme"));
                    Fragment newFragment = new FragmentOrganisationFoldingCell();
                    newFragment.setArguments(args);
                    getFragmentManager().beginTransaction().add(R.id.container_organisation_presentation_for_search, newFragment).commit();
                    organisationList.add(newFragment);
                }

                if(jsonarr.length() == 0){
                    errorText.setVisibility(View.VISIBLE);
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }

这是 FramgmentOrganisationFoldingCell:

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_organisation_folding_cell, container, false);

        final FoldingCell fc = (FoldingCell) view.findViewById(R.id.folding_cell);

        Bundle args = getArguments();

        TextView organisationName = view.findViewById(R.id.search_organisation_default_name);
        organisationName.setText(args.getString("name"));
        TextView t = view.findViewById(R.id.cell_content);
        t.setText(args.getString("name"));

        LinearLayout fragContainer = (LinearLayout) view.findViewById(R.id.cell_content_frag);

        getFragmentManager().beginTransaction().replace(fragContainer.getId(), new FragmentProfileOrganisation(), "Organisation" + args.getString("name")).commit();

        fc.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                fc.toggle(false);
            }
        });
        return view;
    }

这里是fragment_organisation_folding_cell,这是我在FragmentOrganisationFoldingCell中膨胀的视图:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/block">
    <com.ramotion.foldingcell.FoldingCell
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/folding_cell"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:clipChildren="false"
        android:clipToPadding="false">
        <!-- CONTENT (UNFOLDED) LAYOUT (MUST BE AT LEAST 2x times BIGGER than content layout bellow)-->
        <include layout="@layout/cell_content_layout" />

        <!-- TITLE (FOLDED) LAYOUT (MUST BE AT LEAST 2x times SMALLER than content layout above) -->
        <include layout="@layout/cell_title_layout" />
    </com.ramotion.foldingcell.FoldingCell>
</LinearLayout>

这是 cell_content_layout 的 XML 代码,其中包含我用来添加片段的 LinearLayout。 (所以我相信这是造成麻烦的文件)。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:visibility="gone">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="60sp"
        android:layout_margin="40dp"
        android:id="@+id/cell_content"/>

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/cell_content_frag">
    </LinearLayout>

</LinearLayout>

这里是 fragment_organisation_search XML 文件:

...
        <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@drawable/block"
            android:id="@+id/container_organisation_presentation_for_search"
            android:layout_margin="5dp">

        </LinearLayout>
...

是否有人也将片段添加到折叠单元中?如果有人有任何想法,我将非常感激!

【问题讨论】:

    标签: android android-layout android-fragments android-linearlayout


    【解决方案1】:

    您的代码中用于解析搜索结果的以下命令会将newFragment 添加到ID 为R.id.container_organisation_presentation_for_search 的视图中。该 ID 在您的 XML 中用于实际片段。

    getFragmentManager().beginTransaction().add(R.id.container_organisation_presentation_for_search, newFragment).commit();
    

    我认为你想要的可能是:

    getFragmentManager().beginTransaction().add(R.id.cell_content_frag, newFragment).commit();
    

    【讨论】:

    • 首先,感谢您帮助我。这是行不通的,因为cell_content_frag 是在newFragment 中膨胀的XML 文件。它不在解析搜索结果的调用父级中。我无法将其添加到父级使用的布局中,因为cell_content_frag 必须是折叠单元格的一部分,并且我不能只将折叠单元格添加到父级的布局中,因为我想动态创建它们。 (这就是我使用FragmentTransaction 的原因)。我想要做的是为每个结果添加一个包装器(折叠单元),我需要它来包装一个片段。它应该包装一个cell_content_frag
    猜你喜欢
    • 2013-08-24
    • 1970-01-01
    • 1970-01-01
    • 2016-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-06
    • 1970-01-01
    相关资源
    最近更新 更多