Android Fragment从Listview中显示项目详细信息

时间:2016-07-11 14:37:19

标签: android fragment

我需要片段知识。假设我有一个Listview活动视图,单击我想要调用片段的每个项目并显示相同活动的项目详细信息。我只想简单地替换内容。有人可以举个例子吗?

1 个答案:

答案 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>