【发布时间】: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