2012-07-11 36 views
1

我正在嘗試使用4個按鈕在另一個頂部創建一個頁面。如何使用按鈕單擊在Android中展開和隱藏滾動查看

當我點擊一個按鈕時,按鈕應該保持突出顯示,滾動視圖應該在文本下方出現,並按下該按鈕下方的按鈕(如果需要,甚至關閉屏幕)。重新點擊按鈕後,滾動視圖會再次隱藏。

如何讓滾動視圖隱藏並重新出現在按鈕上?

這就是我想要它看起來像:http://img407.imageshack.us/img407/3448/47163794.png

+3

聽起來更像你應該使用ExpandableListView。 – kcoppock 2012-07-11 21:04:21

+0

你不能使用可見性標誌嗎? – Jin 2012-07-11 21:13:37

+0

ExpandableListView可能更符合我的需求,有什麼好的教程或樣本?我很難找到他們 – jacosta 2012-07-11 21:18:38

回答

1

這裏是我有ExpandableListView最近做。這相當簡單。

它是迄今爲止我見過的最大的構造函數之一。


使用ExpandableListView創建佈局。

<LinearLayout> 

    <ExpandableListView> 
     <!-- Set the properties, if you need to see them, let me know. --> 
    </ExpandableListView> 

</LinearLayout> 

現在一些人認爲使用的佈局。

public void onCreate() { 

    ExpandableListView example = findViewById(/* The id of the expandable list view */); 


    example.setAdapter(
      new SimpleExpandableListAdapter(
         /* the context */, 
         /* the map of parents/groups */, 
         /* the layout to map the parents/groups to */, 
         /* the key to search for in the parents/groups map */, 
         /* the id of the textview inside the layout to map the parent/group to */, 
         /* the map of children */, 
         /* the layout to map the children to */, 
         /* the key to search for in the children map */, 
         /* the id of the textview inside the layout to map the children to */) 
      ) 
} 

還有其他提供適配器,但是這是最基礎的。

基本上只是看看developer website。這example也有點幫助,但開發人員的網站走了很長的路。

如果您有任何問題,請告訴我。


跟進

他把createGroupList()。看起來這是他評論的功能 。這是幹什麼的?

他基本上是創造的HashMaps在該功能的List並返回創建的對象。是的,你應該這樣做,它有助於保持你的代碼更清潔。

此外,這些名單顯示真的很小,無論如何,使他們更大?

修改TextView您已顯示您的子女和羣組。在其佈局中向其View添加填充。只需調整dp即可。你可以增加textSize或者你可以添加一個style,就像我在下面展示的那樣。

<TextView 
     android:id="@+id/groupTextView" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     style="@android:style/TextAppearance.Medium" 
     android:paddingLeft="50dp" 
     android:paddingTop="25dp" 
     android:paddingBottom="25dp"/> 

我希望父組是列表,但不希望孩子組 是子列表,我想他們是一個文本框,我可以添加文字(這是我從 搶淨)。我如何製作兒童文本框而不是 列表。

您可能需要爲這種情況創建自己的Adapter。您需要延長BaseExpandableListAdapter。我可能是錯的,可能有一個更簡單的方法......但我不知道一個。


附加跟進

不知道你的意思。進入子列表 的文本出現在以下代碼行中:child.put(「Sub Item」,「test」)。我可以 把我想要的文字放在「測試」,但「測試」出現在 每個父母名單。

您的createChildList需要返回List<ArrayList<HashMap<String, String>>>

createChildList應該是這個樣子:

private List<ArrayList<HashMap<String, String>>> createChildList(int maxValue) { 

       ArrayList<ArrayList<HashMap<String, String>>> groupList = 
           new ArrayList<ArrayList<HashMap<String, String>>>(); 

       for (int i = 0; i <= maxValue; i++) { 
         ArrayList<HashMap<String, String>> childList = 
             new ArrayList<HashMap<String, String>>(); 

         for (int j = 0; j <= maxValue; j++) { 
           HashMap<String, String> map = new HashMap<String, String>(); 

           map.put("Sub Item", "test: " + String.valueOf(j)); 
           childList.add(map); 
         } 

         groupList.add(childList); 
       } 

       return groupList; 
     } 

讓我知道是否可行。

+0

嘿,這真的很有幫助謝謝。有幾件事情,我跟着這個例子及其好的鏈接,但是對於/ *父母/組* /部分的地圖,他放了createGroupList()。看起來這是他評論的一個功能。這是幹什麼的?我應該使用他評論過的功能嗎?對不起,我有點新,在此,我真的很感激幫助 – jacosta 2012-07-12 14:31:21

+0

我想我其實已經想通了。還有兩個問題,我希望父組成爲列表,但不希望子組成爲子列表,我希望它們成爲可以添加文本的文本框(我可以從網上抓取)。我如何製作兒童文本框而不是列表。此外,這些名單顯示真的很小,無論如何,使他們更大?謝謝! – jacosta 2012-07-12 14:40:25

+0

在您的評論中添加了更多關於您的問題的答案。如果有幫助,請不要忘記+1並接受答案。 – prolink007 2012-07-12 15:04:30

相關問題