FragmentTransaction将内容添加到错误的LinearLayout

时间:2018-02-08 20:58:32

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

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

这是第1和第2折叠单元的屏幕,展开: Here is a screen of 1st and 2nd folding-cells, unfolded

以下是搜索结果的代码:

             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>
...

是否有人在将折叠片段添加到折叠单元格时有任何问题?如果有人有任何想法,我会非常感激!

1 个答案:

答案 0 :(得分:0)

您的代码中正在解析搜索结果的以下命令会将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();