Swipe to change the title of a ListView

Source: Internet
Author: User

Reprint Please specify Source: http://blog.csdn.net/forwardyzk/article/details/42710837

We usually see that when the ListView is sliding, the content of the title changes constantly, and the caption has a push effect, and the following share an example with you.

Ideas:

1. Customize the ListView to draw a sub-title (Childview) to the ListView and set its position to (0,0,width,height)

2. Add a swipe listener event to the ListView.
When you swipe down, the title content of the currently first fully displayed item is compared to the title content
If same, the title content and location are not changed,
If not, change the content of the title to the title of the first item that is currently displayed, changing the title position
When you swipe up, the title content of the first display item is compared with the title content
If same, the title content and location are not changed,

If not, change the content of the title title to the title of the first item that is currently displayed, changing the title position

Customizing a Pinnedheaderlistview Integrated listview

public class Pinnedheaderlistview extends ListView {/** * Change header interface * */public interface Pinnedheaderconfig {void configure Pinnedheader (View header, int position);} Private Pinnedheaderconfig madapter;private View mheaderview;private boolean mheaderviewvisible;private int mheaderviewwidth;private int Mheaderviewheight;public Pinnedheaderlistview (context context) {super (context);} Public Pinnedheaderlistview (context context, AttributeSet Attrs) {Super (context, attrs);} Public Pinnedheaderlistview (context context, AttributeSet Attrs,int Defstyle) {Super (context, attrs, Defstyle);} public void Setpinnedheaderview (view view) {Mheaderview = View;if (Mheaderview! = null) {setfadingedgelength (0);} Showlog ("Setpinnedheaderview");} @Overridepublic void Setadapter (ListAdapter adapter) {Super.setadapter (adapter); madapter = (pinnedheaderconfig) adapter;} @Overrideprotected void onmeasure (int widthmeasurespec, int heightmeasurespec) {super.onmeasure (Widthmeasurespec, HEIGHTMEASURESPEC);//measure long and wide if (mheadervIew = null) {Showlog ("onmeasure"); Measurechild (Mheaderview, Widthmeasurespec, heightmeasurespec); mHeaderViewWidth = Mheaderview.getmeasuredwidth (); mheaderviewheight = Mheaderview.getmeasuredheight ();}} @Overrideprotected void OnLayout (Boolean changed, int left, int top, int. Right,int bottom) {super.onlayout (changed, left, Top, right, bottom), if (mheaderview! = null) {Showlog ("onlayout"); mheaderview.layout (0, 0, Mheaderviewwidth, Mheaderviewheight); Movetitle (Getfirstvisibleposition ());}} /** * Handles the movement of the caption * * @param position */public void movetitle (int position) {if (Mheaderview = null) {return;} View Firstview = getchildat (0), int bottom = Firstview.getbottom (); int headerheight = Mheaderview.getheight (); int Y;if (bo Ttom < headerheight) {y = (bottom-headerheight);} else {y = 0;} Sets the contents of the caption display Madapter.configurepinnedheader (mheaderview, position); if (Mheaderview.gettop ()! = y) {mheaderview.layout (0, Y, mheaderviewwidth, mheaderviewheight + y);} Mheaderviewvisible = true;} @Overrideprotected VOID Dispatchdraw (canvas canvas) {super.dispatchdraw (canvas); if (mheaderviewvisible) {showlog ("Dispatchdraw"); Drawchild (Canvas, Mheaderview, Getdrawingtime ());}} public void Showlog (String message) {LOG.D ("message", message);}}


Setpinnedheaderview (view view): Adds a caption to the ListView.

In Onmeasure (), measure the length and width of this title first,

Measurechild (Mheaderview, Widthmeasurespec, Heightmeasurespec); indicates that one of the headings in the ListView is accessed first, and then gets its length and width after it is accommodated.

In OnLayout () set the display position, mheaderview.layout (0, 0, Mheaderviewwidth, mheaderviewheight), where the length and width are obtained in onmeasure ().

It then paints its title view in Dispatchdraw (Canvas canvas), which is primarily a painting subassembly, which is drawn using the Drawchild () method.

/** * Item Information Bean class *  */public class ItemInfo {private string title;//title private string content;//content public string Getti Tle () {return title;} public void Settitle (String title) {this.title = title;} Public String getcontent () {return content;} public void SetContent (String content) {this.content = content;}}


Control the movement of the title and the transformation of the content in the sliding listener event of the ListView

@Overridepublic void Onscroll (abslistview view, int firstvisibleitem,int visibleitemcount, int totalitemcount) {if (view instanceof Pinnedheaderlistview) {showlog ("adapter-onscroll");//handles the swipe down if (!data.get (Firstvisibleitem + 1 < Data.size ()? Firstvisibleitem + 1:data.size ()-1). GetTitle (). Equals (Titlecontent)) {((Pinnedheaderlistview) view). Movetitle ( Firstvisibleitem);} else {//handles sliding up if (!data.get (Firstvisibleitem). GetTitle (). Equals (Titlecontent)) {((Pinnedheaderlistview) view). Movetitle (Firstvisibleitem);}}}


we have to deal with the two cases of sliding up and down, whether it is to swipe up or down, when appropriate, you need to change the title of the content and position, in this case, the current display of the title of the first item and its next item title of the content is not the same time, You need to change the title of the title, the title content of the first item that is currently displayed is not displayed at all, and the contents of its caption change (change to the title of the first items at this time).

The movement of the title

public void Movetitle (int position) {if (Mheaderview = = null) {return;} View Firstview = getchildat (0), int bottom = Firstview.getbottom (); int headerheight = Mheaderview.getheight (); int Y;if (bo Ttom < headerheight) {y = (bottom-headerheight);} else {y = 0;} Sets the contents of the caption display Madapter.configurepinnedheader (mheaderview, position); if (Mheaderview.gettop ()! = y) {mheaderview.layout (0, Y, mheaderviewwidth, mheaderviewheight + y);} Mheaderviewvisible = true;}


the value of the change in the Y-direction position (the height difference between the bottom of the first item and the title view of the ListView)

Mheaderview.layout (0, Y, mheaderviewwidth, mheaderviewheight + y);

Madapter.configurepinnedheader (Mheaderview, position); Set the contents of the title

public void Configurepinnedheader (View header, int position) {Showlog ("Adapter-configurepinnedheader"); titlecontent = Data.get (position). GetTitle ();((TextView) Header.findviewbyid (R.id.header_text)). SetText (String.valueof ( titlecontent));}

Steps to use:

Activity_main.xml

<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "    android:id=" @+id/listview "    android:layout_width=" fill_parent "    android:layout_height=" fill _parent "    android:orientation=" vertical ">    <com.example.view.pinnedheaderlistview        android:id=" @+id/section_list_view "        android:layout_width=" fill_parent "        android:layout_height=" Fill_parent        " Android:background= "@null"/></linearlayout>


Mainactivity.java

public void Initview () {list<iteminfo> List = new arraylist<iteminfo> () iteminfo info;for (int i = 0; i < 5 0; i++) {info = new ItemInfo (), Info.settitle (string.valueof (I/5) + "group"), Info.setcontent ("This is the" + i + "Item"); List.add (info );} adapter = new Listviewadapter (Getlayoutinflater ()); Adapter.setdata (list); ListView = (Pinnedheaderlistview) Findviewbyid (R.id.section_list_view); Listview.setadapter (adapter); Listview.setonscrolllistener (adapter); Listview.setpinnedheaderview (Getlayoutinflater (). Inflate (R.layout.child_titleview_section, ListView,false));}


Setpinnedheaderview (): Set title view, and change the TextView ID in the header in the adapter is the control in its view

SetData () Sets the loaded data, ItemInfo

Setonscrolllistener () sets the scroll listener, where the adapter implements the amount Onscrolllistener interface, so the adapter object can be written directly

source Download: http://download.csdn.net/detail/forwardyzk/8361563


:





Swipe to change the title of a ListView

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.