Remove header from listView

前端 未结 7 2068
执念已碎
执念已碎 2021-02-01 04:58

I\'m having some problems when trying to remove the header from a listView. At first I use addHeaderView() to add it, but when I change to another layo

相关标签:
7条回答
  • 2021-02-01 05:29

    If you are using addHeaderView(), you can't delete your header after that.

    So, don't use addHeaderView(). Rather, create your own adapter that blends your main roster of Views with your header. While my MergeAdapter will not handle your specific case, you can use it to see the concept of blending multiple row sources:

    https://github.com/commonsguy/cwac-merge

    0 讨论(0)
  • 2021-02-01 05:30

    Most people don't like to use AddHeaderView, however I sometimes found it very convenient, to avoid modifying complex adapters, or if the headers are not very related to them.

    With this easy trick you will be able to seamlessly remove/add headers:

    I add an empty LinearLayout with orientation vertical, and height wrap_content, as the only Header View (let mListView be the target listView):

     LinearLayout mCustomHeaders=new LinearLayout(context);
     mCustomHeaders.setOrientation(LinearLayout.VERTICAL);
    
     mListView.addHeaderView(mCustomHeaders);
     mListView.setAdapter (.......)
    

    Thenafter, I can add random stuff, anywhere, to the list as header, even when the list is full:

     mCustomHeaders.add(myHeaderView1);
     mCustomHeaders.add(myHeaderView2);
     mCustomHeaders.add(myHeaderView3);
    

    You can also delete all headers, anytime:

     mCustomHeaders.removeAllViews(); // will erase all headers
    

    You get the idea .... Hope it helps !

    0 讨论(0)
  • 2021-02-01 05:31

    Where drawerLogoView is my headerview, here's what I do:

        drawerLogoView = mInflater.inflate(R.layout.navigation_drawer_custom_layout, null);
        mDrawerList.addHeaderView(drawerLogoView,null,false);                
        LinearLayout layOut = ((LinearLayout)drawerLogoView.findViewById(R.id.NavProfilePreviewLayout));
        layOut.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, 0));
    

    That way, it becomes invisible :D To show it back, you can use this:

        LinearLayout layOut = ((LinearLayout)drawerLogoView.findViewById(R.id.NavProfilePreviewLayout));
        layOut.setLayoutParams(newRelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT));
    
    0 讨论(0)
  • 2021-02-01 05:35

    Try the approach mentioned below..

    Android ListView#addHeaderView and ListView#addFooterView methods are strange: you have to add the header and footer Views before you set the ListView's adapter so the ListView can take the headers and footers into consideration -- you get an exception otherwise. Here we add a ProgressBar (spinner) as the headerView:

    // spinner is a ProgressBar

    listView.addHeaderView(spinner);
    

    We'd like to be able to show and hide that spinner at will, but removing it outright is dangerous because we'd never be able to add it again without destroying the ListView -- remember, we can't addHeaderView after we've it's adapter:

    listView.removeHeaderView(spinner); //dangerous!
    

    So let's hide it! Turns out that's hard, too. Just hiding the spinner view itself results in an empty, but still visible, header area.

    Now try to hide the spinner:

    spinner.setVisibility(View.GONE);
    

    Result: header area still visible with an ugly space:

    The solution is to put the progress bar in a LinearLayout that wraps it's content, and hiding the content. That way the wrapping LinearLayout will collapse when its content is hidden, resulting in a headerView that is technically still present, but 0dip high:

    <LinearLayout 
          xmlns:a="http://schemas.android.com/apk/res/android"
          android:orientation="vertical"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content">
        <!-- simplified -->
          <ProgressBar
            android:id="@+id/spinner"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/> 
      </LinearLayout>
    

    Then, set the layout as the header:

    spinnerLayout = getLayoutInflater().inflate(R.layout.header_view_spinner, null);
    listView.addHeaderView(spinnerLayout);
    

    And when we need to hide it, hide the layout's content, not the layout:

    spinnerLayout.findViewById(R.id.spinner).setVisibility(View.GONE);
    

    Now the header disappears from view. No more ugly space at the top!

    0 讨论(0)
  • 2021-02-01 05:37

    The problem is that you are always creating a new object when you do:

     View headerView = someView
    

    So the new view is not the same as the view already added as listView header, try this:

    View headerView = inflater.inflate(R.layout.adapter_datatable_saleitem_header, null, false);
            headerView.setTag(this.getClass().getSimpleName() + "header");
            if (listView.getHeaderViewsCount() > 0) {
                View oldView = listView.findViewWithTag(this.getClass().getSimpleName() + "header");
                if (oldView != null) {
                    listView.removeHeaderView(oldView);
                }
            }
    
    0 讨论(0)
  • 2021-02-01 05:40

    I encountered this problem in a slightly disguised scenario: the ListView I was dealing with came from PreferenceFragment, and the header represents a PreferenceCategory. So, my freedom of setting up the ListView was severely limited. But there were two approaches (partly inspired by other answers on this page). One was to add a custom layout to my PreferenceCategory (using a class that extends android.preference.PreferenceCategory, see Custom PreferenceCategory Headings). But I found an easier workaround: for the first Preference in this PreferenceCategory, I override onCreateView():

    @Override public View onCreateView(ViewGroup parent) {
        parent.setTop(-parent.getChildAt(0).getTop());
        return super.onCreateView(parent);
    }
    
    0 讨论(0)
提交回复
热议问题