ListView Fixed Head Label

Source: Internet
Author: User

ListView Fixed Head Label

Due to the recent need for this listview fixed head

It turns out that the ListView adds a Headview head and then determines whether the hidden button is displayed depending on the distance of the slide.

The next step is to show the code, first the layout.

Main layout

 <?xml version= "1.0" encoding= "Utf-8"? ><framelayout 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= ".        Mainactivity "> <linearlayout android:id=" @+id/contentss "android:layout_width=" Wrap_content " android:layout_height= "Wrap_content" > <listview android:id= "@+id/main_list_view" Andro Id:layout_width= "Match_parent" android:layout_height= "match_parent" android:background= "@color/mater        ial_deep_teal_200 "/> </LinearLayout> <linearlayout android:id=" @+id/main_section_b_outside "        Android:layout_width= "Match_parent" android:layout_height= "wrap_content" android:visibility= "Gone" > <include layout= "@layout/main_section_b" android:layout_width= "Match_parent" Androidoid:layout_height= "Wrap_content"/> </LinearLayout></FrameLayout> 

The second layout is Headview.

    <?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical"    android:layout_width="match_parent"    android:layout_height="match_parent">        <TextView            android:background="@color/material_blue_grey_800"            android:clickable="true"            android:id="@+id/abc"            android:layout_width="match_parent"            android:layout_height="300dp"            android:textSize="20dp"            android:text="hi"/>    <include        android:id="@+id/main_section_b_inside"        android:layout_width="match_parent"        android:layout_height="wrap_content"        layout="@layout/main_section_b"/></LinearLayout>

The third layout is the button.

        <?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="horizontal"    android:layout_width="match_parent"    android:layout_height="match_parent">    <Button        android:layout_weight="1"        android:id="@+id/first"        android:text="diyige"        android:layout_width="wrap_content"        android:layout_height="100dp" />    <Button        android:layout_weight="1"        android:text="dierge"        android:id="@+id/two"        android:layout_width="wrap_content"        android:layout_height="100dp" /></LinearLayout>

The next is writing logic.

        public class MainActivity2 extends Activity implements view.onclicklistener{private ListView listview;        Private LinearLayout sectionb;        private int aheight;        Private View Headerview;        Private Fragmentmanager fr;        Private Act1 Act1;        Private Act2 Act2;    Private Boolean isfrist = true;            @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);            Setcontentview (R.layout.activity_main);            SECTIONB = (linearlayout) Findviewbyid (r.id.main_section_b_outside);            Headerview = Layoutinflater.from (this). Inflate (R.layout.main_header,null);            Button first = (Button) Findviewbyid (R.id.first);            button (button) Findviewbyid (r.id.two);            First.setonclicklistener (this);            Two.setonclicklistener (this);            FR = Getfragmentmanager ();            Act1 = new Act1 ();            Act2 = new Act2 (); InitlistvIew (Headerview);            } private void Initlistview (View headerview) {listview = (ListView) Findviewbyid (R.id.main_list_view);            Listview.addheaderview (Headerview, NULL, TRUE); arrayadapter<string> adapter = new Arrayadapter<> (this, Android.            R.layout.simple_expandable_list_item_1);            for (int i = 0; i<100; i++) {adapter.add ("item" +string.valueof (i));            } listview.setadapter (adapter); Listview.setonscrolllistener (New Abslistview.onscrolllistener () {@Override public void ONSC  Rollstatechanged (abslistview view, int scrollstate) {} @Override public void Onscroll (abslistview view, int firstvisibleitem, int visibleitemcount, int totalitemcount) {if (Isfri                        ST) {if (listview.getchildat (0) = = null) {isfrist = true; } else {getmyselfheight (listview.getchildat (0));                        Isfrist = false; }} if (Getscrolly () >= aheight) {if (Sectionb.getvisibil                        ity () = = View.gone) {sectionb.setvisibility (view.visible); }} else if (getscrolly () < Aheight) {if (sectionb.getvisibility () = = VIEW.V                        isible) {sectionb.setvisibility (view.gone);        }                    }                }            });        }//Get Head's height public void getmyselfheight (view v) {View VX = V.findviewbyid (r.id.main_section_b_inside);    Aheight = V.gettop () +v.getheight ()-vx.getheight ();            }//get scrolling distance public int getscrolly () {View c = listview.getchildat (0);            if (c = = null) {return 0;        }    int firstvisibleposition = Listview.getfirstvisibleposition ();            int top = C.gettop ();            int headerheight = 0;            LOG.D ("Test", top+ "top");            LOG.D ("Test",-top + c.getheight () + headerheight+ "");            LOG.D ("Test", Top + "top" +aheight+ "seleteheight" + "Headerheight" +headerheight);            if (firstvisibleposition >= 1) {headerheight = Listview.getheight ();        } Return-top + firstvisibleposition*c.getheight () + headerheight; } @Override public void OnClick (View v) {switch (V.getid ()) {R.id.first:arra yadapter<string> adapter = new Arrayadapter<> (this, Android.                R.layout.simple_expandable_list_item_1);                for (int i = 0; i<100; i++) {adapter.add ("Itemact1" +string.valueof (i));                } adapter.notifydatasetchanged ();                Listview.setadapter (adapter);    Break        Case r.id.two:arrayadapter<string> Adapter1 = new Arrayadapter<> (this, Android.                R.layout.simple_expandable_list_item_1);                for (int i = 0; i<100; i++) {adapter1.add ("Itemact2" +string.valueof (i));                } adapter1.notifydatasetchanged ();                Listview.setadapter (Adapter1);        Break        }} @Override protected void OnStart () {TextView TV = (TextView) Findviewbyid (R.ID.ABC);        Button first = (Button) Findviewbyid (R.id.first);        button (button) Findviewbyid (r.id.two);        Tv.setonclicklistener (this);        First.setonclicklistener (this);        Two.setonclicklistener (this);    Super.onstart (); }}

It's a little messy, but it's going to be.

Here are a few key points that the ListView getscrolly is an empty method that requires us to write our own logic and get the current sliding distance.

Read some information on the Internet, only to know that the ListView does not provide any method to get the scroll height, it must be based on Getchildat (0). Top and Getfirstvisibleposition () to calculate the overall calculation.

The code is as follows:

public int getScrollY() {    View c = mListView.getChildAt(0);    if (c == null) {        return 0;    }    int firstVisiblePosition = mListView.getFirstVisiblePosition();    int top = c.getTop();    return -top + firstVisiblePosition * c.getHeight() ;

But we need to hide and show the buttons to process them.

Get scrolling distance

        public int getScrollY() {            View c = listView.getChildAt(0);            if (c == null) {                return 0;            }            int firstVisiblePosition = listView.getFirstVisiblePosition();            int top = c.getTop();            int headerHeight = 0;            if (firstVisiblePosition >= 1) {                headerHeight = listView.getHeight();            }            return -top +  firstVisiblePosition*c.getHeight() + headerHeight;        }

Then there is the click event issue for the two sets of buttons.
Because one layout in the main layout of the first one another in the second layout so doomed can't set them up once click events need to be set separately click events

And then it gets the height of the headview gets the No. 0 item of the ListView so no wonder the ListView item subscript is starting from 1

  //    获取head的高度    public void getmyselfheight(View v){        View vx = v.findViewById(R.id.main_section_b_inside);        aHeight = v.getTop()+v.getHeight()-vx.getHeight();    }

Finally, it's the effect, and I hope it helps.

ListView Fixed Head Label

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.