在Android 3.0及更高版本上,Android團隊正在努力推動您將片段用於活動。我發現這很有用,但我希望能夠處理我的應用中的點擊事件。我在應用程序的右側使用了一個列表片段,因此在承載片段的活動中發生onclick(或任何點擊偵聽器)。所以我不得不從把一個項目放入XML來使用片段管理器。FragmentManager多個屏幕上的碎片?
設計文件他們表現出這樣的畫面:
http://developer.android.com/training/basics/fragments/fragment-ui.html
我想是的片段A/B的平板電腦界面。然而,在這個頁面中沒有任何地方給你提供了一個這樣做的例子 - 看起來片段管理器一次只能使用一個片段 - 這完全與圖片所描繪的相反。這讓我覺得它在XML中使用...但是,我怎麼會得到一個onclick?這些文件對我來說沒有多大意義。它顯示了一件事,然後說了別的。如果我想刪除平板電腦上的片段A會怎麼樣?添加尚不存在的片段C?如果你試圖使用片段管理器,甚至可能嗎?
我想我不明白如果片段管理器使用多於一個片段,如果是這樣,我該如何使用這個來獲取圖片中的項目,如平板電腦 - 左側(A)是一個listview,右邊是(B)。沒有該片段的ID,我不知道如何訪問它。
不知道這是否是相關的,但這裏是我的一些代碼:
增加了單一的FrameLayout我喜歡在指南中提出
//Activity
FragLaunch launchPane = new FragLaunch();
// In case this activity was started with special instructions from an Intent,
// pass the Intent's extras to the fragment as arguments
// firstFragment.setArguments(getIntent().getExtras());
// Add the fragment to the 'fragment_container' FrameLayout
getFragmentManager().beginTransaction()
.add(R.id.launch_frag_container, launchPane).commit();
}
另外一個片段,在7" 肖像模式平板電腦,我希望它使用一個可滑動的viewpager。當我用XML設計它時,它的工作就像一個魅力,但現在我必須訪問listfragment它不工作(無法訪問,因爲我不能有兩個片段)
FragLaunch內容視圖的XML:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:gravity="center_vertical"
android:orientation="vertical" >
<TextView
android:id="@+id/initial_directions"
style="@style/textScalar.Roboto.Light"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="@string/initial_directions"
android:layout_marginBottom="30dp"
tools:context=".Launch" />
</LinearLayout>
我希望有照片這一項顯示爲片段A:對於片段 FragHistory.java/xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="@+id/spamhistory"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Spam History" />
<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:drawSelectorOnTop="false" />
</LinearLayout>
沒有人對此有什麼見解?
我認爲它被稱爲視圖尋呼機。我現在沒有我的源代碼。它本質上是操作欄選項卡,您可以左右滑動來切換選項卡。這是一個包含多個子視圖的單一視圖。我想知道的是如何使用Fragment管理器同時在屏幕上顯示Frag A和B,因爲他們沒有告訴你如何在任何指南中這樣做。 viewpager工作正常,但我沒有按鈕的工作,因爲我現在只是添加它們。 – Mgamerz
我更新了我的答案,向您展示如何一次將兩個片段添加到屏幕。如果您需要澄清,請讓我知道。 – Don
它的工作原理,仍然需要弄清楚爲什麼當我再次旋轉它時,它不再顯示任何東西。我知道這與交易有關。填充UI的速度也明顯較慢,差不多整整兩秒。在UI線程上做的不多。 – Mgamerz