2012-01-05 15 views
0

我有一個已經建立的活動,必須擴展另一個活動,所以我寧願使用commonsware拖放TouchListView類與我現有的活動,而不是擴展ListActivity。但是,我找不到如何做到這一點的例子。在我的活動的XML我已經包括:如何在常規活動中使用commonsware

<com.test.dragdrop.TouchListView 
      xmlns:android="http://schemas.android.com/apk/res/android" 
      xmlns:tlv="http://schemas.android.com/apk/res/com.test.activities" 
      android:id="@android:id/list" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:drawSelectorOnTop="false" 
      tlv:normal_height="64dip" 
      tlv:expanded_height="128dip" 
      tlv:grabber="@+id/icon" 
      tlv:remove_mode="slideRight" 
        android:layout_centerHorizontal="true" 
        android:layout_centerVertical="true" 
        android:layout_below="@id/menu_bar_top_include" 
        android:layout_above="@id/footerbar_lists" 
        android:cacheColorHint="#0000" 
        android:paddingTop="2dip"/> 

現在,我不知道如何翻譯此動態在我的actvities代碼中動態創建它。我假設我必須將代碼放在onCreate()中,然後創建一個RelativeLayout包裝器,並將TouchListView類放入其中,但我不確定何時需要自定義視圖類......任何人都可以這樣做?

注:我也試圖簡單地使用:

TouchListView TLV =(TouchListView)findViewById(R.id.list);

但我得到編譯器錯誤:
---由於某種原因,編譯器無法找到「R.id.list」---這是因爲它在自定義視圖內嗎?有沒有其他方式需要指定?

回答

1

在常規活動中使用TouchListView與在常規活動中使用ListView沒有什麼不同。您只需撥打findViewById()即可獲得ListView並撥打ListView撥打電話setAdapter()將您所需的ListAdapter與它聯繫起來。

+0

感謝回覆。是的,那是我試圖做的,但我得到編譯器錯誤:TouchListView tlv =(TouchListView)findViewById(R.id.list); ---由於某種原因,編譯器找不到「R.id.list」---這是因爲它在自定義視圖內嗎?有沒有其他方式需要指定? – Mike6679 2012-01-05 16:18:39

+0

@Mike:然後更新您的問題並提供編譯器錯誤。 – CommonsWare 2012-01-05 16:20:09

+0

我打開這個任何建議,無論是獲取findViewById()工作或創建一個TouchListView的實例,並動態設置所有參數 - 感謝。 – Mike6679 2012-01-05 22:38:40