2012-02-29 141 views
8

我需要能夠動態添加並從我的ListView中刪除頁眉和頁腳。動態添加/刪除頁眉和頁腳

所以我用我的頁眉和頁腳初始化我的活動,然後在某些時候我想隱藏它們,之後我需要添加以前的頁眉和頁腳,並保持相同的Adapter

所以我找到了這個解決方案,但它很醜,我真的希望有另一種方式。
基本上,我必須設置一個空適配器才能夠添加標題視圖,然後設置一個空適配器來添加頁腳視圖。爲了完成我設置我的真正的適配器。

編輯:我要補充的是使用visibility屬性(GONE &可見)這裏不是一個解決方案,因爲我的中間過程中的頁眉頁腳&意見一定不能在適配器。

public class TestAdapterHeader extends ListActivity implements OnClickListener { 
     private static String[] items = { "test 1", "test 2", "test 3", "test 4", 
       "test 5", "test 6", "test 7", "test 8", "test 9", "test 10", 
       "test 11", "test 12", "test 13", "test 14", "test 15", "test 16", 
       "test 17", "test 18", "test 19", "test 20" }; 

     private ArrayAdapter mAdapter; 
     private LinearLayout mParentView; 
     private TextView mHeaderView, mFooterView; 

     private boolean mViewsHidden = false; 


     @Override 
     public void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 

      initViews(); 

      mAdapter = new ArrayAdapter<String>(this, 
        android.R.layout.simple_list_item_1, items); 
      setListAdapter(mAdapter); 
     } 


     private void initViews() { 
      // The main layout 
      mParentView = new LinearLayout(this); 
      mParentView.setOrientation(LinearLayout.VERTICAL); 
      mParentView.setBackgroundColor(Color.BLACK); 

      // The button to hide the views 
      Button hideViewsButton = new Button(this); 
      hideViewsButton.setText("Add/Remove views"); 
      hideViewsButton.setOnClickListener(this); 

      // The listview 
      ListView listView = new ListView(this); 
      listView.setId(android.R.id.list); 
      listView.setCacheColorHint(Color.TRANSPARENT); 

      mParentView.addView(hideViewsButton); 
      mParentView.addView(listView); 

      // Set the content view 
      setContentView(mParentView); 

      AbsListView.LayoutParams lp = new AbsListView.LayoutParams(AbsListView.LayoutParams.MATCH_PARENT, 150); 

      mHeaderView = new TextView(this); 
      mHeaderView.setTextColor(Color.WHITE); 
      mHeaderView.setBackgroundColor(Color.BLUE); 
      mHeaderView.setGravity(Gravity.CENTER); 
      mHeaderView.setLayoutParams(lp); 
      mHeaderView.setText("Header"); 

      mFooterView = new TextView(this); 
      mFooterView.setTextColor(Color.WHITE); 
      mFooterView.setBackgroundColor(Color.BLUE); 
      mFooterView.setGravity(Gravity.CENTER); 
      mFooterView.setLayoutParams(lp); 
      mFooterView.setText("Footer"); 


      getListView().addHeaderView(mHeaderView); 
      getListView().addFooterView(mFooterView); 
     } 


     @Override 
     public void onClick(View v) { 
      mViewsHidden = !mViewsHidden; 

      // Remove header & footer views 
      if (mViewsHidden) { 
       getListView().removeHeaderView(mHeaderView); 
       getListView().removeFooterView(mFooterView); 
      } 
      else { 
       // Remove the ListAdapter to be able to add our headerView 
       setListAdapter(null); 
       getListView().addHeaderView(mHeaderView); 

       // Set an empty ListAdapter to be able to add our footerView 
       setListAdapter(new ArrayAdapter<String>(TestAdapterHeader.this, -1)); 
       getListView().addFooterView(mFooterView); 

       // Re set our Adapter 
       setListAdapter(mAdapter); 
      } 

      mParentView.requestLayout(); 

     } 
    } 
+0

你有沒有解決這個問題?我有類似的需求。 – Vadi 2012-04-15 04:16:44

+0

不,我沒有找到比我給出的解決方案更聰明的東西。但至少它有效。 – Chayy 2012-06-15 11:01:15

回答

2

你應該使用view.setVisibility(int visibility) View.GONE,View.VISIBLE或View.INVISIBLE

 .... 
     if (mViewsHidden) { 
      mHeaderView.setVisibility(View.GONE); 
      mFooterView.setVisibility(View.GONE); 
     } 
     .... 
     else { 
      mHeaderView.setVisibility(View.VISIBLE); 
      mFooterView.setVisibility(View.VISIBLE); 
     } 
     .... 
+0

就像我向Maneesh解釋的那樣,使用可見性屬性在這裏不是一個解決方案。 – Chayy 2012-02-29 21:02:41

6

您可以使用下面的代碼做同樣的

// to show the footer view 
footerView.setVisibility(View.VISIBLE); 

// to hide the footer view  
footerView.setVisibility(View.GONE); 
+0

我真的很喜歡簡單地刪除它們。用戶可以使用拖放系統重新排列列表中的項目,就像我們在音樂應用程序中看到的一樣。在這種情況下,頁眉和頁腳不得在適配器中。 – Chayy 2012-02-29 21:01:28

+0

@Chayy你的黑客適用於大多數設備和操作系統,你可能會被卡住。但是,我發現了困難的方式setListAdapter(null); &getListView()。addHeaderView(mHeaderView);不能以可靠的方式在2.3.5上工作(特別是droid 3手機)。因此,即使您不喜歡,設置可見性也許是您的最佳選擇。 – LEO 2013-08-20 12:33:08

+0

不適用於4.4 – 2014-02-20 17:07:34

1

你需要添加和頁腳在setview之前的列表視圖中的標題視圖,之後你可以使用setVisibility來操作

祝你好運;)

0

這似乎是那裏爲數不多的解決方案之一。我只是嘗試了一些非常相似的東西,但它工作正常,但我在同一條船上。對我來說,這感覺就像一個黑客。

還有幾個其它選項,如手動添加/刪除/隱藏的頁眉/頁腳到列表視圖以外的視圖。但是,您可能無法獲得很好的滾動效果,這幾乎是listview標題的要點。

我只是想設置一個以View.GONE代替作爲視圖消失,有一個在ListView控件的頂部的差距。

+4

您不應該爲標題視圖本身設置View.GONE。您應該在所需的內容視圖周圍放置一個包含wrap_content高度的線性佈局。您將此父級佈局設置爲標題。當您想要隱藏或顯示標題時,您可以將內容視圖(而不是父視圖)的可見性設置爲View.GONE。這樣就不會出現間隙。這確實是一個黑客般的解決方案,但它的工作原理:-D – stoilkov 2012-09-03 05:52:50

0

我找到了解決這種情況的方法,儘管大多數人可能不喜歡它。只需在列表視圖上方和下方添加線性佈局即可。現在您可以使用可見性選項來動態添加和刪除視圖。如果您將參數設置爲WRAP_CONTENT,那麼如果沒有孩子,佈局將不佔用空間。

+0

好吧,列表視圖上方的視圖不是標題,它不會隨列表視圖一起滾動。頁腳也是一樣。 – Chayy 2012-10-09 13:00:29

+0

據我所知,ListView的內置標題不會隨列表視圖一起滾動。爲什麼不插入沒有任何信息的標題並在運行時更改其內容/可見性?你說你不能讓他們在那裏,但我不知道爲什麼。 – Slynk 2012-10-09 16:15:03

+0

那麼,我的ListView可以設置在編輯模式下,我可以重新排序和刪除項目。要做這個操作,我需要先刪除頁眉和頁腳,然後讓用戶編輯列表視圖,當他離開編輯模式時,我添加頁眉/頁腳。也許更清晰的方法是適應這種編輯模式來忽略頁眉和頁腳... – Chayy 2012-10-12 10:02:41

8

不要嘗試添加/刪除頁眉/頁腳視圖。

爲了能夠添加/刪除的頁眉和頁腳視圖動態地,簡單地設定適配器的RelativeLayout到頁眉和頁腳前添加。 之後,你可以添加/刪除你想要的RelativeLayouts。

將RelativeLayouts的大小設置爲WRAP_CONTENT。

哈利

+2

沒有設置適配器滾動ListView到最佳?這是非常棒的效果。如何避免這種情況?順便說一句。每個人都應該感謝谷歌這樣美妙的API ...... :( – Piotr 2014-03-31 23:42:50

0

我得到了類似的problem-我不得不動態添加或刪除頁眉和我做了這樣:
- 我刪除從適配器項目和設定adapter=null;
- 對於去除,R emoveHeaderView(mHeaderView);
- 對於添加標題:addHeaderView(mHeaderView);
- 再次創建適配器以及內容

它的工作原理,但沒有動畫,它看起來醜陋

-1

嘗試使用list.addFooterView(footerview)爲頁腳和list.addHeaderView(headerview)用於首標。

0
its work fine for me! 

private void addHeaderList(){ 
     mListView.setAdapter(null); 
     mListView.addHeaderView(headerList); 

    } 
    private void removeHeaderList(){ 
     mListView.removeHeaderView(headerList); 
    } 

    just called call function before setadapter again. 
      addHeaderList(); 
      mListView.setAdapter(mAdapter); 
2

我剛剛有這個問題,繼承人我做了什麼。爲視圖添加標籤並通過標籤找到並移除它。

 final Button btnAddMore = new Button(this); 
     btnAddMore.setTag("footer"); 

     if(myList.getFooterViewsCount() >0) 
     { 
      View v = myList.findViewWithTag("footer"); 
      if(v != null) 
      { 
       myList.removeFooterView(v); 
      }   
     } 
0

它很簡單!

1)提供和ID佈局容器的你頁腳。

Ex。 footer.xml

<?xml version="1.0" encoding="utf-8"?> 
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="match_parent" 
     android:id="@+id/parent_footer_search_list" 
     android:layout_height="fill_parent" 
     android:background="#fff" 
     android:orientation="vertical"> 
     <TextView 

      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 

      android:gravity="center|left" 
      android:lineSpacingExtra="5dp" 

      android:text="Create new list" 


      android:textColor="#666666" 
      android:textSize="16.8sp" 
      android:textStyle="bold" /> 
     </LinearLayout> 

在上面的XML,parent_footer_search_list是你的主容器的id。 現在讓我們回到你的片段/活動,你已經膨脹了這個頁腳。 因此,爲了將其刪除,

LinearLayout parentContainerForFooter = (LinearLayout) footer.findViewById(R.id.parent_footer_search_list); 

**yourview**.removeFooterView(parentContainerForFooter); 

完成!這樣,你正在使用正確的android標準。 不要使用wrap_content,可見性GONE等,他們只是黑客!