5
我正在開發一個Android應用程序,利用更多的主/明細表單的片段。我希望主要活動由左側的列表片段組成,基於左側選擇的項目,我想在右側顯示具有不同佈局的片段。 (注意:右邊的每個片段需要不同的佈局/視圖)如何在Android中交換具有不同佈局的片段?
我遇到的所有示例都只使用右側的一個公共片段,方法是更改其中的某些值或交換/替換新片段具有相同的佈局。
如果有人能夠解釋這個問題,那麼它會幫助我很大。謝謝!
我正在開發一個Android應用程序,利用更多的主/明細表單的片段。我希望主要活動由左側的列表片段組成,基於左側選擇的項目,我想在右側顯示具有不同佈局的片段。 (注意:右邊的每個片段需要不同的佈局/視圖)如何在Android中交換具有不同佈局的片段?
我遇到的所有示例都只使用右側的一個公共片段,方法是更改其中的某些值或交換/替換新片段具有相同的佈局。
如果有人能夠解釋這個問題,那麼它會幫助我很大。謝謝!
如果您使用framelayouts來保存您的碎片,它與您提到的其他碎片相同。你只是實例化你的片段(不管佈局如何),並將它交換到framelayout中而不是另一個。
如果您已將碎片硬編碼到XML中,那麼您將無法做到這一點(就我所能確定的那樣)。
main.xml中
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/frames"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@id/hline1"
android:layout_below="@id/horizontalline"
android:orientation="horizontal" >
<FrameLayout
android:id="@+id/leftpane"
android:layout_width="0px"
android:layout_height="match_parent"
android:layout_weight=".4" />
<TextView
android:id="@+id/verticalline"
android:layout_width="2dp"
android:layout_height="match_parent"
android:background="@color/bar_background"
android:gravity="center_horizontal"
android:paddingLeft="5dip"
android:paddingRight="5dip" />
<FrameLayout
android:id="@+id/rightpane"
android:layout_width="0px"
android:layout_height="match_parent"
android:layout_weight="1" >
</FrameLayout>
</LinearLayout>
然後你使用ID爲的FrameLayout和你實例化片段的名稱,把你的片段到FrameLayout裏。
EventListFragment eventlist = new EventListFragment();
getFragmentManager().beginTransaction().replace(R.id.leftpane, eventlist).commit();
EventDetailFragment eventadd = new EventDetailFragment();
getFragmentManager().beginTransaction().replace(R.id.rightpane, eventadd).commit();
當你要更改的內容,再次做同樣的事情(以下將用新的/不同的片段,它可以有取代片段在右窗格中它與相關自己與衆不同,佈局它):
EventSuperDetailFragment eventsuper = new EventSuperDetailFragment();
getFragmentManager().beginTransaction().replace(R.id.rightpane, eventsuper).commit();
非常感謝您的幫助!片段概念對我來說是相當新的。你能用一個簡單的例子來說明你的觀點嗎? – 2012-08-03 14:09:10
我已經更新了我的答案,希望它能指引您朝着正確的方向發展。讓我知道! – Barak 2012-08-03 14:41:18
謝謝巴拉克!是的,它的工作!此外,我想知道是否有可能在一個片段內創建一個片段。 – 2012-08-07 12:47:37