2012-03-07 90 views
0

我正在構建一個作爲大學項目的flashcards應用程序,並且希望水平滾動顯示卡片。我爲閃存卡設計本身構建了一個xml文件:在活動佈局中多次合併/包含xml佈局文件的最佳解決方案

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/relativeLayout1" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" android:background="@drawable/whitenote" android:padding="3dp"> 

    <ImageButton 
     android:id="@+id/imageButtonPins" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentTop="true" 
     android:layout_centerHorizontal="true" android:background="@color/transparent" android:src="@drawable/pinselector"/> 

    <TextView 
     android:id="@+id/textViewWord" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/imageButton1" 
     android:layout_centerHorizontal="true" 
     android:text="Word" android:textColor="@color/black" android:paddingTop="30dp" android:textSize="20dp"/> 

    <TextView 
     android:id="@+id/textViewMeaning" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/textViewWord" 
     android:layout_centerHorizontal="true" 
     android:text="Meaning" android:textColor="@color/black"/> 

</RelativeLayout> 

我也創建了Class文件。

在活動佈局中,我還有幾個元素,因此在根級別上需要一個線性佈局。我已經能夠使用和使用佈局充氣器來顯示單個閃卡用於測試目的。

問題
在這兩個方面,在和佈局吹氣我一直無法獲得的ImageButton工作。我的問題是如何讓按鈕工作。

更新:管理讓ImageButton正在使用。意識到我必須處理活動中的onclick事件,而不是自定義適配器類。這應該讓我獲得的話,只要我可以跟蹤顯示「當前」閃存卡

此外,什麼是最好的方式來處理flashcard應用程序的滾動?我目前的計劃是使用Horizo​​ntalScrollView並對其進行自定義,因爲我需要(a)刷卡使閃卡只能移動到下一個(b)我需要關注「當前」閃存卡,因爲我需要來自其子女觀點的一些數據(即,該詞)。

+0

'ImageButton'不能正常工作嗎?它不可見嗎?它沒有迴應嗎?什麼是錯的,你期望會發生什麼? – kabuko 2012-03-07 23:09:05

+0

@Kabuko:我已經設法讓按鈕開始工作了。它沒有響應點擊事件,我意識到它必須在活動類中處理,而不是在自定義適配器類中處理。 – 2012-03-07 23:20:07

回答

1

你在考慮碎片嗎?

您可以在這裏獲得一些有關ViewPager的幫助。此功能在Android 3.0或更高版本或Android 1.6兼容性軟件包中受支持。

http://geekyouup.blogspot.com/2011/07/viewpager-example-from-paug.html

如果你不希望使用的片段,你可以簡單地使用畫廊。這樣,您可以實現水平滾動。 (就像在亞馬遜應用程序中)沒有複雜的ViewPager。

+0

ViewPager是我已經走了,雖然所有三個建議,我給你正確的答案,因爲你鏈接的例子是非常有用的。謝謝,每個人 – 2012-03-09 22:15:06

1

對於問題的第二部分,請看ViewPager

+0

謝謝,我會立即檢查這個 – 2012-03-07 23:20:21

1

A HorizontalScrollViewGallery可能是最直接的實現方法。我不使用Gallery - 但至少知道它存在是件好事。

如果你想有一個更健壯的實現,我同意dcanh121並認爲你應該檢查出Fragment基於ViewPager。這將允許更多的選擇,而不僅僅是一個視圖,但可能會根據目標矯枉過正。 A fragment基本上是ActivityView的奇怪後代,但不要在此引用我。

此外,

充氣佈局是昂貴的,所以儘量只充氣XML爲View一次,並重新使用該View對象。每次繪製新的flashcard時儘量不要重新填充XML。