2016-07-11 6 views
-1

私はフラグメントの知識が必要です。私がリストビューアクティビティのビューを1つ持っているとします。フラグメントの呼び出しと同じアクティビティのアイテムの詳細を表示する各アイテムをクリックします。私は単にコンテンツを置き換えたいだけです。誰かが私にこれのための例を与えることができますか?Android Fragment to Item View Details

あなたがあなたの onCreateに表示

ようなLinearLayoutや他のウィジェットなど、あなたの意見の一部にフラグメントを置き換えるために、このサンプルコードを使用することができます

+0

これを解決する方法はたくさんあります。あなたは 'FragmentManager'を調べましたか? –

+0

いいえ、私にいくつかのサンプルを提供してください。 –

+0

googleを使ってくださいその断片を使用する方法 –

答えて

0

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>