我需要片段知识。假设我有一个Listview活动视图,单击我想要调用片段的每个项目并显示相同活动的项目详细信息。我只想简单地替换内容。有人可以举个例子吗?
答案 0 :(得分:0)
您可以使用此示例代码将部分视图中的片段替换为LinearLayout
或其他小部件作为视图
在onCreate
:
myList.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> parent, View view, int position, long i)
{
updateFragment(position);
}
});
和updateFragment
方法:
public void updateFragment(int selectedItem) {
// Getting reference to the FragmentManager
mFragment = null;
fts = getSupportFragmentManager().beginTransaction();
switch (selectedItem) {
case 0:
mFragment = new FragmentOnlineCategories();
break;
}
fts.replace(R.id.categoriesViewFragments, mFragment, "0");
fts.commit();
}
Xml布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#ffffff">
<include layout="@layout/actionbar_top_linearlayout"/>
<LinearLayout
android:id="@+id/categoriesViewFragments"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
/>
</LinearLayout>