2009-07-21 73 views
20

我正在尋找創建Accordion-style widget such as on this page的最佳方式。 有沒有使用標準Android工具包實現相同效果的方法,還是我需要構建自定義小部件?如果是這樣 - 如果有的話,你會推薦哪一個?Android - 手風琴部件

+1

奇妙的解釋我有一個很好的解決方案,通過使用視圖類型隱藏和可見。這是它.. http://android-puremvc-ormlite.blogspot.com/2011/07/android-simple-accordion-panel.html – Surojit 2011-07-07 13:49:34

+0

爲什麼不是一個本地部件? – mitsest 2017-09-28 15:45:59

回答

21

我已經在github上推android accordion view項目頂部堆疊ExpandableListView-S。我們將它用於我們的客戶,在2.2,2.3.x,3.2和4.0.3上進行了測試。對我們來說相當不錯。

要在下一步中添加/展開動畫。

這裏是小截圖:

enter image description here

27

而且如果您仍然懷疑 - 這可以用線性佈局內堆疊的一對按鈕/佈局完成。僞代碼如下

<LinearLayout android:orientation="vertical"> 
    <Button android:text="Panel 1"/> 
    <SomeKindOfLayout android:id="@+id/panel1"> 
      <!-- widgets in first panel go here --> 
    </SomeKindOfLayout> 
    <Button android:text="Panel 2"/> 
    <SomeKindOfLayout android:id="@+id/panel2" android:visibility="gone"> 
      <!-- widgets in second panel go here --> 
    </SomeKindOfLayout> 
    <Button android:text="Panel 3"/> 
    <SomeKindOfLayout android:id="@+id/panel3" android:visibility="gone"> 
      <!-- widgets in third panel go here --> 
    </SomeKindOfLayout> 
    <Button android:text="Panel 4"/> 
    <SomeKindOfLayout android:id="@+id/panel4" android:visibility="gone"> 
      <!-- widgets in fourth panel go here --> 
    </SomeKindOfLayout></LinearLayout> 

另一件事可能嘗試上的相互

+15

嘿,那段代碼看起來很熟悉; ;-) – CommonsWare 2009-07-22 10:54:23

+4

我不得不與世界分享:)對於這個記錄 - 這是一個由@commonsguy在maillist上給我的片段(android.developers) – Bostone 2009-07-23 02:44:07

3

我有我自己的風險,並說,@Bostone答案是最合適的。我會在下面給你我的代碼,這樣你就可以看到。感謝GitHub MaciejŁopaciński,但正如@SudoPlz在評論中所說的那樣,根本沒有任何文檔。我不知道從哪裏開始。我試着用Eclipse和Android Studio來做到這一點,我也無法運行該項目來開始瞭解它的工作方式。此外,該項目的最後一次提交大約在1年前,這讓我非常擔心,如果出現問題,我會陷入死衚衕。所以,毫不拖延地,這裏是總部設在@Bostone我的解決方案:

1.-首先,你必須創建一個按鈕和一個佈局嵌套在一個滾動型:

<ScrollView 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" 
     tools:context=".TasksListActivity"> 


      <Button 
       android:id="@+id/magic_btn" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:text="Magic Btn"/> 
      <LinearLayout 
       android:id="@+id/magic_layout" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:orientation="vertical" 
       android:visibility="gone"> 

     </LinearLayout> 
</ScrollView> 

2:在那之後去你的對應JAVA,找到按鈕並設置一個onClickListener,在onClickListener裏面你會發現這個佈局。

Button findMagicBtn = (Button) findViewById(R.id.magic_btn); 
     findMagicBtn.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 

      } 
     }); 

3.-所以現在在onClickListener裏面我們會找到佈局。正如您在第一步所看到的,佈局默認設置爲GONE,但現在我們必須將其設置爲可見。問題是,我怎麼能使它再次消失,反之亦然?因此,我們將添加一個條件用於獲取佈局的知名度,並基於其將被隱藏或顯示:

Button findMagicBtn = (Button) findViewById(R.id.magic_btn); 
     findMagicBtn.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       LinearLayout findMagicLl = (LinearLayout) findViewById(R.id.magic_layout); 
       if (findMagicLl.getVisibility() == View.VISIBLE) { 
        findMagicLl.setVisibility(View.GONE); 
       } else { 
        findMagicLl.setVisibility(View.VISIBLE); 
       } 
      } 
     }); 

這可你想讓對方多次被放完了。

所以,現在如果你真的想讓它看起來像一個手風琴,你可以皮條客按鈕。你可以把一個箭頭在它的右側,使用例如:

findMagicBtn.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.right_arrow, 0); 

你應該做的是,onClickListener內,並使用條件改變箭頭的方向,通過改變繪製(如果消失了,向下箭頭,如果是可見的向上箭頭)。

此外,你可以把它通過添加動畫,這樣看起來更自然:

ScaleAnimation animation = new ScaleAnimation(1f, 1f, 1f, 0f, Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, 0f); 
animation.setDuration(180); 
animation.setFillAfter(true); 
findMagicLl.startAnimation(animation); 

你將不得不考慮的是,以上的動畫必須做,而認爲是可見的。請注意,setFillAfter(true)將永久改變容器的大小,如果你這樣做,也許你不想再使用VIEW.GONE。這個問題有一個關於Scale Animation